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

iOS10推送之基礎(chǔ)知識(shí)(必看篇)

 更新時(shí)間:2016年09月26日 10:35:54   作者:徐不同  
相信每位iOS開(kāi)發(fā)者們都知道,在iOS10更新之后,推送也是做了一些小小的修改,下面我就給大家仔細(xì)說(shuō)說(shuō)iOS10的推送,有需要的朋友們可以參考借鑒。下面來(lái)一起看看吧。

前言

在北京時(shí)間9月14號(hào)凌晨1點(diǎn),蘋(píng)果正式推送iOS 10正式版,下面給大家詳細(xì)的介紹iOS10推送的基礎(chǔ)知識(shí),在看完簡(jiǎn)單入門(mén)篇大家就可以簡(jiǎn)單適配了,然后再通過(guò)中級(jí)篇的內(nèi)容,相信對(duì)大家學(xué)習(xí)理解有很大的幫助,下面話不多說(shuō)了,來(lái)看看吧。

一、簡(jiǎn)單入門(mén)篇

相對(duì)簡(jiǎn)單的推送證書(shū)以及環(huán)境的問(wèn)題,我就不在這里講啦,我在這里說(shuō)的,是指原有工程的適配。

1.首先我們需要打開(kāi)下面的開(kāi)關(guān)。所有的推送平臺(tái),不管是極光還是什么的,要想收到推送,這個(gè)是必須打開(kāi)的喲~


之后,系統(tǒng)會(huì)生成一個(gè)我們以前沒(méi)見(jiàn)過(guò)的文件,如圖:


可能產(chǎn)生的問(wèn)題:之前有朋友反饋過(guò),將開(kāi)發(fā)環(huán)境由 development 變成 production ,在開(kāi)關(guān)這里會(huì)產(chǎn)生錯(cuò)誤,如圖:


如果大家點(diǎn)擊Fix issue之后,會(huì)驚奇的發(fā)現(xiàn),APS Environment由 production 又變成 development 了。

解決辦法:我的建議是不做任何修改。

經(jīng)過(guò)我的測(cè)試,打包之后,生成的ipa包內(nèi),是沒(méi)有這個(gè).entitlements 文件的。經(jīng)過(guò)測(cè)試,我發(fā)現(xiàn)是可以正常收到推送信息的。測(cè)試的方法如下,大家也可以測(cè)試一下。

測(cè)試方法:打包之后安裝ipa文件,然后利用極光推送,選擇生產(chǎn)環(huán)境,推送,即可。

經(jīng)過(guò)上面的操作,你就會(huì)驚奇的發(fā)現(xiàn),推送已經(jīng)適配完畢了,iOS10的系統(tǒng),已經(jīng)可以正常接收通知了。

二、中級(jí)篇

這里我會(huì)給大家講一講iOS10的推送,如何注冊(cè),通過(guò)什么代理,哪些方法可以用,哪些方法不可以用。

1.系統(tǒng)自帶方法

大家不管是使用三方平臺(tái)的推送,還是系統(tǒng)自帶的推送,都先應(yīng)該了解下系統(tǒng)自帶方法,如何實(shí)現(xiàn)遠(yuǎn)程通知的實(shí)現(xiàn)。

第一步導(dǎo)入#import <UserNotifications/UserNotifications.h>

且要遵守<UNUserNotificationCenterDelegate>的協(xié)議,在Appdelegate.m中。

這里需要注意,我們最好寫(xiě)成這種形式

#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

