如图中关系所示,UserNotification框架中的核心类列举如下:
UNNotificationCenter:通知管理中心,单例,通知的注册,接收通知后的回调处理等,是UserNotification框架的核心。
UNNotification:通知对象,其中封装了通知请求。
UNNotificationSettings:通知相关设置。
UNNotificationCategory:通知模板。
UNNotificationAction:用于定义通知模板中的用户交互行为。
UNNotificationRequest:注册通知请求,其中定义了通知的内容和触发方式。
UNNotificationResponse:接收到通知后的回执。
UNNotificationContent:通知的具体内容。
UNNotificationTrigger:通知的触发器,由其子类具体定义。
UNNotificationAttachment:通知附件类,为通知内容添加媒体附件。
UNNotificationSound:定义通知音效。
UNPushNotificationTrigger:远程通知的触发器,UNNotificationTrigger子类。
UNTimeInervalNotificationTrigger:计时通知的触发器,UNNotificationTrigger子类。
UNCalendarNotificationTrigger:周期通知的触发器,UNNotificationTrigger子类。
UNLocationNotificationTrigger:地域通知的触发器,UNNotificationTrigger子类。
UNNotificationCenterDelegate:协议,其中方法用于监听通知状态。
三、进行通知用户权限申请与创建普通的本地通知
要在iOS系统中使用通知,必须获取到用户权限,UserNotification框架中申请通知用户权限需要通过UNNotificationCenter来完成,示例如下:
//进行用户权限的申请[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionCarPlay completionHandler:^(BOOL granted, NSError * _Nullable error) {//在block中会传入布尔值granted,表示用户是否同意if (granted) { //如果用户权限申请成功,设置通知中心的代理 [UNUserNotificationCenter currentNotificationCenter].delegate = self;}}];申请用户权限的方法中需要传入一个权限内容的参数,其枚举定义如下:
typedef NS_OPTIONS(NSUInteger, UNAuthorizationOptions) { //允许更新app上的通知数字 UNAuthorizationOptionBadge = (1 << 0), //允许通知声音 UNAuthorizationOptionSound = (1 << 1), //允许通知弹出警告 UNAuthorizationOptionAlert = (1 << 2), //允许车载设备接收通知 UNAuthorizationOptionCarPlay = (1 << 3),};获取到用户权限后,使用UserNotification创建普通的通知,示例代码如下:
//通知内容类 UNMutableNotificationContent * content = [UNMutableNotificationContent new]; //设置通知请求发送时 app图标上显示的数字 content.badge = @2; //设置通知的内容 content.body = @"这是iOS10的新通知内容:普通的iOS通知"; //默认的通知提示音 content.sound = [UNNotificationSound defaultSound]; //设置通知的副标题 content.subtitle = @"这里是副标题"; //设置通知的标题 content.title = @"这里是通知的标题"; //设置从通知激活app时的launchImage图片 content.launchImageName = @"lun"; //设置5S之后执行 UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefault" content:content trigger:trigger]; //添加通知请求 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { }];效果如下面图示:
四、通知音效类UNNotificationSound
通知可以进行自定义的音效设置,其中方法如下:
//系统默认的音效+ (instancetype)defaultSound;//自定义的音频音效/*注意,音频文件必须在bundle中或者在Library/Sounds目录下*/+ (instancetype)soundNamed:(NSString *)name __WATCHOS_PROHIBITED;五、通知触发器UNNotificationTrigger
//创建触发器 在timeInterval秒后触发 可以设置是否循环触发+ (instancetype)triggerWithTimeInterval:(NSTimeInterval)timeInterval repeats:(BOOL)repeats;//获取下次触发的时间点- (nullable NSDate *)nextTriggerDate;2.UNCalendarNotificationTrigger
//创建触发器 设置触发时间 可以设置是否循环触发+ (instancetype)triggerWithDateMatchingComponents:(NSDateComponents *)dateComponents repeats:(BOOL)repeats;//下一次触发的时间点- (nullable NSDate *)nextTriggerDate;3.UNLocationNotificationTrigger
//地域信息@property (NS_NONATOMIC_IOSONLY, readonly, copy) CLRegion *region;//创建触发器+ (instancetype)triggerWithRegion:(CLRegion *)region repeats:(BOOL)repeats __WATCHOS_PROHIBITED;六、为通知内容添加附件
//创建图片附件 UNNotificationAttachment * attach = [UNNotificationAttachment attachmentWithIdentifier:@"imageAttach" URL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg"]] options:nil error:nil]; UNMutableNotificationContent * content = [UNMutableNotificationContent new]; //设置附件数组 content.attachments = @[attach]; content.badge = @1; content.body = @"这是iOS10的新通知内容:普通的iOS通知"; //默认的通知提示音 content.sound = [UNNotificationSound defaultSound]; content.subtitle = @"这里是副标题"; content.title = @"这里是通知的标题"; //设置5S之后执行 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的附件数组虽然是一个数组,但是系统的通知模板只能展示其中的第一个附件,设置多个附件也不会有额外的效果,但是如果开发者进行通知模板UI的自定义,则此数组就可以派上用场了。音频附件界面如下:
需要注意,添加附件的格式和大小都有一定的要求,如下表格所示:
创建通知内容附件UNNotificationAttachment实例的方法中有一个options配置字典,这个字典中可以进行配置的键值对如下:
//配置附件的类型的键 需要设置为NSString类型的值,如果不设置 则默认从扩展名中推断extern NSString * const UNNotificationAttachmentOptionsTypeHintKey __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);//配置是否隐藏缩略图的键 需要配置为NSNumber 0或者1extern NSString * const UNNotificationAttachmentOptionsThumbnailHiddenKey __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);//配置使用一个标准的矩形来对缩略图进行裁剪,需要配置为CGRectCreateDictionaryRepresentation(CGRect)创建的矩形引用extern NSString * const UNNotificationAttachmentOptionsThumbnailClippingRectKey __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);//使用视频中的某一帧作为缩略图 配置为NSNumber时间extern NSString * const UNNotificationAttachmentOptionsThumbnailTimeKey __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);七、定义通知模板UNNotificationCategory
//创建用户活动 /* options参数可选如下: //需要在解开锁屏下使用 UNNotificationActionOptionAuthenticationRequired //是否指示有破坏性 UNNotificationActionOptionDestructive //是否允许活动在后台启动app UNNotificationActionOptionForeground //无设置 UNNotificationActionOptionNone */ UNTextInputNotificationAction * action = [UNTextInputNotificationAction actionWithIdentifier:@"action" title:@"回复" options:UNNotificationActionOptionAuthenticationRequired textInputButtonTitle:@"活动" textInputPlaceholder:@"请输入回复内容"]; //创建通知模板 UNNotificationCategory * category = [UNNotificationCategory categoryWithIdentifier:@"myNotificationCategoryText" actions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; UNMutableNotificationContent * content = [UNMutableNotificationContent new]; content.badge = @1; content.body = @"这是iOS10的新通知内容:普通的iOS通知"; //默认的通知提示音 content.sound = [UNNotificationSound defaultSound]; content.subtitle = @"这里是副标题"; content.title = @"这里是通知的标题"; //设置通知内容对应的模板 需要注意 这里的值要与对应模板id一致 content.categoryIdentifier = @"myNotificationCategoryText"; //设置5S之后执行 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) { }];需要注意,要使用模板,通知内容UNNotificationContent的categoryIdentifier要与UNNotificationCategory的id一致。效果如下:
也可以为通知模板添加多个自定义的用户交互按钮,示例如下:
UNNotificationAction * action = [UNNotificationAction actionWithIdentifier:@"action" title:@"活动标题1" options:UNNotificationActionOptionNone]; UNNotificationAction * action2 = [UNNotificationAction actionWithIdentifier:@"action" title:@"活动标题2" options:UNNotificationActionOptionNone]; UNNotificationAction * action3 = [UNNotificationAction actionWithIdentifier:@"action" title:@"活动标题3" options:UNNotificationActionOptionNone]; UNNotificationAction * action4 = [UNNotificationAction actionWithIdentifier:@"action" title:@"活动标题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的新通知内容:普通的iOS通知"; //默认的通知提示音 content.sound = [UNNotificationSound defaultSound]; content.subtitle = @"这里是副标题"; content.title = @"这里是通知的标题"; content.categoryIdentifier = @"myNotificationCategoryBtn"; //设置5S之后执行 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) { }];需要注意,系统模板最多支持添加4个用户交互按钮,如下图:
八、自定义通知模板UI
通过前边的介绍,我们发现通过UserNotification框架开发者已经可以完成许多从来很难实现的效果。然而这都不是UserNotification框架最强大的地方,UserNotification框架最强大的地方在于其可以完全自定义通知的UI界面。
完全自定义通知界面是通过iOS扩展来实现的,首先创建一个新的target,如下图:
选择Notification Content,如下:
创建完成后,会发现工程中多了一个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;需要注意,自定义的通知界面上虽然可以放按钮,可以放任何UI控件,但是其不能进行用户交互,唯一可以进行用户交互的方式是通过协议中的媒体按钮及其回调方法。
用如下的代码创建通知:
UNNotificationAction * action = [UNNotificationAction actionWithIdentifier:@"action" title:@"活动标题1" options:UNNotificationActionOptionNone];//根据id拿到自定义UI的模板UNNotificationCategory * category = [UNNotificationCategory categoryWithIdentifier:@"myNotificationCategoryH" actions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];UNMutableNotificationContent * content = [UNMutableNotificationContent new];content.badge = @1;content.body = @"这是iOS10的新通知内容:普通的iOS通知";//默认的通知提示音content.sound = [UNNotificationSound defaultSound];content.subtitle = @"这里是副标题";content.title = @"这里是通知的标题";content.categoryIdentifier = @"myNotificationCategoryH";//设置5S之后执行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键值设置为0或者不设置,则不会隐藏系统默认的UI,如下:
九、通知回调的处理
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框架中其他零散知识
typedef NS_ENUM(NSInteger, UNErrorCode) {//通知不被允许UNErrorCodeNotificationsNotAllowed = 1,//附件无效urlUNErrorCodeAttachmentInvalidURL = 100,//附件类型错误UNErrorCodeAttachmentUnrecognizedType,//附件大小错误UNErrorCodeAttachmentInvalidFileSize,//附件数据错误UNErrorCodeAttachmentNotInDataStore,UNErrorCodeAttachmentMoveIntoDataStoreFailed,UNErrorCodeAttachmentCorrupt,//时间无效UNErrorCodeNotificationInvalidNoDate = 1400,//无内容UNErrorCodeNotificationInvalidNoContent,} __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);2.UNNotification类
@interface UNNotification : NSObject <NSCopying, NSSecureCoding>//触发的时间@property (nonatomic, readonly, copy) NSDate *date;//内置的通知请求对象@property (nonatomic, readonly, copy) UNNotificationRequest *request;- (instancetype)init NS_UNAVAILABLE;@end3.UNNotificationSettings类
@interface UNNotificationSettings : NSObject <NSCopying, NSSecureCoding>//用户权限状态@property (NS_NONATOMIC_IOSONLY, readonly) UNAuthorizationStatus authorizationStatus;//音效设置@property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting soundSetting __TVOS_PROHIBITED;//图标提醒设置@property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting badgeSetting __WATCHOS_PROHIBITED;//提醒框设置@property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting alertSetting __TVOS_PROHIBITED;//通知中心设置@property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting notificationCenterSetting __TVOS_PROHIBITED;//锁屏设置@property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting lockScreenSetting __TVOS_PROHIBITED __WATCHOS_PROHIBITED;//车载设备设置@property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting carPlaySetting __TVOS_PROHIBITED __WATCHOS_PROHIBITED;//提醒框风格@property (NS_NONATOMIC_IOSONLY, readonly) UNAlertStyle alertStyle __TVOS_PROHIBITED __WATCHOS_PROHIBITED;@endUNNotificationSetting枚举如下:
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,}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。