欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS10 適配遠(yuǎn)程推送功能實(shí)現(xiàn)代碼

 更新時(shí)間:2016年09月19日 10:12:42   作者:北方人在上海  
這篇文章主要介紹了iOS10 適配遠(yuǎn)程推送功能實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

iOS10正式版發(fā)布之后,網(wǎng)上各種適配XCode8以及iOS10的文章滿天飛。但對(duì)于iOS10適配遠(yuǎn)程推送的文章卻不多。iOS10對(duì)于推送的修改還是非常大的,新增了UserNotifications Framework,今天就結(jié)合自己的項(xiàng)目,說(shuō)一說(shuō)實(shí)際適配的情況。 

一、Capabilities中打開(kāi)Push Notifications 開(kāi)關(guān)

在XCode7中這里的開(kāi)關(guān)不打卡,推送也是可以正常使用的,但是在XCode8中,這里的開(kāi)關(guān)必須要打開(kāi),不然會(huì)報(bào)錯(cuò):

Error Domain=NSCocoaErrorDomain Code=3000 "未找到應(yīng)用程序的“aps-environment”的授權(quán)字符串" UserInfo={NSLocalizedDescription=未找到應(yīng)用程序的“aps-environment”的授權(quán)字符串}

打開(kāi)后會(huì)生成entitlements文件,在這里可以設(shè)置APS Environment 

二、推送的注冊(cè)

首先引入U(xiǎn)serNotifications Framework,

import <UserNotifications/UserNotifications.h>
iOS10修改了注冊(cè)推送的方法,這里我們又要對(duì)不同版本分別進(jìn)行設(shè)置了。在application didFinishLaunchingWithOptions方法中修改以前的推送設(shè)置(我只實(shí)現(xiàn)了iOS8以上的設(shè)置) 

if (IOS_VERSION >= 10.0) {
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  center.delegate = self;
  [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
   if (!error) {
    DLog(@"request authorization succeeded!");
   }
  }];
 } else {
  if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
   //IOS8,創(chuàng)建UIUserNotificationSettings,并設(shè)置消息的顯示類類型
   UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];
 
   [application registerUserNotificationSettings:notiSettings];
  }
 }

三、UNUserNotificationCenterDelegate代理實(shí)現(xiàn)

在iOS10中處理推送消息需要實(shí)現(xiàn)UNUserNotificationCenterDelegate的兩個(gè)方法: 

復(fù)制代碼 代碼如下:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler  
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler

其中第一個(gè)方法為App在前臺(tái)的時(shí)候收到推送執(zhí)行的回調(diào)方法,第二個(gè)為App在后臺(tái)的時(shí)候,點(diǎn)擊推送信息,進(jìn)入App后執(zhí)行的 回調(diào)方法。

以前處理推送,信息是在userInfo參數(shù)中,而新方法中表明上看并沒(méi)有這個(gè)參數(shù),其實(shí)我們一樣可以獲取到userInfo,如下: 

/// App在前臺(tái)時(shí)候回調(diào)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
 NSDictionary *userInfo = notification.request.content.userInfo;
 [self handleRemoteNotificationForcegroundWithUserInfo:userInfo];
}
 
/// App在后臺(tái)時(shí)候點(diǎn)擊推送調(diào)用
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
 NSDictionary *userInfo = response.notification.request.content.userInfo;
 [self handleRemoteNotificationBackgroundWithUserInfo:userInfo];
}

完成上面三個(gè)步驟的設(shè)置,對(duì)于iOS10的推送設(shè)置基本就適配了。要想自定義Notification Content或者實(shí)現(xiàn)其他NotificationAction請(qǐng)參考其他文章。這里只是做了對(duì)iOS10的適配。

本文已被整理到了《iOS推送教程》,歡迎大家學(xué)習(xí)閱讀。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論