九、通知回调的处理
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);
2.UNNotification类
@interface UNNotification : NSObject <NSCopying, NSSecureCoding>
//触发的时间
@property (nonatomic, readonly, copy) NSDate *date;
//内置的通知请求对象
@property (nonatomic, readonly, copy) UNNotificationRequest *request;
- (instancetype)init NS_UNAVAILABLE;
@end
3.UNNotificationSettings类
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;
@end
UNNotificationSetting枚举如下:
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,
}