第二步我們需要在 (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中注冊(cè)通知,代碼如下:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

 if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
  //iOS10特有
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  // 必須寫(xiě)代理,不然無(wú)法監(jiān)聽(tīng)通知的接收與點(diǎn)擊
  center.delegate = self;
  [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
   if (granted) {
    // 點(diǎn)擊允許
    NSLog(@"注冊(cè)成功");
    [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
     NSLog(@"%@", settings);
    }];
   } else {
    // 點(diǎn)擊不允許
    NSLog(@"注冊(cè)失敗");
   }
  }];
 }else if ([[UIDevice currentDevice].systemVersion floatValue] >8.0){
  //iOS8 - iOS10
  [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];

 }else if ([[UIDevice currentDevice].systemVersion floatValue] < 8.0) {
  //iOS8系統(tǒng)以下
  [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
 }
 // 注冊(cè)獲得device Token
 [[UIApplication sharedApplication] registerForRemoteNotifications];

其中,獲得Device Token的方法是沒(méi)有改變的。

// 獲得Device Token
 - (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
 NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]);
}
// 獲得Device Token失敗
- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
 NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}

此次iOS10系統(tǒng)的更新,蘋(píng)果給了我們2個(gè)代理方法來(lái)處理通知的接收和點(diǎn)擊事件,這兩個(gè)方法在<UNUserNotificationCenterDelegate>的協(xié)議中,大家可以查看下。此外,蘋(píng)果把本地通知跟遠(yuǎn)程通知合二為一。區(qū)分本地通知跟遠(yuǎn)程通知的類是UNPushNotificationTrigger.h類中,UNPushNotificationTrigger的類型是新增加的,通過(guò)它,我們可以得到一些通知的觸發(fā)條件,在使用時(shí),我們不應(yīng)該直接使用這個(gè)類,應(yīng)當(dāng)使用它的子類。

我簡(jiǎn)單點(diǎn)說(shuō)

1.UNPushNotificationTrigger (遠(yuǎn)程通知) 遠(yuǎn)程推送的通知類型

2.UNTimeIntervalNotificationTrigger (本地通知) 一定時(shí)間之后,重復(fù)或者不重復(fù)推送通知。我們可以設(shè)置timeInterval(時(shí)間間隔)和repeats(是否重復(fù))。

3.UNCalendarNotificationTrigger(本地通知) 一定日期之后,重復(fù)或者不重復(fù)推送通知 例如,你每天8點(diǎn)推送一個(gè)通知,只要dateComponents為8,如果你想每天8點(diǎn)都推送這個(gè)通知,只要repeats為YES就可以了。

4.UNLocationNotificationTrigger (本地通知)地理位置的一種通知,
當(dāng)用戶進(jìn)入或離開(kāi)一個(gè)地理區(qū)域來(lái)通知。在CLRegion標(biāo)識(shí)符必須是唯一的。因?yàn)槿绻嗤臉?biāo)識(shí)符來(lái)標(biāo)識(shí)不同區(qū)域的UNNotificationRequests,會(huì)導(dǎo)致不確定的行為。

接收通知的代碼如下:

// iOS 10收到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
 NSDictionary * userInfo = notification.request.content.userInfo;
 UNNotificationRequest *request = notification.request; // 收到推送的請(qǐng)求
 UNNotificationContent *content = request.content; // 收到推送的消息內(nèi)容
 NSNumber *badge = content.badge; // 推送消息的角標(biāo)
 NSString *body = content.body; // 推送消息體
 UNNotificationSound *sound = content.sound; // 推送消息的聲音
 NSString *subtitle = content.subtitle; // 推送消息的副標(biāo)題
 NSString *title = content.title; // 推送消息的標(biāo)題

 if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  NSLog(@"iOS10 前臺(tái)收到遠(yuǎn)程通知:%@", [self logDic:userInfo]);

 }
 else {
  // 判斷為本地通知
  NSLog(@"iOS10 前臺(tái)收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
 }
 completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要執(zhí)行這個(gè)方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以設(shè)置
}

下面的代碼則是通知的點(diǎn)擊事件:

