我感覺(jué)我是幸運(yùn)的,因?yàn)楫?dāng)我準(zhǔn)備調(diào)研推送和webview的時(shí)候娩怎,蘋(píng)果爸爸已經(jīng)對(duì)他們進(jìn)行觸及靈魂的改造了搔课,我自然可以靠著大樹(shù)乘涼,直接入手iOS新版本的推送和iOS8出道的WKWebview截亦。扯皮了這么多爬泥,是時(shí)候展現(xiàn)真正的技術(shù)了柬讨!
--------------------------前方蘭博紅溫預(yù)警------------------------
本文是iOS推送系列的第一篇,主要講一下實(shí)現(xiàn)推送功能之前的準(zhǔn)備工作袍啡,以及前期的推送注冊(cè)踩官,后續(xù)將會(huì)更新更多關(guān)于iOS10推送的新鮮內(nèi)容。
iOS10的推送相比較之前的來(lái)說(shuō)葬馋,真的可以用脫胎換骨來(lái)形容卖鲤,新增了UserNotifications Framework,但使用起來(lái)其實(shí)很簡(jiǎn)單畴嘶。
一、戰(zhàn)前準(zhǔn)備
1.你必須要有1個(gè)development證書(shū)集晚,如果要發(fā)布當(dāng)然還要有一個(gè)distribution證書(shū)窗悯;
2.你必須要打開(kāi)工程里的推送開(kāi)關(guān),不打開(kāi)則一切皆為虛空偷拔。蒋院。。
Push Notifications
3.你可能還需要打開(kāi)Background Modes里的Romote notification莲绰,雖然作者現(xiàn)在還有搞清這東西有軟用欺旧,還忘知道的同志留言分享。
Reomote notification
4.閱讀蘋(píng)果爸爸給孩子們寫(xiě)的信---官方文檔蛤签,既能提升文檔閱讀能力辞友,還能加深對(duì)框架的理解,順便把英語(yǔ)給學(xué)了震肮,穩(wěn)賺不賠的買(mǎi)賣(mài)称龙。其實(shí)看文檔,寫(xiě)文檔是一個(gè)開(kāi)發(fā)人員行走江湖的必備技能戳晌。
二鲫尊、iOS10前后的推送注冊(cè)
(附贈(zèng)兩個(gè)三方推送注冊(cè))
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end
/* 判斷機(jī)型 */
// 建議宏和一些常用參數(shù)都添加到項(xiàng)目的config文件中
#define isiOS10 ([[[UIDevice currentDevice]systemVersion]floatValue] >= 10.0)
#define isiOS7 ([[[UIDevice currentDevice]systemVersion]floatValue] >= 7.0)
#define isiOS8 ([[[UIDevice currentDevice]systemVersion]floatValue] >= 8.0)
#define isiOS7_1 ([[[UIDevice currentDevice]systemVersion]floatValue] > 7.0)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 1.系統(tǒng)通知
// 推送注冊(cè),分為iOS10之后和之前
if (ISIOS10) {
// iOS10使用以下方法注冊(cè)沦偎,才能得到授權(quán)
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
UNAuthorizationOptions types10 = UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound;
[center requestAuthorizationWithOptions:types10 completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) { // 獲取通知授權(quán)成功
NSLog(@"Request UNUserNofication authorization succeeded.");
if (granted) { // 點(diǎn)擊允許,這里可以添加一些自己的邏輯
NSLog(@"用戶允許通知");
} else { // 點(diǎn)擊不允許,這里可以添加一些自己的邏輯
NSLog(@"用戶不允許通知");
}
// (2)獲取當(dāng)前通知疫向, UNNotificationSetting只是讀對(duì)象,不能修改豪嚎,只能通過(guò)以下方法獲取
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
// 此處查看并設(shè)置通知相關(guān)信息
}];
} else {
NSLog(@"Request UNUserNofication authorization failed.");
}
}];
}
//iOS8系統(tǒng)以下
else if (isiOS8){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];
}
// iOS8系統(tǒng)以下
else {
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
}
// 遠(yuǎn)程推送注冊(cè)-必須加的一個(gè)方法搔驼!
[[UIApplication sharedApplication] registerForRemoteNotifications];
// 2.UMPush
// (1)UM推送初始化
[UMessage startWithAppkey:UMAppKey launchOptions:launchOptions];
// (2)UM通知注冊(cè)
[UMessage registerForRemoteNotifications];
// (3)UM日志
[UMessage setLogEnabled:YES];
// 3.用友有信
// (1)有信IM相關(guān)設(shè)置
[[YYIMChat sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
// (2)注冊(cè)app
[[YYIMChat sharedInstance] registerApp:YYAPPIDNew etpKey:@"cidtech"];
// (3)注冊(cè)多方通話
[[YYIMChat sharedInstance].chatManager registerDuduWithAccountIdentify:@"" appkeyTemp:@""];
// (4)添加代理
[[YYIMChat sharedInstance].chatManager addDelegate:self];
// (5)注冊(cè)token代理
[[YYIMChat sharedInstance].chatManager registerTokenDelegate:self];
// (6)設(shè)置日志級(jí)別
[[YYIMChat sharedInstance] setLogLevel:YYIM_LOG_LEVEL_VERBOSE];
// (7)本地推送
[[YYIMChat sharedInstance].chatManager setEnableLocalNotification:YES];
// (8)注冊(cè)推送證書(shū)
#if defined(DEBUG) && DEBUG
[[YYIMChat sharedInstance] registerApnsCerName:@"你的開(kāi)發(fā)push證書(shū)"];
#else
[[YYIMChat sharedInstance] registerApnsCerName:@"你的生產(chǎn)push證書(shū)"];
#endif
// (9)設(shè)置高德地圖key,參見(jiàn)高德地圖官網(wǎng)(有信IM內(nèi)置的發(fā)送位置功能需要集成高德地圖API)
[MAMapServices sharedServices].apiKey = kYYMapKey;
}
/** 獲取deviceToken 存儲(chǔ)本地以及相關(guān)注冊(cè) */
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
// 1.有信IM推送注冊(cè)
[[YYIMChat sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
// 2.有盟U-Push推送注冊(cè)
[UMessage registerDeviceToken:deviceToken];
// 3.deviceToken存儲(chǔ)
NSString *deviceTokenAppend = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
[[NSUserDefaults standardUserDefaults] setObject:deviceTokenAppend forKey:DEVICETOKEN];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"deviceToken-------%@",deviceTokenAppend);
}
// iOS10以下系統(tǒng)
else {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
}
// 遠(yuǎn)程推送注冊(cè)-必須加的一個(gè)方法疙渣!
[[UIApplication sharedApplication] registerForRemoteNotifications];
/** iOS10以下-接收到遠(yuǎn)程通知 */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[self.rootVC.tabBarView selectWithIndex:0];
[self.rootVC selectViewControllerWithIndex:0];
[[YYIMChat sharedInstance] application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
[[NSNotificationCenter defaultCenter] postNotificationName:VERIFYNOTIFICATION_REMOTE object:userInfo];
}
/** iOS10以下-接收到本地通知 */
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[[YYIMChat sharedInstance] application:application didReceiveLocalNotification:notification];
}
// iOS10新增:處理前臺(tái)收到通知的代理方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//應(yīng)用處于前臺(tái)時(shí)的遠(yuǎn)程推送接受
//關(guān)閉友盟自帶的彈出框
[UMessage setAutoAlert:NO];
//必須加這句代碼
[UMessage didReceiveRemoteNotification:userInfo];
} else {
//應(yīng)用處于前臺(tái)時(shí)的本地推送接受
}
//當(dāng)應(yīng)用處于前臺(tái)時(shí)提示設(shè)置匙奴,需要哪個(gè)可以設(shè)置哪一個(gè)
completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert);
}
// iOS10新增:處理后臺(tái)點(diǎn)擊通知的代理方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
// 必須加這句代碼
[UMessage didReceiveRemoteNotification:userInfo];
// 應(yīng)用處于后臺(tái)時(shí)的遠(yuǎn)程推送接受
} else {
// 應(yīng)用處于后臺(tái)時(shí)的本地推送接受
}
}
以上是iOS10以及之前版本推送和三方推送有盟推送、用友推送的注冊(cè)妄荔,下面我們就來(lái)看看iOS10推送真正令人驚艷的地方
三泼菌、真正令人激動(dòng)的功能
iOS10之前的推送是這樣的
iOS10前的推送
iOS10之后的推送是這樣的
iOS10后的推送
還可以是這樣的
附帶語(yǔ)音的推送