最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS10通知框架UserNotification理解與應(yīng)用

 更新時間:2016年09月27日 11:09:14   作者:琿少  
在iOS10系統(tǒng)中,通知被整合進了UserNotification框架,除了使通知的處理脫離了UIApplication,通知功能的相關(guān)開發(fā)更加結(jié)構(gòu)化與模塊化外,還新增開放了許多更加靈活的開發(fā)接口,現(xiàn)在,開發(fā)者可以為通知定義UI末班,添加媒體附件,需要的朋友可以參考下

一、引言

        關(guān)于通知,無論與遠程Push還是本地通知,以往的iOS系統(tǒng)暴漏給開發(fā)者的接口都是十分有限的,開發(fā)者只能對標題和內(nèi)容進行簡單的定義,至于UI展示和用戶交互行為相關(guān)的部分,開發(fā)者開發(fā)起來都十分困難。至于本地通知,iOS10之前采用的是UILocationNotification類,遠程通知有蘋果服務(wù)器進行轉(zhuǎn)發(fā),本地通知和遠程通知其回調(diào)的處理都是通過AppDelegate中的幾個回調(diào)方法來完成。iOS10系統(tǒng)中,通知功能的增強是一大優(yōu)化之處,iOS10中將通知功能整合成了一個框架UserNotification,其結(jié)構(gòu)十分類似于iOS8中的UIWebView向WebKit框架整合的思路。并且UserNotification相比之前的通知功能更加強大,主要表現(xiàn)在如下幾點:

1.通知處理代碼可以從AppDelegate中剝離。

2.通知的注冊,設(shè)置,處理更加結(jié)構(gòu)化,更易于模塊化開發(fā)。

3.UserNotification支持自定義通知音效和啟動圖。

4.UserNotification支持向通知內(nèi)容中添加媒體附件,例如音頻,視頻。

5.UserNotification支持開發(fā)者定義多套通知模板。

6.UserNotification支持完全自定義的通知界面。

7.UserNotification支持自定義通知中的用戶交互按鈕。

8.通知的觸發(fā)更加容易管理。

從上面列舉的幾點就可以看出,iOS10中的UsreNotification真的是一個大的改進,溫故而知新,關(guān)于iOS之前版本本地通知和遠程通知的相關(guān)內(nèi)容請查看如下博客:

本地推送:http://m.fzitv.net/article/93602.htm。

遠程推送:http://m.fzitv.net/article/92953.htm

二、UserNotification概覽

        學習一個新的框架或知識模塊時,宏觀上了解其體系,大體上掌握其結(jié)構(gòu)是十分必要的,這更有利于我們對這個框架或模塊的整體把握與理解。UserNotification框架中拆分定義了許多類、枚舉和結(jié)構(gòu)體,其中還定義了許多常量,類與類之間雖然關(guān)系復(fù)雜,但脈絡(luò)十分清晰,把握住主線,層層分析,邊很容易理解和應(yīng)用UserNotification框架。

        下圖中列舉了UserNotification框架中所有核心的類:

如圖中關(guān)系所示,UserNotification框架中的核心類列舉如下:

UNNotificationCenter:通知管理中心,單例,通知的注冊,接收通知后的回調(diào)處理等,是UserNotification框架的核心。

UNNotification:通知對象,其中封裝了通知請求。

UNNotificationSettings:通知相關(guān)設(shè)置。

UNNotificationCategory:通知模板。

UNNotificationAction:用于定義通知模板中的用戶交互行為。

UNNotificationRequest:注冊通知請求,其中定義了通知的內(nèi)容和觸發(fā)方式。

UNNotificationResponse:接收到通知后的回執(zhí)。

UNNotificationContent:通知的具體內(nèi)容。

UNNotificationTrigger:通知的觸發(fā)器,由其子類具體定義。

UNNotificationAttachment:通知附件類,為通知內(nèi)容添加媒體附件。

UNNotificationSound:定義通知音效。

UNPushNotificationTrigger:遠程通知的觸發(fā)器,UNNotificationTrigger子類。

UNTimeInervalNotificationTrigger:計時通知的觸發(fā)器,UNNotificationTrigger子類。

UNCalendarNotificationTrigger:周期通知的觸發(fā)器,UNNotificationTrigger子類。

