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

iOS10 推送最新特性研究

 更新時(shí)間:2016年09月21日 11:56:34   作者:幸福小禰  
這篇文章主要為大家詳細(xì)研究了iOS10 推送的最新特性,推送內(nèi)容更加豐富,感興趣的小伙伴們可以參考一下

最近在研究iOS10關(guān)于推送的新特性, 相比之前確實(shí)做了很大的改變,總結(jié)起來主要是以下幾點(diǎn):

 1.推送內(nèi)容更加豐富,由之前的alert 到現(xiàn)在的title, subtitle, body
 2.推送統(tǒng)一由trigger觸發(fā)
 3.可以為推送增加附件,如圖片、音頻、視頻,這就使推送內(nèi)容更加豐富多彩
 4.可以方便的更新推送內(nèi)容 

import 新框架

添加新的框架 UserNotifications.framework

 

#import <UserNotifications/UserNotifications.h> 

注冊推送 

在設(shè)置通知的時(shí)候,需要先進(jìn)行注冊,獲取授權(quán)
iOS10 所有通知都是通過UNUserNotificationCenter來管理,包括遠(yuǎn)程通知和本地通知

  //iOS8以下
  [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];

  //iOS8 - iOS10
  [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];

  //iOS10
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {

  }

獲取用戶設(shè)置 

iOS10 提供了獲取用戶授權(quán)相關(guān)設(shè)置信息的接口getNotificationSettingsWithCompletionHandler: , 回調(diào)帶有一個(gè)UNNotificationSettings對象,它具有以下屬性,可以準(zhǔn)確獲取各種授權(quán)信息

authorizationStatus
soundSetting
badgeSetting
alertSetting
notificationCenterSetting
lockScreenSetting
carPlaySetting
alertStyle 

像下面的方法,點(diǎn)擊allow

   UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
     if (granted) {
        //點(diǎn)擊允許
        NSLog(@"注冊通知成功");
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        NSLog(@"%@", settings);
        }];
      } else {
        //點(diǎn)擊不允許
        NSLog(@"注冊通知失敗");
      }
    }];

打印信息:   *<UNNotificationSettings: 0x174090a90; authorizationStatus: Authorized, notificationCenterSetting: Enabled, soundSetting: Enabled, badgeSetting: Enabled, lockScreenSetting: Enabled, alertSetting: NotSupported, carPlaySetting: Enabled, alertStyle: Banner>* 

注冊APNS, 獲取token 

iOS10, 注冊APNS和獲取token的方法還和之前一樣
application: didFinishLaunchingWithOptions:調(diào)用 registerForRemoteNotifications方法
 [[UIApplication sharedApplication] registerForRemoteNotifications]; 

在代理方法application: didRegisterForRemoteNotificationsWithDeviceToken:中獲取token

 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0){
    NSLog(@"deviceToken:%@",deviceToken);
  }

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error NS_AVAILABLE_IOS(3_0){
    NSLog(@"didFailToRegisterForRemoteNotificationsWithError:%@",error);
  }
 

設(shè)置處理通知的action 和 category 

在iOS8以前是沒有category這個(gè)屬性的;
在iOS8注冊推送,獲取授權(quán)的時(shí)候,可以一并設(shè)置category, 注冊的方法直接帶有這個(gè)參數(shù);
在iOS10, 需要調(diào)用一個(gè)方法setNotificationCategories:來為管理推送的UNUserNotificationCenter實(shí)例設(shè)置category, category又可以對應(yīng)設(shè)置action;

 //設(shè)置category
//UNNotificationActionOptionAuthenticationRequired 需要解鎖
//UNNotificationActionOptionDestructive 顯示為紅色
//UNNotificationActionOptionForeground  點(diǎn)擊打開app

UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"action1" title:@"策略1行為1" options:UNNotificationActionOptionForeground];

UNTextInputNotificationAction *action2 = [UNTextInputNotificationAction actionWithIdentifier:@"action2" title:@"策略1行為2" options:UNNotificationActionOptionDestructive textInputButtonTitle:@"comment" textInputPlaceholder:@"reply"];

 //UNNotificationCategoryOptionNone
 //UNNotificationCategoryOptionCustomDismissAction 清除通知被觸發(fā)會走通知的代理方法
 //UNNotificationCategoryOptionAllowInCarPlay    適用于行車模式
UNNotificationCategory *category1 = [UNNotificationCategory categoryWithIdentifier:@"category1" actions:@[action2,action1] minimalActions:@[action2,action1] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];

UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"action3" title:@"策略2行為1" options:UNNotificationActionOptionForeground];

UNNotificationAction *action4 = [UNNotificationAction actionWithIdentifier:@"action4" title:@"策略2行為2" options:UNNotificationActionOptionForeground];
UNNotificationCategory *category2 = [UNNotificationCategory categoryWithIdentifier:@"category2" actions:@[action3,action4] minimalActions:@[action3,action4] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];

