推送基础
简单适配
相对简单的推送证书以及环境的问题,我就不在这里讲啦,我在这里说的,是指原有工程的适配。
1.首先我们需要打开下面的开关。所有的推送平台,不管是极光还是什么的,要想收到推送,这个是必须打开的哟~
之后,系统会生成一个我们以前没见过的文件,如图:
可能产生的问题:之前有朋友反馈过,将开发环境由 development 变成 production ,在开关这里会产生错误,如图:
如果大家点击Fix issue之后,会惊奇的发现,APS Environment由 production 又变成 development 了。
解决办法:没有做任何修改
打包之后,生成的ipa包内,是没有这个.entitlements 文件的。经过测试,我发现是可以正常收到推送信息的。测试的方法如下,大家也可以测试一下。
测试方法:打包之后安装ipa文件,然后利用极光推送,选择生产环境,推送,即可。
经过上面的操作,你就会惊奇的发现,推送已经适配完毕了,iOS10的系统,已经可以正常接收通知了。
1.系统自带方法
1.iOS10的系统导入
#import <UserNotifications/UserNotifications.h>
2.注册通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//系统方法
//注册通知
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
//iOS10版本
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
//点击允许
NSLog(@"注册成功");
} else {
//点击允许
NSLog(@"注册失败");
}
}];
} else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0){
//iOS8和9的方法
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
} else {
//iOS8以下
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
}
//注册
[[UIApplication sharedApplication] registerForRemoteNotifications];
//jpush
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
JPUSHRegisterEntity *entity = [[JPUSHRegisterEntity alloc] init];
entity.types = UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
#endif
} else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
[JPUSHService registerForRemoteNotificationTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
}
[JPUSHService setupWithOption:launchOptions appKey:JPUSH_appkey channel:@"Publish channel" apsForProduction:YES];
return YES;
}
3.获取deviceToken系统和jpush方法一致
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
//获取deviceToken
NSLog(@"deviceToken是 -- %@",[NSString stringWithFormat:@"%@",deviceToken]);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//获取deviceToken失败
NSLog(@"获取deviceToken失败 -- %@",error);
}
4.接受通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSLog(@"前台 -- willPresentNotification ");
//iOS 10收到通知
UNNotificationRequest *request = notification.request;//收到推送的请求
NSDictionary *userInfo = request.content.userInfo;
UNNotificationContent *content = request.content;//收到推送的消息内容
NSNumber *badge = content.badge;//角标
NSString *body = content.body;//
UNNotificationSound *sound = content.sound;//
NSString *subtitle = content.subtitle;//
NSString *title = content.title;//
if ([request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
NSLog(@"iOS 10 前台收到远程通知 : %@",[self logDic:userInfo]);
} else {
NSLog(@"iOS10 前台收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
}
//系统方法
completionHandler(UNNotificationPresentationOptionBadge | UNAuthorizationOptionSound | UNNotificationPresentationOptionAlert);//需要执行这个方法,选择是否提醒用户,有badge、sound、alert三种类型可以设置
}
5.通知的点击事件(系统)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSLog(@" 后台 -- didReceiveNotificationResponse ");
//iOS 10收到通知
UNNotificationRequest *request = response.notification.request;//收到推送的请求
NSDictionary *userInfo = request.content.userInfo;
UNNotificationContent *content = request.content;//收到推送的消息内容
NSNumber *badge = content.badge;//角标
NSString *body = content.body;//
UNNotificationSound *sound = content.sound;//
NSString *subtitle = content.subtitle;//
NSString *title = content.title;//
if ([request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
NSLog(@"iOS 10 前台收到远程通知 : %@",[self logDic:userInfo]);
} else {
NSLog(@"iOS10 前台收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
}
//系统方法
completionHandler(UNNotificationPresentationOptionBadge | UNAuthorizationOptionSound | UNNotificationPresentationOptionAlert);//需要执行这个方法,选择是否提醒用户,有badge、sound、alert三种类型可以设置
}
旧版本的通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"iOS7及以上系统,收到通知:%@",[self logDic:userInfo]);
//jpush
[JPUSHService handleRemoteNotification:userInfo];
//系统要求
completionHandler(UIBackgroundFetchResultNewData);
}
6.jpushDelegate
-(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];
}
//系统要求
completionHandler(UNNotificationPresentationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound);
}
-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSDictionary *useInfo = response.notification.request.content.userInfo;
if ([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:useInfo];
}
//系统要求
completionHandler(UNNotificationPresentationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound);
}
7.logDic的方法
- (NSString *)logDic:(NSDictionary *)dic {
if (![dic count]) {
return nil;
}
NSString *tempStr1 = [[dic description] stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
NSString *str = [NSPropertyListSerialization propertyListFromData:tempData mutabilityOption:NSPropertyListImmutable format:NULL errorDescription:NULL];
return str;
}
如果用极光的方法,则写方法1.2.3.6.7.