UNLocationNotificationTrigger:地域通知的觸發(fā)器,UNNotificationTrigger子類。

UNNotificationCenterDelegate:協(xié)議,其中方法用于監(jiān)聽通知狀態(tài)。

三、進行通知用戶權(quán)限申請與創(chuàng)建普通的本地通知

        要在iOS系統(tǒng)中使用通知,必須獲取到用戶權(quán)限,UserNotification框架中申請通知用戶權(quán)限需要通過UNNotificationCenter來完成,示例如下:

//進行用戶權(quán)限的申請
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionCarPlay completionHandler:^(BOOL granted, NSError * _Nullable error) {
  //在block中會傳入布爾值granted,表示用戶是否同意
  if (granted) {
   //如果用戶權(quán)限申請成功,設(shè)置通知中心的代理
   [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  }
}];

申請用戶權(quán)限的方法中需要傳入一個權(quán)限內(nèi)容的參數(shù),其枚舉定義如下:

typedef NS_OPTIONS(NSUInteger, UNAuthorizationOptions) {
 //允許更新app上的通知數(shù)字
 UNAuthorizationOptionBadge = (1 << 0),
 //允許通知聲音
 UNAuthorizationOptionSound = (1 << 1),
 //允許通知彈出警告
 UNAuthorizationOptionAlert = (1 << 2),
 //允許車載設(shè)備接收通知
 UNAuthorizationOptionCarPlay = (1 << 3),
};

獲取到用戶權(quán)限后,使用UserNotification創(chuàng)建普通的通知,示例代碼如下:

 //通知內(nèi)容類
 UNMutableNotificationContent * content = [UNMutableNotificationContent new];
 //設(shè)置通知請求發(fā)送時 app圖標上顯示的數(shù)字
 content.badge = @2;
 //設(shè)置通知的內(nèi)容
 content.body = @"這是iOS10的新通知內(nèi)容:普通的iOS通知";
 //默認的通知提示音
 content.sound = [UNNotificationSound defaultSound];
 //設(shè)置通知的副標題
 content.subtitle = @"這里是副標題";
 //設(shè)置通知的標題
 content.title = @"這里是通知的標題";
 //設(shè)置從通知激活app時的launchImage圖片
 content.launchImageName = @"lun";
 //設(shè)置5S之后執(zhí)行
 UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
 UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefault" content:content trigger:trigger];
 //添加通知請求
 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  
 }];

效果如下面圖示:

             

四、通知音效類UNNotificationSound

        通知可以進行自定義的音效設(shè)置,其中方法如下:

//系統(tǒng)默認的音效
+ (instancetype)defaultSound;
//自定義的音頻音效
/*
注意,音頻文件必須在bundle中或者在Library/Sounds目錄下
*/
+ (instancetype)soundNamed:(NSString *)name __WATCHOS_PROHIBITED;

五、通知觸發(fā)器UNNotificationTrigger

        通知觸發(fā)器可以理解為定義通知的發(fā)送時間,UNNotificationTrigger是觸發(fā)器的基類,具體的觸發(fā)器由它的四個子類實現(xiàn),實際上,開發(fā)者在代碼中可能會用到的觸發(fā)器只有三種,UNPushNotificationTrigger遠程推送觸發(fā)器開發(fā)者不需要創(chuàng)建使用,遠程通知有遠程服務(wù)器觸發(fā),開發(fā)者只需要創(chuàng)建與本地通知有關(guān)的觸發(fā)器進行使用。

1.UNTimeIntervalNotificationTrigger

        UNTimeIntervalNotificationTrigger是計時觸發(fā)器,開發(fā)者可以設(shè)置其在添加通知請求后一定時間發(fā)送。

//創(chuàng)建觸發(fā)器 在timeInterval秒后觸發(fā) 可以設(shè)置是否循環(huán)觸發(fā)
+ (instancetype)triggerWithTimeInterval:(NSTimeInterval)timeInterval repeats:(BOOL)repeats;
//獲取下次觸發(fā)的時間點
- (nullable NSDate *)nextTriggerDate;

2.UNCalendarNotificationTrigger

        UNCalendarNotificationTrigger是日歷觸發(fā)器,開發(fā)者可以設(shè)置其在某個時間點觸發(fā)。

