您的位置:首页 > 其它

极光推送的集成

2017-08-23 10:24 351 查看
AppDelegate 遵循代理方法 JPUSHRegisterDelegate

在 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 ;
}

}];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  极光推送