文章分享至个人我的技术博客: https://cainluo.github.io/14973700585982.htmlhtml
Notification
在iOS
以来发展以来都是一个很是重要的功能之一, 各种App
都须要经过通知来推送消息, 否则怎么告诉用户, App
来消息推送啦, 或者有什么安排事项到点要告诉你了, 这些都是须要推送的.git
但咱们都知道, 在以前的消息推送里, 不管是UI
仍是内容, 都是很是的死, 不够人性化, 好在苹果爸爸也看到了这一点, 在iOS 10
就改善了这个东西.github
一样我这里也是Objective-C
版本, 喜欢看Swift
版本的能够到这里去看.微信
那么在iOS 10
苹果爸爸推出了UNUserNotification
这套框架, 除去了之前那套硬邦邦的通知以外, 咱们能够额外给通知添加一些Action Button
, 或者是一些不同的内容, 说那么多废话, 直接来看项目吧.app
新工程和咱们之前建立的一毛同样, 这里就不过多的演示了, 文章后面有项目地址, 你们能够自行去看.框架
咱们来看看AppDelegate.m
文件:ide
这里要本身写代码的地方我都标上去了, 若是仍是看不懂, 那我就真的没办法了...布局
再来看看NotificationController.m
文件, 这里咱们直接忽略掉UI
的布局, 由于这不是重点:spa
因为这里没有仔细的去封装代码, 因此看起来有些臃肿, 这个你们就不要喷了....3d
对了, 我想起一个坑, 在我导入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