//創(chuàng)建觸發(fā)器 設(shè)置觸發(fā)時間 可以設(shè)置是否循環(huán)觸發(fā)
+ (instancetype)triggerWithDateMatchingComponents:(NSDateComponents *)dateComponents repeats:(BOOL)repeats;
//下一次觸發(fā)的時間點
- (nullable NSDate *)nextTriggerDate;

3.UNLocationNotificationTrigger

        UNLocationNotificationTrigger是地域觸發(fā)器,開發(fā)者可以設(shè)置當用戶進入某一區(qū)域時觸發(fā)。

//地域信息
@property (NS_NONATOMIC_IOSONLY, readonly, copy) CLRegion *region;
//創(chuàng)建觸發(fā)器
+ (instancetype)triggerWithRegion:(CLRegion *)region repeats:(BOOL)repeats __WATCHOS_PROHIBITED;

六、為通知內(nèi)容添加附件

        附件主要指的是媒體附件,例如圖片,音頻和視頻,為通知內(nèi)容添加附件需要使用UNNotificationAttachment類。示例代碼如下:

 //創(chuàng)建圖片附件
 UNNotificationAttachment * attach = [UNNotificationAttachment attachmentWithIdentifier:@"imageAttach" URL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg"]] options:nil error:nil];
 UNMutableNotificationContent * content = [UNMutableNotificationContent new];
 //設(shè)置附件數(shù)組
 content.attachments = @[attach];
 content.badge = @1;
 content.body = @"這是iOS10的新通知內(nèi)容:普通的iOS通知";
 //默認的通知提示音
 content.sound = [UNNotificationSound defaultSound];
 content.subtitle = @"這里是副標題";
 content.title = @"這里是通知的標題";
 //設(shè)置5S之后執(zhí)行
 UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
 UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefaultImage" content:content trigger:trigger];
 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  
 }];

效果如下圖      

需要注意,UNNotificationContent的附件數(shù)組雖然是一個數(shù)組,但是系統(tǒng)的通知模板只能展示其中的第一個附件,設(shè)置多個附件也不會有額外的效果,但是如果開發(fā)者進行通知模板UI的自定義,則此數(shù)組就可以派上用場了。音頻附件界面如下:

需要注意,添加附件的格式和大小都有一定的要求,如下表格所示:

創(chuàng)建通知內(nèi)容附件UNNotificationAttachment實例的方法中有一個options配置字典,這個字典中可以進行配置的鍵值對如下:

//配置附件的類型的鍵 需要設(shè)置為NSString類型的值,如果不設(shè)置 則默認從擴展名中推斷
extern NSString * const UNNotificationAttachmentOptionsTypeHintKey __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
//配置是否隱藏縮略圖的鍵 需要配置為NSNumber 0或者1
extern NSString * const UNNotificationAttachmentOptionsThumbnailHiddenKey __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
//配置使用一個標準的矩形來對縮略圖進行裁剪,需要配置為CGRectCreateDictionaryRepresentation(CGRect)創(chuàng)建的矩形引用
extern NSString * const UNNotificationAttachmentOptionsThumbnailClippingRectKey __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
//使用視頻中的某一幀作為縮略圖 配置為NSNumber時間
extern NSString * const UNNotificationAttachmentOptionsThumbnailTimeKey __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);

七、定義通知模板UNNotificationCategory

        聊天類軟件在iOS系統(tǒng)中,常常采用后臺推送的方式推送新消息,用戶可以在不進入應(yīng)用程序的情況下,直接在左面回復(fù)通知推送過來的信息,這種功能就是通過UNNotificationCategory模板與UNNotificationAction用戶活動來實現(xiàn)的。關(guān)于文本回復(fù)框,UserNotification框架中提供了UNTextInputNotificationAction類,其是UNNotificationAction的子類。示例代碼如下:

 //創(chuàng)建用戶活動
 /*
 options參數(shù)可選如下:
 //需要在解開鎖屏下使用
 UNNotificationActionOptionAuthenticationRequired
 //是否指示有破壞性
 UNNotificationActionOptionDestructive
 //是否允許活動在后臺啟動app
 UNNotificationActionOptionForeground
 //無設(shè)置
 UNNotificationActionOptionNone
 */
 UNTextInputNotificationAction * action = [UNTextInputNotificationAction actionWithIdentifier:@"action" title:@"回復(fù)" options:UNNotificationActionOptionAuthenticationRequired textInputButtonTitle:@"活動" textInputPlaceholder:@"請輸入回復(fù)內(nèi)容"];
 //創(chuàng)建通知模板
 UNNotificationCategory * category = [UNNotificationCategory categoryWithIdentifier:@"myNotificationCategoryText" actions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
 UNMutableNotificationContent * content = [UNMutableNotificationContent new];
 content.badge = @1;
 content.body = @"這是iOS10的新通知內(nèi)容:普通的iOS通知";
 //默認的通知提示音
 content.sound = [UNNotificationSound defaultSound];
 content.subtitle = @"這里是副標題";
 content.title = @"這里是通知的標題";
 //設(shè)置通知內(nèi)容對應(yīng)的模板 需要注意 這里的值要與對應(yīng)模板id一致
 content.categoryIdentifier = @"myNotificationCategoryText";
 //設(shè)置5S之后執(zhí)行
 UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
  [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category, nil]];
 UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefaultText" content:content trigger:trigger];
 
 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  
 }];