[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category1,category2, nil]];
 

設(shè)置通知內(nèi)容 

因?yàn)閕OS10遠(yuǎn)程通知與本地通知統(tǒng)一起來了,通知內(nèi)容屬性是一致的,不過遠(yuǎn)程推送就需要在payload進(jìn)行具體設(shè)置了,下面以本地通知為例,介紹關(guān)于UNNotificationContent的內(nèi)容
官網(wǎng)上明確說明了,我們是不能直接創(chuàng)建UNNotificationContent的實(shí)例的, 如果我們需要自己去配置內(nèi)容的各個(gè)屬性,我們需要用到UNMutableNotificationContent
看一下它的一些屬性:
attachments          //附件
badge                //徽標(biāo)
body                 //推送內(nèi)容body
categoryIdentifier   //category標(biāo)識
launchImageName      //點(diǎn)擊通知進(jìn)入應(yīng)用的啟動圖
sound               //聲音
subtitle            //推送內(nèi)容子標(biāo)題
title               //推送內(nèi)容標(biāo)題
userInfo           //遠(yuǎn)程通知內(nèi)容

UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  content.title = @"Test";
  content.subtitle = @"1234567890";
  content.body = @"Copyright © 2016年 jpush. All rights reserved.";
  content.badge = @1;
  NSError *error = nil;
  NSString *path = [[NSBundle mainBundle] pathForResource:@"718835727" ofType:@"png"];
  UNNotificationAttachment *att = [UNNotificationAttachment attachmentWithIdentifier:@"att1" URL:[NSURL fileURLWithPath:path] options:nil error:&error];
  if (error) {
    NSLog(@"attachment error %@", error);
  }
  content.attachments = @[att];
  content.categoryIdentifier = @"category1”; //這里設(shè)置category1, 是與之前設(shè)置的category對應(yīng)
  content.launchImageName = @"1-Eb_0OvtcxJXHZ7-IOoBsaQ";

UNNotificationSound *sound = [UNNotificationSound defaultSound];
content.sound = sound;

 

通知觸發(fā)器 

UNNotificationTrigger
iOS 10觸發(fā)器有4種
 •UNPushNotificationTrigger 觸發(fā)APNS服務(wù),系統(tǒng)自動設(shè)置(這是區(qū)分本地通知和遠(yuǎn)程通知的標(biāo)識)
 •UNTimeIntervalNotificationTrigger 一段時(shí)間后觸發(fā)
 •UNCalendarNotificationTrigger 指定日期觸發(fā)
 •UNLocationNotificationTrigger 根據(jù)位置觸發(fā),支持進(jìn)入某地或者離開某地或者都有

 //十秒后
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];

//每周日早上8:00
NSDateComponents *component = [[NSDateComponents alloc] init];
component.weekday = 1;
component.hour = 8;
UNCalendarNotificationTrigger *trigger2 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:component repeats:YES];

//圓形區(qū)域,進(jìn)入時(shí)候進(jìn)行通知
CLLocationCoordinate2D cen = CLLocationCoordinate2DMake(80.335400, -90.009201);
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:cen
                               radius:500.0 identifier:@“center"];
region.notifyOnEntry = YES; //進(jìn)入的時(shí)候
region.notifyOnExit = NO;  //出去的時(shí)候
UNLocationNotificationTrigger *trigger3 = [UNLocationNotificationTrigger
  triggerWithRegion:region repeats:NO];
 

添加通知 / 更新通知

 1.創(chuàng)建一個(gè)UNNotificationRequest類的實(shí)例,一定要為它設(shè)置identifier, 在后面的查找,更新, 刪除通知,這個(gè)標(biāo)識是可以用來區(qū)分這個(gè)通知與其他通知
 2.把request加到UNUserNotificationCenter, 并設(shè)置觸發(fā)器,等待觸發(fā)
 3.
如果另一個(gè)request具有和之前request相同的標(biāo)識,不同的內(nèi)容, 可以達(dá)到更新通知的目的

  NSString *requestIdentifer = @"TestRequest";
  UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifer content:content trigger:trigger1];
  //把通知加到UNUserNotificationCenter, 到指定觸發(fā)點(diǎn)會被觸發(fā)
  [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  }];

 //在另外需要更新通知的地方
UNMutableNotificationContent *newContent = [[UNMutableNotificationContent alloc] init];
newContent.title = @"Update";
newContent.subtitle = @"XXXXXXXXX";
newContent.body = @"Copyright © 2016年 jpush. All rights reserved.";
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
 UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"TestRequest" content:newContent trigger:trigger1];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

}];

 

獲取和刪除通知 

這里通知是有兩種狀態(tài)
 •Pending 等待觸發(fā)的通知
 •Delivered 已經(jīng)觸發(fā)展示在通知中心的通知

 //獲取未觸發(fā)的通知
[[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
  NSLog(@"pending: %@", requests);
}];

