小白一只,有错误敬请指出
官方Demo下载:https://docs.jiguang.cn/jpush/resources/
证书配置:https://docs.jiguang.cn/jpush/client/iOS/ios_cer_guide/
- 导入Demo 中 lib中的三个文件两个 .a 文件和一个 JPUSHService.h 文件
- 参照Demo中的方法写到自己的工程里
- 每次app回到后台时清空极光后台的badge数(也有时候不需要清空,视具体情况而定,只是想说明一下记得清空后台的消息数)
- (void)applicationDidEnterBackground:(UIApplication *)application {
[application setApplicationIconBadgeNumber:0];
[JPUSHService setBadge:0];//清空JPush服务器中存储的badge值
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[application setApplicationIconBadgeNumber:0];
[JPUSHService setBadge:0];//清空JPush服务器中存储的badge值
}
- 极光推送跳转到详情页面,收到推送消息需要做处理,应该在下面几个方法,详情请看代码
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
[JPUSHService handleRemoteNotification:userInfo];
//iOS6及以下系统,收到通知 在这里处理点击通知栏的通知之后的一些列操作,执行什么方法写在这里,例如跳转到某个特定的界面
NSLog(@"iOS6及以下系统,收到通知:%@", [self logDic:userInfo]);
//userInfo是推送详情里面包含的字典,和后台商量好可以用于具体操作,如下面通过返回的 @"type1" 和 @"type2"字段 可以判断所需做的具体的操作
//判断应用处于后台还是后台
if (application.applicationState == UIApplicationStateInactive) {
if ([userInfo[@"type1"] isEqualToString:@"air"]) {
if ([userInfo[@"type2"] isEqualToString:@"0"]) {
UIViewController *vc1 = [[UIViewController alloc] init];
UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:vc1];
self.window.rootViewController = nav;
}else if ([userInfo[@"type2"] isEqualToString:@"1"]) {
UIViewController *vc2 = [[UIViewController alloc] init];
UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:vc2];
self.window.rootViewController = nav;
}
}
}else if (application.applicationState == UIApplicationStateActive) {
//处于前台一般弹出窗口提示提送详情
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//iOS7及以上系统,收到通知 在这里处理点击通知栏的通知之后的一些列操作,执行什么方法写在这里,例如跳转到某个特定的界面
[JPUSHService handleRemoteNotification:userInfo];
NSLog(@"iOS7及以上系统,收到通知:%@", [self logDic:userInfo]);
//userInfo是推送详情里面包含的字典,和后台商量好可以用于具体操作,如下面通过返回的 @"type1" 和 @"type2"字段 可以判断所需做的具体的操作
//判断应用处于后台还是后台
if (application.applicationState == UIApplicationStateInactive) {
if ([userInfo[@"type1"] isEqualToString:@"air"]) {
if ([userInfo[@"type2"] isEqualToString:@"0"]) {
UIViewController *vc1 = [[UIViewController alloc] init];
UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:vc1];
self.window.rootViewController = nav;
}else if ([userInfo[@"type2"] isEqualToString:@"1"]) {
UIViewController *vc2 = [[UIViewController alloc] init];
UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:vc2];
self.window.rootViewController = nav;
}
}
}else if (application.applicationState == UIApplicationStateActive) {
//处于前台一般弹出窗口提示提送详情
}
completionHandler(UIBackgroundFetchResultNewData);
}
//应用处于后台
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
NSDictionary * userInfo = notification.request.content.userInfo;
UNNotificationRequest *request = notification.request; // 收到推送的请求
UNNotificationContent *content = request.content; // 收到推送的消息内容
NSNumber *badge = content.badge; // 推送消息的角标
NSString *body = content.body; // 推送消息体
UNNotificationSound *sound = content.sound; // 推送消息的声音
NSString *subtitle = content.subtitle; // 推送消息的副标题
NSString *title = content.title; // 推送消息的标题
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[content.badge integerValue]];
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
NSLog(@"iOS10 前台收到远程通知:%@", [self logDic:userInfo]);
//APP在前台运行时收到通知不做反应
}
else {
// 判断为本地通知
NSLog(@"iOS10 前台收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);
}
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
}
//应用处于后台
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSDictionary * userInfo = response.notification.request.content.userInfo;
UNNotificationRequest *request = response.notification.request; // 收到推送的请求
UNNotificationContent *content = request.content; // 收到推送的消息内容
NSNumber *badge = content.badge; // 推送消息的角标
NSString *body = content.body; // 推送消息体
UNNotificationSound *sound = content.sound; // 推送消息的声音
NSString *subtitle = content.subtitle; // 推送消息的副标题
NSString *title = content.title; // 推送消息的标题
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[content.badge integerValue]];
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
NSLog(@"iOS10 收到远程通知:%@", [self logDic:userInfo]);
//userInfo是推送详情里面包含的字典,和后台商量好可以用于具体操作,如下面通过返回的 @"type1" 和 @"type2"字段 可以判断所需做的具体的操作
//应用处于后台
if ([userInfo[@"type1"] isEqualToString:@"air"]) {
if ([userInfo[@"type2"] isEqualToString:@"0"]) {
UIViewController *vc1 = [[UIViewController alloc] init];
UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:vc1];
self.window.rootViewController = nav;
}else if ([userInfo[@"type2"] isEqualToString:@"1"]) {
UIViewController *vc2 = [[UIViewController alloc] init];
UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:vc2];
self.window.rootViewController = nav;
}
}
}
else {
// 判断为本地通知
NSLog(@"iOS10 收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);
}
completionHandler(); // 系统要求执行这个方法
}
网友评论