最近项目集成推送功能,我把总体的集成步骤及推送功能实现服务器
功能实现: @在登陆状态下,才可推送消息成功,退出登陆状态,不可推送消息app
@应用程序在后台挂起状态下,可实现推送,进入相应界面ide
@应用程序在前台状况下,不管任何界面,可实现推送给,并进入相应界面--声音设置fetch
@应用程序被杀死,但登陆过状态下,还能收到消息ui
须要在登陆时,设置alias,退出登陆时,清除aliasurl
- #import "JPUSHService.h"// 引入JPush功能所需头文件
- #ifdef NSFoundationVersionNumber_iOS_9_x_Max// iOS10注册APNs所需头文件
- #import <UserNotifications/UserNotifications.h>
- #endif
- //#import <AdSupport/AdSupport.h>// 若是须要使用idfa功能所须要引入的头文件(可选)
- #define JPushAppKey @"9961aac66c9ee59685fb1d4e"//推送的appkey
- #define JPushChannel @"App Store"
- #ifdef DEBUG//0 (默认值)表示采用的是开发证书,1 表示采用生产证书发布应用。
- #define isProduction NO
- #else
- #define isProduction YES
- #endif
- // iOS 10 Support 推送放放- 播放声音设置
- - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
- // Required 收到的推送消息
- NSDictionary * userInfo = notification.request.content.userInfo;
- if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
- [JPUSHService handleRemoteNotification:userInfo];
- // 添加各类需求。。。。。
- //组装并播放音效
- SystemSoundID soundID = 1000;
- //NSString *path = [[NSBundle mainBundle] pathForResource:@"video_new" ofType:@"caf"];
- NSURL *url = [[NSBundle mainBundle] URLForResource:@"video_new" withExtension:@"caf"];
- if (url) {
- OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)url,&soundID);
- if (error != kAudioServicesNoError) {//获取的声音的时候,出现错误
- soundID = 1000;
- }
- }
- AudioServicesPlaySystemSound(soundID);
- AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//震动
- }
- completionHandler(UNNotificationPresentationOptionAlert);
- // 须要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型能够选择设置
- }
- //注册APNs成功并上报DeviceToken
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- // Required - 注册 DeviceToken
- [JPUSHService registerDeviceToken:deviceToken];
- NSLog(@"√√√√√%@",[JPUSHService registrationID]);//同一设备 卸载从新安装登陆都不一样
- //得到注册后的regist_id,此值通常传给后台作推送的标记用,先存储起来
- _registerid = [JPUSHService registrationID];
- //提交register_id给后台(此方法为后台提供)
- if (ApplicationDelegate.isLogin) {
- if ([_registerid length]) {
- [self giveRegisterId:[JPUSHService registrationID]];
- }
- }
- //设置别名,提交得到的register_id给后台
- // 这是极光提供的方法,USER_INFO.userID是用户的id,你能够根据帐号或者其余来设置,只要保证惟一即可
- NSSet *set2 = [[NSSet alloc] initWithObjects:_registerid, nil];
- [JPUSHService setTags:set2 alias:_registerid fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
- NSLog(@"设置结果:%i 用户别名:%@",iResCode,@"USER_INFO.userID");
- }];
- }
- //删除推送的alias
- [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
- NSLog(@"rescode: %ld, \ntags: %@, \nalias: %@\n", (long)iResCode, @"tag" , iAlias);
- } seq:0];
扩展:极光推送中的定向推送
极光推送中,不使用广播推送,那么怎样作到定向推送,是开发者和需求必定会出现的问题,极光推送中能够有两个惟一值:
(1)注册Jpush成功后生成的registrationID,这个registrationID是标记设备惟一性的,你发现,当你在启动屡次,注册Jpush时,这个值是不变的;在同一个设备上,更换用户登陆,这个值仍然不变;最后,你删除应用程序,再下载时启动注册Jpush,这个值仍是不变。这就能够定向向某台设备作推送,若是你能给本身的服务器上传这个值,而且给这个值绑定一些东西,是否是能够作更多事情呢。
(2)alias:只要了解极光推送的都知道这是设置别名的,官方文档上说明了这个值不是惟一的,可是建议开发者把它做为用户的惟一标记。我以为这个做为惟一值是最好的,当你想定向向某个用户作推送,或者召唤他回归咱们的应用程序,这个值就太好了。你能够将它设置为userId,这个时候推送就能知道向哪一个用户发了。spa
连接:https://www.jianshu.com/p/6cca682a2892代理
连接:https://www.jianshu.com/p/6a6166aadfe7code