//獲取通知中心列表的通知
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
  NSLog(@"Delivered: %@", notifications);
}];

 //清除某一個(gè)未觸發(fā)的通知
 [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[@"TestRequest1"]];
 //清除某一個(gè)通知中心的通知
 [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[@"TestRequest2"]];
 //對應(yīng)的刪除所有通知
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];

 delegate 

<UNUserNotificationCenterDelegate> 

iOS10收到通知不再是在application: didReceiveRemoteNotification:方法去處理, iOS10推出新的代理方法,接收和處理各類通知(本地或者遠(yuǎn)程)

 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  //應(yīng)用在前臺收到通知
  NSLog(@"========%@", notification);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
  //點(diǎn)擊通知進(jìn)入應(yīng)用
  NSLog(@"response:%@", response);
}

最后 

下一篇文章繼續(xù)介紹關(guān)于富媒體推送的 UNNotificationServiceExtension 和 Notification content extension, 未完待續(xù)。。。

相關(guān)文章

  • iOS 中weak的實(shí)現(xiàn)代碼示例

    iOS 中weak的實(shí)現(xiàn)代碼示例

    本篇文章主要介紹了iOS 中weak的實(shí)現(xiàn)代碼示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05
  • CocoaPods1.9.0 安裝使用教程詳解

    CocoaPods1.9.0 安裝使用教程詳解

    CocoaPods是OS X和iOS下的一個(gè)第三類庫管理工具,這篇文章主要介紹了CocoaPods1.9.0 安裝使用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • IOS 指紋識別兩種方式詳解及實(shí)例

    IOS 指紋識別兩種方式詳解及實(shí)例

    這篇文章主要介紹了IOS 指紋識別兩種方式詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • iOS中Block的回調(diào)使用和解析詳解

    iOS中Block的回調(diào)使用和解析詳解

    剛剛進(jìn)入iOS開發(fā)行業(yè),發(fā)現(xiàn)開發(fā)中要用到大量的block回調(diào),由此可見它的重要性。本文主要講的是 Block 回調(diào)的使用,以及 Block 是如何實(shí)現(xiàn)這種神奇的回調(diào)兩部分來講的,下面來一起看看吧。
    2016-09-09
  • iOS中從網(wǎng)絡(luò)獲取數(shù)據(jù)的幾種方法的比較

    iOS中從網(wǎng)絡(luò)獲取數(shù)據(jù)的幾種方法的比較

    IOS中獲取網(wǎng)絡(luò)數(shù)據(jù)一般有三種:1、NSURLCondition(已過時(shí)) 2、NSURLSession 3、三方庫AFNetWorking。下面通過本文給大家比較這三種方法的區(qū)別對比
    2017-11-11
  • iOS動畫實(shí)現(xiàn)雨花與櫻花特效

    iOS動畫實(shí)現(xiàn)雨花與櫻花特效

    小編今天為大家?guī)硪粓鲣冷罏r瀝的夜空之雨和滿天飛舞的櫻花之戀,希望能在炎炎夏日為您帶來一絲清爽的涼意!學(xué)習(xí)iOS動畫的小伙伴們可以參考學(xué)習(xí)。
    2016-08-08
  • ios電子書翻頁效果代碼詳解

    ios電子書翻頁效果代碼詳解

    這篇文章主要介紹了ios電子書翻頁效果代碼實(shí)現(xiàn)過程以及對應(yīng)的代碼講解,有需要的朋友參考下。
    2018-02-02
  • iOS實(shí)現(xiàn)動態(tài)的開屏廣告示例代碼

    iOS實(shí)現(xiàn)動態(tài)的開屏廣告示例代碼

    啟動圖是在iOS開發(fā)過程中必不可少的一個(gè)部分,很多app在啟動圖之后會有一張自定義的開屏廣告圖,但是有的時(shí)候需要讓啟動圖看起來就是一個(gè)廣告,而且還要這個(gè)廣告里面會動,iOS的啟動圖只能是靜態(tài)的,而且固定,為了實(shí)現(xiàn)看起來的動畫效果,只能進(jìn)行偽造了。下面來一起看看
    2016-09-09
  • iOS開發(fā)APP跳轉(zhuǎn)到設(shè)置或系統(tǒng)頁面詳解

    iOS開發(fā)APP跳轉(zhuǎn)到設(shè)置或系統(tǒng)頁面詳解

    這篇文章主要為大家介紹了iOS開發(fā)APP跳轉(zhuǎn)到設(shè)置或系統(tǒng)頁面詳解,<BR>有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • 使用iOS推送時(shí)警告錯(cuò)誤的解決方法

    使用iOS推送時(shí)警告錯(cuò)誤的解決方法

    這篇文章主要為大家詳細(xì)介紹了使用iOS推送時(shí)警告錯(cuò)誤的解決方法,感興趣的小伙伴們可以參考一下
    2016-09-09

最新評論