iOS10通知框架UserNotification理解与应用

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

如果将UNNotificationExtensionDefaultContentHidden键值设置为0或者不设置,则不会隐藏系统默认的UI,如下:

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

九、通知回调的处理

        UserNotification框架对于通知的回调处理,是通过UNUserNotificationCenterDelegate协议来实现的,这个协议中有两个方法,如下:


/*
这个方法在应用在前台,并且将要弹出通知时被调用,后台状态下弹通知不会调用这个方法
这个方法中的block块completionHandler()可以传入一个UNNotificationPresentationOptions类型的枚举
有个这个参数,开发者可以设置在前台状态下,依然可以弹出通知消息,枚举如下:
typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
  //只修改app图标的消息数
  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时被调用,无论前台还是后台
*/
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_PROHIBITED;

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

        前面所介绍的内容基本涵盖了UserNotification框架中所有的内容,在以后的应用开发中,开发者可以在通知方面发挥更大的想象力与创造力,给用户更加友好的体验。除了前边所介绍过的核心内容外,UserNotification框架中还有一些零散的类、枚举等。

1.错误码描述


typedef NS_ENUM(NSInteger, UNErrorCode) {
  //通知不被允许
  UNErrorCodeNotificationsNotAllowed = 1,
  
  //附件无效url
  UNErrorCodeAttachmentInvalidURL = 100,
  //附件类型错误
  UNErrorCodeAttachmentUnrecognizedType,
  //附件大小错误
  UNErrorCodeAttachmentInvalidFileSize,
  //附件数据错误
  UNErrorCodeAttachmentNotInDataStore,
  UNErrorCodeAttachmentMoveIntoDataStoreFailed,
  UNErrorCodeAttachmentCorrupt,
  
  //时间无效
  UNErrorCodeNotificationInvalidNoDate = 1400,
  //无内容
  UNErrorCodeNotificationInvalidNoContent,
} __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);