需要注意,要使用模板,通知內(nèi)容UNNotificationContent的categoryIdentifier要與UNNotificationCategory的id一致。效果如下:

        也可以為通知模板添加多個自定義的用戶交互按鈕,示例如下:

UNNotificationAction * action = [UNNotificationAction actionWithIdentifier:@"action" title:@"活動標題1" options:UNNotificationActionOptionNone];
 UNNotificationAction * action2 = [UNNotificationAction actionWithIdentifier:@"action" title:@"活動標題2" options:UNNotificationActionOptionNone];
 UNNotificationAction * action3 = [UNNotificationAction actionWithIdentifier:@"action" title:@"活動標題3" options:UNNotificationActionOptionNone];
 UNNotificationAction * action4 = [UNNotificationAction actionWithIdentifier:@"action" title:@"活動標題4" options:UNNotificationActionOptionNone];
  UNNotificationCategory * category = [UNNotificationCategory categoryWithIdentifier:@"myNotificationCategoryBtn" actions:@[action,action2,action3,action4] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
 UNMutableNotificationContent * content = [UNMutableNotificationContent new];
 content.badge = @1;
 content.body = @"這是iOS10的新通知內(nèi)容:普通的iOS通知";
 //默認的通知提示音
 content.sound = [UNNotificationSound defaultSound];
 content.subtitle = @"這里是副標題";
 content.title = @"這里是通知的標題";
 content.categoryIdentifier = @"myNotificationCategoryBtn";
 //設(shè)置5S之后執(zhí)行
 UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
 UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefault" content:content trigger:trigger];
 [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category, nil]];
 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  
 }];

需要注意,系統(tǒng)模板最多支持添加4個用戶交互按鈕,如下圖:

八、自定義通知模板UI

        通過前邊的介紹,我們發(fā)現(xiàn)通過UserNotification框架開發(fā)者已經(jīng)可以完成許多從來很難實現(xiàn)的效果。然而這都不是UserNotification框架最強大的地方,UserNotification框架最強大的地方在于其可以完全自定義通知的UI界面。

        完全自定義通知界面是通過iOS擴展來實現(xiàn)的,首先創(chuàng)建一個新的target,如下圖:

選擇Notification Content,如下:

創(chuàng)建完成后,會發(fā)現(xiàn)工程中多了一個Notification Content的擴展,其中自帶一個storyboard文件和一個NotificationViewController類,開發(fā)者可以在storyboard文件或者直接在Controller類中進行自定義界面的編寫。

    需要注意,NotificationViewController自動遵守了UNNotificationContentExtension協(xié)議,這個協(xié)議專門用來處理自定義通知UI的內(nèi)容展示,其中方法列舉如下:

//接收到通知時會被調(diào)用
/*
開發(fā)者可以從notification對象中拿到附件等內(nèi)容進行UI刷新
*/
- (void)didReceiveNotification:(UNNotification *)notification;
//當用戶點擊了通知中的用戶交互按鈕時會被調(diào)用
/*
response對象中有通知內(nèi)容相關(guān)信息
在回調(diào)block塊completion中,開發(fā)者可以傳入一個UNNotificationContentExtensionResponseOption參數(shù)來告訴系統(tǒng)如何處理這次用戶活動
UNNotificationContentExtensionResponseOption枚舉中可選值如下:
typedef NS_ENUM(NSUInteger, UNNotificationContentExtensionResponseOption) {
  //不關(guān)閉當前通知界面
  UNNotificationContentExtensionResponseOptionDoNotDismiss,
  //關(guān)閉當前通知界面
  UNNotificationContentExtensionResponseOptionDismiss,
  //關(guān)閉當前通知界面并將用戶活動傳遞給宿主app處理
  UNNotificationContentExtensionResponseOptionDismissAndForwardAction,
} __IOS_AVAILABLE(10_0) __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __OSX_UNAVAILABLE;
*/
- (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption option))completion;
/*
這個屬性作為get方法進行實現(xiàn) 這個方法用來返回一個通知界面要顯示的媒體按鈕
typedef NS_ENUM(NSUInteger, UNNotificationContentExtensionMediaPlayPauseButtonType) {
  //不顯示媒體按鈕
  UNNotificationContentExtensionMediaPlayPauseButtonTypeNone,
  //默認的媒體按鈕 當點擊按鈕后 進行播放與暫停的切換 按鈕始終顯示
  UNNotificationContentExtensionMediaPlayPauseButtonTypeDefault,
  //Overlay風格 當點擊按鈕后,媒體播放,按鈕隱藏 點擊媒體后,播放暫停,按鈕顯示。
  UNNotificationContentExtensionMediaPlayPauseButtonTypeOverlay,
} __IOS_AVAILABLE(10_0) __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __OSX_UNAVAILABLE;
*/
@property (nonatomic, readonly, assign) UNNotificationContentExtensionMediaPlayPauseButtonType mediaPlayPauseButtonType;
//返回媒體按鈕的位置
@property (nonatomic, readonly, assign) CGRect mediaPlayPauseButtonFrame;
//返回媒體按鈕的顏色
@property (nonatomic, readonly, copy) UIColor *mediaPlayPauseButtonTintColor;
//點擊播放按鈕的回調(diào)
- (void)mediaPlay;
//點擊暫停按鈕的回調(diào)
- (void)mediaPause;
//媒體開始播放的回調(diào)
- (void)mediaPlayingStarted __IOS_AVAILABLE(10_0) __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __OSX_UNAVAILABLE;
//媒體開始暫停的回調(diào)
- (void)mediaPlayingPaused __IOS_AVAILABLE(10_0) __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __OSX_UNAVAILABLE;

需要注意,自定義的通知界面上雖然可以放按鈕,可以放任何UI控件,但是其不能進行用戶交互,唯一可以進行用戶交互的方式是通過協(xié)議中的媒體按鈕及其回調(diào)方法。

        定義好了通知UI模板,若要進行使用,還需要再Notification Content擴展中的info.plist文件的NSExtension字典的NSExtensionAttributes字典里進行一些配置,正常情況下,開發(fā)者需要進行配置的鍵有3個,分別如下:

UNNotificationExtensionCategory:設(shè)置模板的categoryId,用于與UNNotificationContent對應(yīng)。

UNNotificationExtensionInitialContentSizeRatio:設(shè)置自定義通知界面的高度與寬度的比,寬度為固定寬度,在不同設(shè)備上有差別,開發(fā)者需要根據(jù)寬度計算出高度進行設(shè)置,系統(tǒng)根據(jù)這個比值來計算通知界面的高度。

UNNotificationExtensionDefaultContentHidden:是有隱藏系統(tǒng)默認的通知界面。

配置info.plist文件如下:

用如下的代碼創(chuàng)建通知:

UNNotificationAction * action = [UNNotificationAction actionWithIdentifier:@"action" title:@"活動標題1" options:UNNotificationActionOptionNone];
  //根據(jù)id拿到自定義UI的模板
  UNNotificationCategory * category = [UNNotificationCategory categoryWithIdentifier:@"myNotificationCategoryH" actions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  UNMutableNotificationContent * content = [UNMutableNotificationContent new];
  content.badge = @1;
  content.body = @"這是iOS10的新通知內(nèi)容:普通的iOS通知";
  //默認的通知提示音
  content.sound = [UNNotificationSound defaultSound];
  content.subtitle = @"這里是副標題";
  content.title = @"這里是通知的標題";
  content.categoryIdentifier = @"myNotificationCategoryH";
  //設(shè)置5S之后執(zhí)行
  UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
  UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefaultCustomUIH" content:content trigger:trigger];
  [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category, nil]];
  [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    
  }];