// 通知的點(diǎn)擊事件
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{

 NSDictionary * userInfo = response.notification.request.content.userInfo;
 UNNotificationRequest *request = response.notification.request; // 收到推送的請(qǐng)求
 UNNotificationContent *content = request.content; // 收到推送的消息內(nèi)容
 NSNumber *badge = content.badge; // 推送消息的角標(biāo)
 NSString *body = content.body; // 推送消息體
 UNNotificationSound *sound = content.sound; // 推送消息的聲音
 NSString *subtitle = content.subtitle; // 推送消息的副標(biāo)題
 NSString *title = content.title; // 推送消息的標(biāo)題
 if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  NSLog(@"iOS10 收到遠(yuǎn)程通知:%@", [self logDic:userInfo]);

 }
 else {
  // 判斷為本地通知
  NSLog(@"iOS10 收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
 }

 // Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.
 completionHandler(); // 系統(tǒng)要求執(zhí)行這個(gè)方法

}

在點(diǎn)擊事件中,如果我們不寫(xiě)completionHandler()這個(gè)方法,可能會(huì)報(bào)一下的錯(cuò)誤,希望大家注意下~

Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.

最后最后,我們要大家補(bǔ)充一下,舊版本的一些方法,方便大家擴(kuò)充iOS10的通知的通知,不影響原有邏輯。

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
 NSLog(@"iOS6及以下系統(tǒng),收到通知:%@", [self logDic:userInfo]);
}

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:
(void (^)(UIBackgroundFetchResult))completionHandler {

 NSLog(@"iOS7及以上系統(tǒng),收到通知:%@", [self logDic:userInfo]);
 completionHandler(UIBackgroundFetchResultNewData);
}

2.極光推送(需要下載最新的版本)

如果用到三方的一些平臺(tái),做推送就會(huì)更為簡(jiǎn)單。

1.注冊(cè)通知的代碼如下

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) {
  //可以添加自定義categories
  [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
              UIUserNotificationTypeSound |
              UIUserNotificationTypeAlert)
           categories:nil];
 } else {
  //categories 必須為nil
  [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
              UIRemoteNotificationTypeSound |
              UIRemoteNotificationTypeAlert)
           categories:nil];
 }

注冊(cè)完成之后,我們則需要加入極光推送更新后,新加入的2個(gè)方法,這兩個(gè)方法在<JPUSHRegisterDelegate>代理方法中。

/*
 * @brief handle UserNotifications.framework [willPresentNotification:withCompletionHandler:]
 * @param center [UNUserNotificationCenter currentNotificationCenter] 新特性用戶通知中心
 * @param notification 前臺(tái)得到的的通知對(duì)象
 * @param completionHandler 該callback中的options 請(qǐng)使用UNNotificationPresentationOptions
 */
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler;
/*
 * @brief handle UserNotifications.framework [didReceiveNotificationResponse:withCompletionHandler:]
 * @param center [UNUserNotificationCenter currentNotificationCenter] 新特性用戶通知中心
 * @param response 通知響應(yīng)對(duì)象
 * @param completionHandler
 */
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler;

使用時(shí),只需要在上面的代碼中添加極光的處理方法就可以了,具體使用如下圖:

if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
 // 這個(gè)方法,不管是收到通知代理還是點(diǎn)擊通知的代理,如果使用極光推送,我們都是需要增加這個(gè)方法的。
 [JPUSHService handleRemoteNotification:userInfo];

 NSLog(@"iOS10 收到遠(yuǎn)程通知:%@", [self logDic:userInfo]);
 [rootViewController addNotificationCount];
 }
 else {
 // 判斷為本地通知
 NSLog(@"iOS10 收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
 }

通過(guò)上面的文章,相信大家已經(jīng)可以初步了解新版本的推送,要如何處理啦~

總結(jié)

以上就是iOS10推送之基礎(chǔ)知識(shí)的全部?jī)?nèi)容,不知道大家都學(xué)會(huì)了嗎?希望這篇文章能對(duì)各位iOS開(kāi)發(fā)者們有所幫助,如果有疑問(wèn)大家可以留言交流。

相關(guān)文章

最新評(píng)論