iOS10通知框架UserNotification理解与应用

2020-01-18 16:20:19于海丽

选择Notification Content,如下:

iOS10通知框架,iOS10通知框架UserNotification,iOS10远程推送,iOS10本地推送

创建完成后,会发现工程中多了一个Notification Content的扩展,其中自带一个storyboard文件和一个NotificationViewController类,开发者可以在storyboard文件或者直接在Controller类中进行自定义界面的编写。

    需要注意,NotificationViewController自动遵守了UNNotificationContentExtension协议,这个协议专门用来处理自定义通知UI的内容展示,其中方法列举如下:


//接收到通知时会被调用
/*
开发者可以从notification对象中拿到附件等内容进行UI刷新
*/
- (void)didReceiveNotification:(UNNotification *)notification;
//当用户点击了通知中的用户交互按钮时会被调用
/*
response对象中有通知内容相关信息
在回调block块completion中,开发者可以传入一个UNNotificationContentExtensionResponseOption参数来告诉系统如何处理这次用户活动
UNNotificationContentExtensionResponseOption枚举中可选值如下:
typedef NS_ENUM(NSUInteger, UNNotificationContentExtensionResponseOption) {
  //不关闭当前通知界面
  UNNotificationContentExtensionResponseOptionDoNotDismiss,
  //关闭当前通知界面
  UNNotificationContentExtensionResponseOptionDismiss,
  //关闭当前通知界面并将用户活动传递给宿主app处理
  UNNotificationContentExtensionResponseOptionDismissAndForwardAction,
} __IOS_AVAILABLE(10_0) __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __OSX_UNAVAILABLE;
*/
- (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption option))completion;
/*
这个属性作为get方法进行实现 这个方法用来返回一个通知界面要显示的媒体按钮
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;
//点击播放按钮的回调
- (void)mediaPlay;
//点击暂停按钮的回调
- (void)mediaPause;
//媒体开始播放的回调
- (void)mediaPlayingStarted __IOS_AVAILABLE(10_0) __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __OSX_UNAVAILABLE;
//媒体开始暂停的回调
- (void)mediaPlayingPaused __IOS_AVAILABLE(10_0) __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __OSX_UNAVAILABLE;