效果如下圖:

如果將UNNotificationExtensionDefaultContentHidden鍵值設(shè)置為0或者不設(shè)置,則不會隱藏系統(tǒng)默認的UI,如下:

九、通知回調(diào)的處理

        UserNotification框架對于通知的回調(diào)處理,是通過UNUserNotificationCenterDelegate協(xié)議來實現(xiàn)的,這個協(xié)議中有兩個方法,如下:

/*
這個方法在應(yīng)用在前臺,并且將要彈出通知時被調(diào)用,后臺狀態(tài)下彈通知不會調(diào)用這個方法
這個方法中的block塊completionHandler()可以傳入一個UNNotificationPresentationOptions類型的枚舉
有個這個參數(shù),開發(fā)者可以設(shè)置在前臺狀態(tài)下,依然可以彈出通知消息,枚舉如下:
typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
  //只修改app圖標的消息數(shù)
  UNNotificationPresentationOptionBadge  = (1 << 0),
  //只提示通知音效
  UNNotificationPresentationOptionSound  = (1 << 1),
  //只彈出通知框
  UNNotificationPresentationOptionAlert  = (1 << 2),
} __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
//什么都不做
static const UNNotificationPresentationOptions UNNotificationPresentationOptionNone 
*/
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
/*
這個方法當接收到通知后,用戶點擊通知激活app時被調(diào)用,無論前臺還是后臺
*/
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_PROHIBITED;

十、UserNotification框架中其他零散知識

        前面所介紹的內(nèi)容基本涵蓋了UserNotification框架中所有的內(nèi)容,在以后的應(yīng)用開發(fā)中,開發(fā)者可以在通知方面發(fā)揮更大的想象力與創(chuàng)造力,給用戶更加友好的體驗。除了前邊所介紹過的核心內(nèi)容外,UserNotification框架中還有一些零散的類、枚舉等。

1.錯誤碼描述

typedef NS_ENUM(NSInteger, UNErrorCode) {
  //通知不被允許
  UNErrorCodeNotificationsNotAllowed = 1,
  
  //附件無效url
  UNErrorCodeAttachmentInvalidURL = 100,
  //附件類型錯誤
  UNErrorCodeAttachmentUnrecognizedType,
  //附件大小錯誤
  UNErrorCodeAttachmentInvalidFileSize,
  //附件數(shù)據(jù)錯誤
  UNErrorCodeAttachmentNotInDataStore,
  UNErrorCodeAttachmentMoveIntoDataStoreFailed,
  UNErrorCodeAttachmentCorrupt,
  
  //時間無效
  UNErrorCodeNotificationInvalidNoDate = 1400,
  //無內(nèi)容
  UNErrorCodeNotificationInvalidNoContent,
} __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);

2.UNNotification類

@interface UNNotification : NSObject <NSCopying, NSSecureCoding>
//觸發(fā)的時間
@property (nonatomic, readonly, copy) NSDate *date;
//內(nèi)置的通知請求對象
@property (nonatomic, readonly, copy) UNNotificationRequest *request;

- (instancetype)init NS_UNAVAILABLE;

@end

3.UNNotificationSettings類

        UNNotificationSettings類主要用來獲取與通知相關(guān)的信息。

@interface UNNotificationSettings : NSObject <NSCopying, NSSecureCoding>
//用戶權(quán)限狀態(tài)
@property (NS_NONATOMIC_IOSONLY, readonly) UNAuthorizationStatus authorizationStatus;
//音效設(shè)置
@property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting soundSetting __TVOS_PROHIBITED;
//圖標提醒設(shè)置
@property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting badgeSetting __WATCHOS_PROHIBITED;
//提醒框設(shè)置
@property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting alertSetting __TVOS_PROHIBITED;
//通知中心設(shè)置
@property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting notificationCenterSetting __TVOS_PROHIBITED;
//鎖屏設(shè)置
@property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting lockScreenSetting __TVOS_PROHIBITED __WATCHOS_PROHIBITED;
//車載設(shè)備設(shè)置
@property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting carPlaySetting __TVOS_PROHIBITED __WATCHOS_PROHIBITED;
//提醒框風格
@property (NS_NONATOMIC_IOSONLY, readonly) UNAlertStyle alertStyle __TVOS_PROHIBITED __WATCHOS_PROHIBITED;

