iOS10通知框架UserNotification理解與應(yīng)用
一、引言
關(guān)于通知,無論與遠(yuǎn)程Push還是本地通知,以往的iOS系統(tǒng)暴漏給開發(fā)者的接口都是十分有限的,開發(fā)者只能對(duì)標(biāo)題和內(nèi)容進(jìn)行簡單的定義,至于UI展示和用戶交互行為相關(guān)的部分,開發(fā)者開發(fā)起來都十分困難。至于本地通知,iOS10之前采用的是UILocationNotification類,遠(yuǎn)程通知有蘋果服務(wù)器進(jìn)行轉(zhuǎn)發(fā),本地通知和遠(yuǎn)程通知其回調(diào)的處理都是通過AppDelegate中的幾個(gè)回調(diào)方法來完成。iOS10系統(tǒng)中,通知功能的增強(qiáng)是一大優(yōu)化之處,iOS10中將通知功能整合成了一個(gè)框架UserNotification,其結(jié)構(gòu)十分類似于iOS8中的UIWebView向WebKit框架整合的思路。并且UserNotification相比之前的通知功能更加強(qiáng)大,主要表現(xiàn)在如下幾點(diǎn):
1.通知處理代碼可以從AppDelegate中剝離。
2.通知的注冊(cè),設(shè)置,處理更加結(jié)構(gòu)化,更易于模塊化開發(fā)。
3.UserNotification支持自定義通知音效和啟動(dòng)圖。
4.UserNotification支持向通知內(nèi)容中添加媒體附件,例如音頻,視頻。
5.UserNotification支持開發(fā)者定義多套通知模板。
6.UserNotification支持完全自定義的通知界面。
7.UserNotification支持自定義通知中的用戶交互按鈕。
8.通知的觸發(fā)更加容易管理。
從上面列舉的幾點(diǎn)就可以看出,iOS10中的UsreNotification真的是一個(gè)大的改進(jìn),溫故而知新,關(guān)于iOS之前版本本地通知和遠(yuǎn)程通知的相關(guān)內(nèi)容請(qǐng)查看如下博客:
本地推送:http://www.dbjr.com.cn/article/93602.htm。
遠(yuǎn)程推送:http://www.dbjr.com.cn/article/92953.htm。
二、UserNotification概覽
學(xué)習(xí)一個(gè)新的框架或知識(shí)模塊時(shí),宏觀上了解其體系,大體上掌握其結(jié)構(gòu)是十分必要的,這更有利于我們對(duì)這個(gè)框架或模塊的整體把握與理解。UserNotification框架中拆分定義了許多類、枚舉和結(jié)構(gòu)體,其中還定義了許多常量,類與類之間雖然關(guān)系復(fù)雜,但脈絡(luò)十分清晰,把握住主線,層層分析,邊很容易理解和應(yīng)用UserNotification框架。
下圖中列舉了UserNotification框架中所有核心的類:
如圖中關(guān)系所示,UserNotification框架中的核心類列舉如下:
UNNotificationCenter:通知管理中心,單例,通知的注冊(cè),接收通知后的回調(diào)處理等,是UserNotification框架的核心。
UNNotification:通知對(duì)象,其中封裝了通知請(qǐng)求。
UNNotificationSettings:通知相關(guān)設(shè)置。
UNNotificationCategory:通知模板。
UNNotificationAction:用于定義通知模板中的用戶交互行為。
UNNotificationRequest:注冊(cè)通知請(qǐng)求,其中定義了通知的內(nèi)容和觸發(fā)方式。
UNNotificationResponse:接收到通知后的回執(zhí)。
UNNotificationContent:通知的具體內(nèi)容。
UNNotificationTrigger:通知的觸發(fā)器,由其子類具體定義。
UNNotificationAttachment:通知附件類,為通知內(nèi)容添加媒體附件。
UNNotificationSound:定義通知音效。
UNPushNotificationTrigger:遠(yuǎn)程通知的觸發(fā)器,UNNotificationTrigger子類。
UNTimeInervalNotificationTrigger:計(jì)時(shí)通知的觸發(fā)器,UNNotificationTrigger子類。
UNCalendarNotificationTrigger:周期通知的觸發(fā)器,UNNotificationTrigger子類。
UNLocationNotificationTrigger:地域通知的觸發(fā)器,UNNotificationTrigger子類。
UNNotificationCenterDelegate:協(xié)議,其中方法用于監(jiān)聽通知狀態(tài)。
三、進(jìn)行通知用戶權(quán)限申請(qǐng)與創(chuàng)建普通的本地通知
要在iOS系統(tǒng)中使用通知,必須獲取到用戶權(quán)限,UserNotification框架中申請(qǐng)通知用戶權(quán)限需要通過UNNotificationCenter來完成,示例如下:
//進(jìn)行用戶權(quán)限的申請(qǐng) [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionCarPlay completionHandler:^(BOOL granted, NSError * _Nullable error) { //在block中會(huì)傳入布爾值granted,表示用戶是否同意 if (granted) { //如果用戶權(quán)限申請(qǐng)成功,設(shè)置通知中心的代理 [UNUserNotificationCenter currentNotificationCenter].delegate = self; } }];
申請(qǐng)用戶權(quán)限的方法中需要傳入一個(gè)權(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è)置通知請(qǐng)求發(fā)送時(shí) app圖標(biāo)上顯示的數(shù)字 content.badge = @2; //設(shè)置通知的內(nèi)容 content.body = @"這是iOS10的新通知內(nèi)容:普通的iOS通知"; //默認(rèn)的通知提示音 content.sound = [UNNotificationSound defaultSound]; //設(shè)置通知的副標(biāo)題 content.subtitle = @"這里是副標(biāo)題"; //設(shè)置通知的標(biāo)題 content.title = @"這里是通知的標(biāo)題"; //設(shè)置從通知激活app時(shí)的launchImage圖片 content.launchImageName = @"lun"; //設(shè)置5S之后執(zhí)行 UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefault" content:content trigger:trigger]; //添加通知請(qǐng)求 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { }];
效果如下面圖示:
四、通知音效類UNNotificationSound
通知可以進(jìn)行自定義的音效設(shè)置,其中方法如下:
//系統(tǒng)默認(rèn)的音效 + (instancetype)defaultSound; //自定義的音頻音效 /* 注意,音頻文件必須在bundle中或者在Library/Sounds目錄下 */ + (instancetype)soundNamed:(NSString *)name __WATCHOS_PROHIBITED;
五、通知觸發(fā)器UNNotificationTrigger
通知觸發(fā)器可以理解為定義通知的發(fā)送時(shí)間,UNNotificationTrigger是觸發(fā)器的基類,具體的觸發(fā)器由它的四個(gè)子類實(shí)現(xiàn),實(shí)際上,開發(fā)者在代碼中可能會(huì)用到的觸發(fā)器只有三種,UNPushNotificationTrigger遠(yuǎn)程推送觸發(fā)器開發(fā)者不需要?jiǎng)?chuàng)建使用,遠(yuǎn)程通知有遠(yuǎn)程服務(wù)器觸發(fā),開發(fā)者只需要?jiǎng)?chuàng)建與本地通知有關(guān)的觸發(fā)器進(jìn)行使用。
1.UNTimeIntervalNotificationTrigger
UNTimeIntervalNotificationTrigger是計(jì)時(shí)觸發(fā)器,開發(fā)者可以設(shè)置其在添加通知請(qǐng)求后一定時(shí)間發(fā)送。
//創(chuàng)建觸發(fā)器 在timeInterval秒后觸發(fā) 可以設(shè)置是否循環(huán)觸發(fā) + (instancetype)triggerWithTimeInterval:(NSTimeInterval)timeInterval repeats:(BOOL)repeats; //獲取下次觸發(fā)的時(shí)間點(diǎn) - (nullable NSDate *)nextTriggerDate;
2.UNCalendarNotificationTrigger
UNCalendarNotificationTrigger是日歷觸發(fā)器,開發(fā)者可以設(shè)置其在某個(gè)時(shí)間點(diǎn)觸發(fā)。
//創(chuàng)建觸發(fā)器 設(shè)置觸發(fā)時(shí)間 可以設(shè)置是否循環(huán)觸發(fā) + (instancetype)triggerWithDateMatchingComponents:(NSDateComponents *)dateComponents repeats:(BOOL)repeats; //下一次觸發(fā)的時(shí)間點(diǎn) - (nullable NSDate *)nextTriggerDate;
3.UNLocationNotificationTrigger
UNLocationNotificationTrigger是地域觸發(fā)器,開發(fā)者可以設(shè)置當(dāng)用戶進(jìn)入某一區(qū)域時(shí)觸發(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通知"; //默認(rèn)的通知提示音 content.sound = [UNNotificationSound defaultSound]; content.subtitle = @"這里是副標(biāo)題"; content.title = @"這里是通知的標(biāo)題"; //設(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ù)組雖然是一個(gè)數(shù)組,但是系統(tǒng)的通知模板只能展示其中的第一個(gè)附件,設(shè)置多個(gè)附件也不會(huì)有額外的效果,但是如果開發(fā)者進(jìn)行通知模板UI的自定義,則此數(shù)組就可以派上用場了。音頻附件界面如下:
需要注意,添加附件的格式和大小都有一定的要求,如下表格所示:
創(chuàng)建通知內(nèi)容附件UNNotificationAttachment實(shí)例的方法中有一個(gè)options配置字典,這個(gè)字典中可以進(jìn)行配置的鍵值對(duì)如下:
//配置附件的類型的鍵 需要設(shè)置為NSString類型的值,如果不設(shè)置 則默認(rèn)從擴(kuò)展名中推斷 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); //配置使用一個(gè)標(biāo)準(zhǔn)的矩形來對(duì)縮略圖進(jìn)行裁剪,需要配置為CGRectCreateDictionaryRepresentation(CGRect)創(chuàng)建的矩形引用 extern NSString * const UNNotificationAttachmentOptionsThumbnailClippingRectKey __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0); //使用視頻中的某一幀作為縮略圖 配置為NSNumber時(shí)間 extern NSString * const UNNotificationAttachmentOptionsThumbnailTimeKey __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
七、定義通知模板UNNotificationCategory
聊天類軟件在iOS系統(tǒng)中,常常采用后臺(tái)推送的方式推送新消息,用戶可以在不進(jìn)入應(yīng)用程序的情況下,直接在左面回復(fù)通知推送過來的信息,這種功能就是通過UNNotificationCategory模板與UNNotificationAction用戶活動(dòng)來實(shí)現(xiàn)的。關(guān)于文本回復(fù)框,UserNotification框架中提供了UNTextInputNotificationAction類,其是UNNotificationAction的子類。示例代碼如下:
//創(chuàng)建用戶活動(dòng) /* options參數(shù)可選如下: //需要在解開鎖屏下使用 UNNotificationActionOptionAuthenticationRequired //是否指示有破壞性 UNNotificationActionOptionDestructive //是否允許活動(dòng)在后臺(tái)啟動(dòng)app UNNotificationActionOptionForeground //無設(shè)置 UNNotificationActionOptionNone */ UNTextInputNotificationAction * action = [UNTextInputNotificationAction actionWithIdentifier:@"action" title:@"回復(fù)" options:UNNotificationActionOptionAuthenticationRequired textInputButtonTitle:@"活動(dòng)" textInputPlaceholder:@"請(qǐng)輸入回復(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通知"; //默認(rèn)的通知提示音 content.sound = [UNNotificationSound defaultSound]; content.subtitle = @"這里是副標(biāo)題"; content.title = @"這里是通知的標(biāo)題"; //設(shè)置通知內(nèi)容對(duì)應(yīng)的模板 需要注意 這里的值要與對(duì)應(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一致。效果如下:
也可以為通知模板添加多個(gè)自定義的用戶交互按鈕,示例如下:
UNNotificationAction * action = [UNNotificationAction actionWithIdentifier:@"action" title:@"活動(dòng)標(biāo)題1" options:UNNotificationActionOptionNone]; UNNotificationAction * action2 = [UNNotificationAction actionWithIdentifier:@"action" title:@"活動(dòng)標(biāo)題2" options:UNNotificationActionOptionNone]; UNNotificationAction * action3 = [UNNotificationAction actionWithIdentifier:@"action" title:@"活動(dòng)標(biāo)題3" options:UNNotificationActionOptionNone]; UNNotificationAction * action4 = [UNNotificationAction actionWithIdentifier:@"action" title:@"活動(dòng)標(biāo)題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通知"; //默認(rèn)的通知提示音 content.sound = [UNNotificationSound defaultSound]; content.subtitle = @"這里是副標(biāo)題"; content.title = @"這里是通知的標(biāo)題"; 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個(gè)用戶交互按鈕,如下圖:
八、自定義通知模板UI
通過前邊的介紹,我們發(fā)現(xiàn)通過UserNotification框架開發(fā)者已經(jīng)可以完成許多從來很難實(shí)現(xiàn)的效果。然而這都不是UserNotification框架最強(qiáng)大的地方,UserNotification框架最強(qiáng)大的地方在于其可以完全自定義通知的UI界面。
完全自定義通知界面是通過iOS擴(kuò)展來實(shí)現(xiàn)的,首先創(chuàng)建一個(gè)新的target,如下圖:
選擇Notification Content,如下:
創(chuàng)建完成后,會(huì)發(fā)現(xiàn)工程中多了一個(gè)Notification Content的擴(kuò)展,其中自帶一個(gè)storyboard文件和一個(gè)NotificationViewController類,開發(fā)者可以在storyboard文件或者直接在Controller類中進(jìn)行自定義界面的編寫。
需要注意,NotificationViewController自動(dòng)遵守了UNNotificationContentExtension協(xié)議,這個(gè)協(xié)議專門用來處理自定義通知UI的內(nèi)容展示,其中方法列舉如下:
//接收到通知時(shí)會(huì)被調(diào)用 /* 開發(fā)者可以從notification對(duì)象中拿到附件等內(nèi)容進(jìn)行UI刷新 */ - (void)didReceiveNotification:(UNNotification *)notification; //當(dāng)用戶點(diǎn)擊了通知中的用戶交互按鈕時(shí)會(huì)被調(diào)用 /* response對(duì)象中有通知內(nèi)容相關(guān)信息 在回調(diào)block塊completion中,開發(fā)者可以傳入一個(gè)UNNotificationContentExtensionResponseOption參數(shù)來告訴系統(tǒng)如何處理這次用戶活動(dòng) UNNotificationContentExtensionResponseOption枚舉中可選值如下: typedef NS_ENUM(NSUInteger, UNNotificationContentExtensionResponseOption) { //不關(guān)閉當(dāng)前通知界面 UNNotificationContentExtensionResponseOptionDoNotDismiss, //關(guān)閉當(dāng)前通知界面 UNNotificationContentExtensionResponseOptionDismiss, //關(guān)閉當(dāng)前通知界面并將用戶活動(dòng)傳遞給宿主app處理 UNNotificationContentExtensionResponseOptionDismissAndForwardAction, } __IOS_AVAILABLE(10_0) __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __OSX_UNAVAILABLE; */ - (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption option))completion; /* 這個(gè)屬性作為get方法進(jìn)行實(shí)現(xiàn) 這個(gè)方法用來返回一個(gè)通知界面要顯示的媒體按鈕 typedef NS_ENUM(NSUInteger, UNNotificationContentExtensionMediaPlayPauseButtonType) { //不顯示媒體按鈕 UNNotificationContentExtensionMediaPlayPauseButtonTypeNone, //默認(rèn)的媒體按鈕 當(dāng)點(diǎn)擊按鈕后 進(jìn)行播放與暫停的切換 按鈕始終顯示 UNNotificationContentExtensionMediaPlayPauseButtonTypeDefault, //Overlay風(fēng)格 當(dāng)點(diǎn)擊按鈕后,媒體播放,按鈕隱藏 點(diǎn)擊媒體后,播放暫停,按鈕顯示。 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ǎn)擊播放按鈕的回調(diào) - (void)mediaPlay; //點(diǎn)擊暫停按鈕的回調(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控件,但是其不能進(jìn)行用戶交互,唯一可以進(jìn)行用戶交互的方式是通過協(xié)議中的媒體按鈕及其回調(diào)方法。
定義好了通知UI模板,若要進(jìn)行使用,還需要再Notification Content擴(kuò)展中的info.plist文件的NSExtension字典的NSExtensionAttributes字典里進(jìn)行一些配置,正常情況下,開發(fā)者需要進(jìn)行配置的鍵有3個(gè),分別如下:
UNNotificationExtensionCategory:設(shè)置模板的categoryId,用于與UNNotificationContent對(duì)應(yīng)。
UNNotificationExtensionInitialContentSizeRatio:設(shè)置自定義通知界面的高度與寬度的比,寬度為固定寬度,在不同設(shè)備上有差別,開發(fā)者需要根據(jù)寬度計(jì)算出高度進(jìn)行設(shè)置,系統(tǒng)根據(jù)這個(gè)比值來計(jì)算通知界面的高度。
UNNotificationExtensionDefaultContentHidden:是有隱藏系統(tǒng)默認(rèn)的通知界面。
配置info.plist文件如下:
用如下的代碼創(chuàng)建通知:
UNNotificationAction * action = [UNNotificationAction actionWithIdentifier:@"action" title:@"活動(dòng)標(biāo)題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通知"; //默認(rèn)的通知提示音 content.sound = [UNNotificationSound defaultSound]; content.subtitle = @"這里是副標(biāo)題"; content.title = @"這里是通知的標(biāo)題"; 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è)置,則不會(huì)隱藏系統(tǒng)默認(rèn)的UI,如下:
九、通知回調(diào)的處理
UserNotification框架對(duì)于通知的回調(diào)處理,是通過UNUserNotificationCenterDelegate協(xié)議來實(shí)現(xiàn)的,這個(gè)協(xié)議中有兩個(gè)方法,如下:
/* 這個(gè)方法在應(yīng)用在前臺(tái),并且將要彈出通知時(shí)被調(diào)用,后臺(tái)狀態(tài)下彈通知不會(huì)調(diào)用這個(gè)方法 這個(gè)方法中的block塊completionHandler()可以傳入一個(gè)UNNotificationPresentationOptions類型的枚舉 有個(gè)這個(gè)參數(shù),開發(fā)者可以設(shè)置在前臺(tái)狀態(tài)下,依然可以彈出通知消息,枚舉如下: typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) { //只修改app圖標(biāo)的消息數(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); /* 這個(gè)方法當(dāng)接收到通知后,用戶點(diǎn)擊通知激活app時(shí)被調(diào)用,無論前臺(tái)還是后臺(tái) */ - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_PROHIBITED;
十、UserNotification框架中其他零散知識(shí)
前面所介紹的內(nèi)容基本涵蓋了UserNotification框架中所有的內(nèi)容,在以后的應(yīng)用開發(fā)中,開發(fā)者可以在通知方面發(fā)揮更大的想象力與創(chuàng)造力,給用戶更加友好的體驗(yàn)。除了前邊所介紹過的核心內(nèi)容外,UserNotification框架中還有一些零散的類、枚舉等。
1.錯(cuò)誤碼描述
typedef NS_ENUM(NSInteger, UNErrorCode) { //通知不被允許 UNErrorCodeNotificationsNotAllowed = 1, //附件無效url UNErrorCodeAttachmentInvalidURL = 100, //附件類型錯(cuò)誤 UNErrorCodeAttachmentUnrecognizedType, //附件大小錯(cuò)誤 UNErrorCodeAttachmentInvalidFileSize, //附件數(shù)據(jù)錯(cuò)誤 UNErrorCodeAttachmentNotInDataStore, UNErrorCodeAttachmentMoveIntoDataStoreFailed, UNErrorCodeAttachmentCorrupt, //時(shí)間無效 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ā)的時(shí)間 @property (nonatomic, readonly, copy) NSDate *date; //內(nèi)置的通知請(qǐng)求對(duì)象 @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; //圖標(biāo)提醒設(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; //提醒框風(fēng)格 @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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
簡單講解Objective-C的基本特性及其內(nèi)存管理方式
這篇文章主要介紹了簡單講解Objective-C的基本特性及其內(nèi)存管理方式,雖然Swift語言出現(xiàn)后iOS和Mac OS應(yīng)用開發(fā)方面Objective-C正在成為過去時(shí),但現(xiàn)有諸多項(xiàng)目仍然在使用,需要的朋友可以參考下2016-01-01IOS實(shí)現(xiàn)點(diǎn)擊滑動(dòng)抽屜效果
這篇文章主要為大家詳細(xì)介紹了IOS實(shí)現(xiàn)點(diǎn)擊滑動(dòng)抽屜效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02iOS開發(fā)之清除緩存功能的實(shí)現(xiàn)
現(xiàn)在的絕大多數(shù)應(yīng)用中都存在著清楚緩存的功能,形形色色,各有千秋,所以小編現(xiàn)為大家介紹一種最基礎(chǔ)的清除緩存的方法,有需要的可以參考借鑒。下面來一起看看吧。2016-09-09IOS 出現(xiàn)問題POST網(wǎng)絡(luò)請(qǐng)求狀態(tài)code:500的解決方法
這篇文章主要介紹了IOS 出現(xiàn)問題POST網(wǎng)絡(luò)請(qǐng)求狀態(tài)code:500的解決方法的相關(guān)資料,需要的朋友可以參考下2017-02-02理解iOS多線程應(yīng)用的開發(fā)以及線程的創(chuàng)建方法
這篇文章主要介紹了理解iOS多線程應(yīng)用的開發(fā)以及線程的創(chuàng)建方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11IOS身份證識(shí)別(OCR源碼)詳解及實(shí)例代碼
這篇文章主要介紹了IOS身份證識(shí)別(OCR源碼)詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03