文章分享至我的個人技術部落格: https://cainluo.github.io/14973700585982.html
UserNotifications
Notification
在iOS
以來發展以來都是一個非常重要的功能之一, 各類App
都需要通過通知來推送訊息, 不然怎麼告訴使用者, App
來訊息推送啦, 或者有什麼安排事項到點要告訴你了, 這些都是需要推送的.
但我們都知道, 在之前的訊息推送裡, 無論是UI
還是內容, 都是非常的死, 不夠人性化, 好在蘋果爸爸也看到了這一點, 在iOS 10
就改善了這個東西.
同樣我這裡也是Objective-C
版本, 喜歡看Swift
版本的可以到這裡去看.
UNUserNotification Of iOS 10
那麼在iOS 10
蘋果爸爸推出了UNUserNotification
這套框架, 除去了以前那套硬邦邦的通知之外, 我們可以額外給通知新增一些Action Button
, 或者是一些不一樣的內容, 說那麼多廢話, 直接來看專案吧.
建立專案
新工程和我們以前建立的一毛一樣, 這裡就不過多的演示了, 文章後面有專案地址, 大家可以自行去看.
我們來看看AppDelegate.m
檔案:


這裡要自己寫程式碼的地方我都標上去了, 如果還是看不懂, 那我就真的沒辦法了...
再來看看NotificationController.m
檔案, 這裡我們直接忽略掉UI
的佈局, 因為這不是重點:




由於這裡沒有仔細的去封裝程式碼, 所以看起來有些臃腫, 這個大家就不要噴了....
對了, 我想起一個坑, 在我匯入MP4
檔案的時候, 我發現檔案的Path
居然為nil
, 於是乎Google
了一下, 發現是我傻缺了, 忘記在這裡點一下:

關於怎麼新增一些Action Button
, 專案裡面沒有弄, 這裡給大家貼句程式碼吧:
UNTextInputNotificationAction *inputAction = [UNTextInputNotificationAction
actionWithIdentifier:@"inputAction"
title:@"輸入"
options:UNNotificationActionOptionForeground
textInputButtonTitle:@"傳送"
textInputPlaceholder:@"隨便說說"];
UNNotificationAction *goodbyeAction = [UNNotificationAction actionWithIdentifier:@"goodbyeAction"
title:@"再見"
options:UNNotificationActionOptionForeground];
UNNotificationAction *cancelAction = [UNNotificationAction actionWithIdentifier:@"cancelAction"
title:@"取消"
options:UNNotificationActionOptionDestructive];
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"inputCategory" actions:@[inputAction,goodbyeAction,cancelAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:@[category]];
複製程式碼
一切就緒
一切就緒之後, 我們就執行來看看:

這是一個正常通知:

這是新增了Image
的通知:

這是新增了一個Video
的通知, 重點是還可以播放:

注意!!!
雖然說蘋果爸爸在UNUserNotification
有了些花樣, 但這裡還是需要注意一點, 這是有限制的, 不然這也太不蘋果了, 那麼是什麼限制呢? 可以點進去這裡看看UNUserNotification型別檔案限制, 一旦超出了, 出現什麼問題, 我就母雞了.
再搜這篇文章的時候, 突然發現了有大神總結了WWDC2016筆記, 大家有需要可以去看看~~
工程地址
專案地址: https://github.com/CainRun/iOS-10-Characteristic/tree/master/5.UserNotifications
最後

