推送基礎(chǔ)
簡單適配
相對簡單的推送證書以及環(huán)境的問題,我就不在這里講啦你虹,我在這里說的并徘,是指原有工程的適配遣钳。
1.首先我們需要打開下面的開關(guān)。所有的推送平臺饮亏,不管是極光還是什么的耍贾,要想收到推送阅爽,這個是必須打開的喲~
屏幕快照 2016-10-28 上午11.05.49.png
之后,系統(tǒng)會生成一個我們以前沒見過的文件荐开,如圖:
屏幕快照 2016-10-28 上午11.07.29.png
可能產(chǎn)生的問題:之前有朋友反饋過付翁,將開發(fā)環(huán)境由 development 變成 production ,在開關(guān)這里會產(chǎn)生錯誤,如圖:
屏幕快照 2016-10-28 上午11.08.01.png
如果大家點(diǎn)擊Fix issue之后晃听,會驚奇的發(fā)現(xiàn)百侧,APS Environment由 production 又變成 development 了。
解決辦法:沒有做任何修改
打包之后能扒,生成的ipa包內(nèi)佣渴,是沒有這個.entitlements 文件的。經(jīng)過測試初斑,我發(fā)現(xiàn)是可以正常收到推送信息的辛润。測試的方法如下,大家也可以測試一下见秤。
測試方法:打包之后安裝ipa文件砂竖,然后利用極光推送,選擇生產(chǎn)環(huán)境鹃答,推送乎澄,即可。
經(jīng)過上面的操作测摔,你就會驚奇的發(fā)現(xiàn)置济,推送已經(jīng)適配完畢了,iOS10的系統(tǒng)锋八,已經(jīng)可以正常接收通知了浙于。
1.系統(tǒng)自帶方法
1.iOS10的系統(tǒng)導(dǎo)入
#import <UserNotifications/UserNotifications.h>
2.注冊通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//系統(tǒng)方法
//注冊通知
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) {
//點(diǎn)擊允許
NSLog(@"注冊成功");
} else {
//點(diǎn)擊允許
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系統(tǒng)和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;//收到推送的消息內(nèi)容
NSNumber *badge = content.badge;//角標(biāo)
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 前臺收到遠(yuǎn)程通知 : %@",[self logDic:userInfo]);
} else {
NSLog(@"iOS10 前臺收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@查库,\\\\nsound:%@路媚,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
}
//系統(tǒng)方法
completionHandler(UNNotificationPresentationOptionBadge | UNAuthorizationOptionSound | UNNotificationPresentationOptionAlert);//需要執(zhí)行這個方法,選擇是否提醒用戶樊销,有badge整慎、sound、alert三種類型可以設(shè)置
}
5.通知的點(diǎn)擊事件(系統(tǒng))
- (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;//收到推送的消息內(nèi)容
NSNumber *badge = content.badge;//角標(biāo)
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 前臺收到遠(yuǎn)程通知 : %@",[self logDic:userInfo]);
} else {
NSLog(@"iOS10 前臺收到本地通知:{\\\\nbody:%@围苫,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@裤园,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
}
//系統(tǒng)方法
completionHandler(UNNotificationPresentationOptionBadge | UNAuthorizationOptionSound | UNNotificationPresentationOptionAlert);//需要執(zhí)行這個方法剂府,選擇是否提醒用戶拧揽,有badge、sound、alert三種類型可以設(shè)置
}
舊版本的通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"iOS7及以上系統(tǒng)淤袜,收到通知:%@",[self logDic:userInfo]);
//jpush
[JPUSHService handleRemoteNotification:userInfo];
//系統(tǒng)要求
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];
}
//系統(tǒng)要求
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];
}
//系統(tǒng)要求
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.