@end

UNNotificationSetting枚舉如下:

typedef NS_ENUM(NSInteger, UNNotificationSetting) {
  //不支持
  UNNotificationSettingNotSupported = 0,
  
  //不可用
  UNNotificationSettingDisabled,
  
  //可用
  UNNotificationSettingEnabled,
} 

UNAuthorizationStatus枚舉如下:

typedef NS_ENUM(NSInteger, UNAuthorizationStatus) {
  //為做選擇
  UNAuthorizationStatusNotDetermined = 0,
  
  // 用戶拒絕
  UNAuthorizationStatusDenied,
  
  // 用戶允許
  UNAuthorizationStatusAuthorized
}

UNAlertStyle枚舉如下:

typedef NS_ENUM(NSInteger, UNAlertStyle) {
  //無
  UNAlertStyleNone = 0,
  //頂部Banner樣式
  UNAlertStyleBanner,
  //警告框樣式
  UNAlertStyleAlert,
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS中利用UIBezierPath + CAAnimation實現(xiàn)心跳動畫效果

    iOS中利用UIBezierPath + CAAnimation實現(xiàn)心跳動畫效果

    這篇文章主要給大家介紹了關(guān)于iOS中利用UIBezierPath + CAAnimation實現(xiàn)心跳動畫效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的日常開發(fā)具有一定的參考學習,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-10-10
  • IOS 通訊錄信息讀取兼容的實現(xiàn)方法

    IOS 通訊錄信息讀取兼容的實現(xiàn)方法

    這篇文章主要介紹了IOS 通訊錄信息讀取兼容的實現(xiàn)方法的相關(guān)資料,這里提供實現(xiàn)方法幫助大家實現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-08-08
  • iOS如何定義名為任意的變量詳解

    iOS如何定義名為任意的變量詳解

    這篇文章主要給大家介紹了關(guān)于iOS如何定義名為任意的變量的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位iOS開發(fā)者們具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2018-05-05
  • iOS支付寶使用方法詳解

    iOS支付寶使用方法詳解

    這篇文章主要為大家詳細介紹了iOS支付寶的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • iOS 圖片旋轉(zhuǎn)方法實例代碼

    iOS 圖片旋轉(zhuǎn)方法實例代碼

    這篇文章主要介紹了iOS 圖片旋轉(zhuǎn)方法實例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-03-03
  • iOS圖片拉伸的4種方法

    iOS圖片拉伸的4種方法

    這篇文章主要為大家詳細介紹了iOS圖片拉伸的4種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • iOS實現(xiàn)聯(lián)系人列表功能

    iOS實現(xiàn)聯(lián)系人列表功能

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)聯(lián)系人列表功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • unix 編程進程控制詳細介紹

    unix 編程進程控制詳細介紹

    這篇文章主要介紹了unix 編程進程控制詳細介紹的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • iPhoneX 各種適配記錄筆記(超全面)

    iPhoneX 各種適配記錄筆記(超全面)

    iPhone X出來之后,關(guān)于劉海的各種適配成了程序員們首要考慮的問題,下面這篇文章主要給大家介紹了關(guān)于iPhoneX 各種適配的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-12-12
  • iOS應(yīng)用開發(fā)中運用設(shè)計模式中的組合模式的實例解析

    iOS應(yīng)用開發(fā)中運用設(shè)計模式中的組合模式的實例解析

    這篇文章主要介紹了iOS應(yīng)用開發(fā)中運用設(shè)計模式中的組合模式的實例解析,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2016-03-03

最新評論

汶川县| 灵丘县| 绥棱县| 南阳市| 大埔区| 富源县| 英德市| 余姚市| 丰宁| 镇安县| 桓台县| 吉隆县| 临海市| 武安市| 浪卡子县| 古田县| 哈巴河县| 金沙县| 井陉县| 普定县| 墨玉县| 旬阳县| 牟定县| 沛县| 淳化县| 重庆市| 绥滨县| 崇仁县| 定襄县| 三穗县| 柘荣县| 乌拉特前旗| 轮台县| 麻栗坡县| 新竹县| 三台县| 大庆市| 江川县| 紫金县| 安阳市| 贵溪市|