极光推送的集成
2017-08-23 10:24
351 查看
AppDelegate 遵循代理方法 JPUSHRegisterDelegate
在 AppDelegate.m 中的 didFinishLaunchingWithOptions 代理方法中加入下面代码
在AppDelegate.m 中加入下面的方法
在 AppDelegate.m 中的 didFinishLaunchingWithOptions 代理方法中加入下面代码
/** -------------- 配置极光推送 -------------- */ if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) { /** 初始化APNs */ JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init]; entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound; [JPUSHService registerForRemoteNotificationConfig:entity delegate:self]; } /** 初始化JPush */ else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { //可以添加自定义categories [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; } [JPUSHService setupWithOption:launchOptions appKey:@"23e1c6a763ce5b349b22a639" channel:@"App Store" apsForProduction:NO advertisingIdentifier:nil]; // appKey去极光官网申请 channel:这个参数随便写个字符串就行 apsForProduction:是否为生产环境 这里是没有advertisingIdentifier的情况,有的话,大家在自行添加 //注册远端消息通知获取device token [application registerForRemoteNotifications]; NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; if (remoteNotification) { // 程序完全退出时,点击通知,添加需求。。。 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"接收到了通知" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; }
在AppDelegate.m 中加入下面的方法
// 获取deviceToken - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [JPUSHService registerDeviceToken:deviceToken]; } // ios 10 support 处于前台时接收到通知 - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler { NSDictionary * userInfo = notification.request.content.userInfo; if ([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { [JPUSHService handleRemoteNotification:userInfo]; // 添加各种需求。。。。。 // 应用处于前台收到推送的时候转成本地通知 =========================== UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.alertTitle = userInfo[@"aps"][@"alert"]; notification.userInfo = userInfo; [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; [self findNoReadNotice]; } completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert); // 处于前台时,添加需求,一般是弹出alert跟用户进行交互,这时候completionHandler(UNNotificationPresentationOptionAlert)这句话就可以注释掉了,这句话是系统的alert,显示在app的顶部, } // iOS 10 Support 程序运行于前台,后台 或杀死 点击推送通知 都会走这个方法 - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { // Required NSDictionary * userInfo = response.notification.request.content.userInfo; if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { [JPUSHService handleRemoteNotification:userInfo]; //推送打开 if (userInfo) { // 取得 APNs 标准信息内容 // NSDictionary *aps = [userInfo valueForKey:@"aps"]; // NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容 // NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge数量 // NSString *sound = [aps valueForKey:@"sound"]; //播放的声音 // 取得Extras字段内容 /** 页面跳转业务处理 */ NSString *customStatus = [userInfo valueForKey:@"r_no"]; //服务端中Extras字段,key是自己定义的,需要与极光的附加字段一致 //跳转页面调用 self 的 pushToViewControllerWithVC 方法 /** 推送编号 */ NSString *n_id = [userInfo valueForKey:@"n_id"]; [self readNotificationWithn_id:n_id]; /** 未读消息数 */ int count = [[userInfo valueForKey:@"count"] intValue]; [JPUSHService setBadge:count];//设置JPush服务器中存储的badge值 [JPUSHService handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData); [self findNoReadNotice]; } completionHandler(); // 系统要求执行这个方法 } } /** 跳转对应控制器 */ -(void)pushToViewControllerWithVC:(UIViewController *)vc { UITabBarController *tab = (UITabBarController *)self.window.rootViewController; UINavigationController *nvc = tab.selectedViewController; UIViewController *viewController = nvc.visibleViewController; viewController.hidesBottomBarWhenPushed = YES; [viewController.navigationController pushViewController:vc animated:YES]; viewController.hidesBottomBarWhenPushed = NO; } //iOS 10以前的统计远程推送信息 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // Required, iOS 7 Support if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0) { [JPUSHService handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData); [self findNoReadNotice]; } } - (void)applicationDidEnterBackground:(UIApplication *)application { [[UIApplication alloc] setApplicationIconBadgeNumber:0]; } // 点击之后badge清零 - (void)applicationWillEnterForeground:(UIApplication *)application { [application setApplicationIconBadgeNumber:0]; [JPUSHService setBadge:0];//清空JPush服务器中存储的badge值 [[UNUserNotificationCenter alloc] removeAllPendingNotificationRequests]; } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); } /** 查看是否有未读消息 */ - (void)findNoReadNotice { NSMutableDictionary *dic = [NSMutableDictionary dictionary]; dic[@"uid"] = [KPUserDefaults sharedKPUserDefaults].uid; [[JKNetworkingTool sharedNetworkingTool] postDataWithUrl:@"Reward/findNoReadNotice" parameters:dic finishedBlock:^(id responseObj, NSError *error) { if (error) { return ; } NSString *status = [NSString stringWithFormat:@"%@",responseObj[@"data"][@"status"]]; UITabBarController *tab = (UITabBarController *)self.window.rootViewController; [tab.tabBar showBadgeOnItmIndex:3]; if ([status isEqualToString:@"1"]) { [tab.tabBar showBadgeOnItmIndex:3]; }else { [tab.tabBar hideBadgeOnItemIndex:3]; } }]; } /** 阅读某条消息调用此接口 */ - (void)readNotificationWithn_id:(NSString *)n_id { NSMutableDictionary *dic = [NSMutableDictionary dictionary]; dic[@"uid"] = [KPUserDefaults sharedKPUserDefaults].uid; dic[@"n_id"] = n_id; [[JKNetworkingTool sharedNetworkingTool] postDataWithUrl:@"Reward/noticeIsRead" parameters:dic finishedBlock:^(id responseObj, NSError *error) { if (error) { return ; } }]; }
相关文章推荐
- 【快速搞定】2分钟搞定极光推送(极光推送Android端集成)
- 使用极光推送实现分组发送和服务端集成
- 集成第三方SDK爬坑总结之极光推送和友盟统计
- 极光推送Push 标签、别名的使用 ,集成过程中遇到的问题及结局方法
- 集成极光推送的几个步骤
- Android JPush的集成 极光推送
- android集成极光消息推送
- iOS极光推送集成
- iOS极光推送集成
- 远程推送,集成极光的SDK,证书制造
- Android Studio集成极光推送
- swift3.0 集成极光推送(v2.2)iOS10.0最新写法
- 怎样使用Android studio将极光推送集成
- 集成极光推送服务,实现通知推送功能
- Yii2集成极光推送(自定义推送内容)
- android studio集成极光推送
- 集成第三方SDK爬坑总结之极光推送和友盟统计
- iOS 极光推送集成
- ionic应用集成极光推送插件jpush