美文网首页
### iOS10 UserNotification

### iOS10 UserNotification

作者: ___1o_8o | 来源:发表于2016-09-28 11:00 被阅读228次

UserNotification类介绍

  • UNNotificationCenter:通知管理中心,单例,通知的注册,接收通知后的回调处理等,是UserNotification框架的核心。

  • UNNotification:通知对象,其中封装了通知请求。

  • UNNotificationSettings:通知相关设置。

  • UNNotificationCategory:通知模板。

  • UNNotificationAction:用于定义通知模板中的用户交互行为。

  • UNNotificationRequest:注册通知请求,其中定义了通知的内容和触发方式。

  • UNNotificationResponse:接收到通知后的回执。

  • UNNotificationContent:通知的具体内容。

  • UNNotificationTrigger:通知的触发器,由其子类具体定义。

  • UNNotificationAttachment:通知附件类,为通知内容添加媒体附件。

  • UNNotificationSound:定义通知音效。

  • UNPushNotificationTrigger:远程通知的触发器,UNNotificationTrigger子类。

  • UNTimeInervalNotificationTrigger:计时通知的触发器,UNNotificationTrigger子类。

  • UNCalendarNotificationTrigger:周期通知的触发器,UNNotificationTrigger子类。

  • UNLocationNotificationTrigger:地域通知的触发器,UNNotificationTrigger子类。

  • UNNotificationCenterDelegate:协议,其中方法用于监听通知状态。

权限申请

权限申请代码

    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionCarPlay completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            //设置代理
            [UNUserNotificationCenter currentNotificationCenter].delegate = self;
        }
    }];

requestAuthorizationWithOptions权限参数介绍

typedef NS_OPTIONS(NSUInteger, UNAuthorizationOptions) {
    UNAuthorizationOptionBadge   = (1 << 0), //数字
    UNAuthorizationOptionSound   = (1 << 1), //声音
    UNAuthorizationOptionAlert   = (1 << 2), //弹框
    UNAuthorizationOptionCarPlay = (1 << 3), //车载设备
} __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);

创建通知

创建通知代码

    //创建通知
    //通知可变内容类
    UNMutableNotificationContent *content = [UNMutableNotificationContent new];
    content.badge = @2;
    //通知内容
    content.body = @"This is iOS10 new Notification Body";
    //默认通知提示音
    content.sound = [UNNotificationSound defaultSound];
    //副标题 - 新特性
    content.subtitle = @"this is subTitle";
    //标题
    content.title = @"this is title";
    //启动图片 - 新特性
    content.launchImageName = @"launchImg";
    //设置延迟执行
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
    //创建Request
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"requestIdentifier" content:content trigger:trigger];
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"addNotificationCompletionHandler");
    }];

通知触发器

UNTimeIntervalNotificationTrigger 计时触发器(多少秒之后触发)


Paste_Image.png

UNCalendarNotificationTrigger 日历触发器


Paste_Image.png

UNLocationNotificationTrigger 定位触发器


Paste_Image.png

通知Attachment附件

附件分为以下几种:

  1. 图片
  2. 音频
  3. 视频

需要使用到UNNotificationAttachment类:下面是示例代码

    //创建一个attachment(图片附件)
    UNNotificationAttachment *attach = [UNNotificationAttachment attachmentWithIdentifier:@"attachment" URL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"example" ofType:@"png"]] options:nil error:nil];
    //创建通知
    UNMutableNotificationContent *content = [UNMutableNotificationContent new];
    //设置附件数组
    content.attachments = @[attach];
    content.badge = @2;
    //通知内容
    content.body = @"This is iOS10 new Notification Body";
    //默认通知提示音
    content.sound = [UNNotificationSound defaultSound];
    //副标题 - 新特性
    content.subtitle = @"this is subTitle";
    //标题
    content.title = @"this is title";
    //启动图片 - 新特性
    content.launchImageName = @"launchImg";
    //设置延迟执行
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
    //创建Request
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"requestIdentifier" content:content trigger:trigger];
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"addNotificationCompletionHandler");
    }];
Paste_Image.png Paste_Image.png Paste_Image.png

注意大小要求

Paste_Image.png

附件options对应的Key

//手动设置附件类型,不设置则自动推断
extern NSString * const UNNotificationAttachmentOptionsTypeHintKey __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);

//是否隐藏缩略图 NSNumber 0或1 默认不隐藏
extern 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);

定义通知模板

可输入的通知模板,通常应用于通讯类App回复信息

    //屏幕解锁状态下
//    UNNotificationActionOptionAuthenticationRequired = (1 << 0)
    //破坏性
//    UNNotificationActionOptionDestructive = (1 << 1),
    //允许后台启动app
//    UNNotificationActionOptionForeground = (1 << 2),
    //无
//    UNNotificationActionOptionNone
    UNTextInputNotificationAction *textAction = [UNTextInputNotificationAction actionWithIdentifier:@"textaction" title:@"回复" options:UNNotificationActionOptionAuthenticationRequired textInputButtonTitle:@"确认" textInputPlaceholder:@"请输入回复内容"];
    //无
//    UNNotificationCategoryOptionNone = (0),
    //代理方法能接收到dismiss action
//    UNNotificationCategoryOptionCustomDismissAction = (1 << 0),
    //车载
//    UNNotificationCategoryOptionAllowInCarPlay = (2 << 0),
    UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"categoryidentify" actions:@[textAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
    ```
    UNMutableNotificationContent *content = [UNMutableNotificationContent new];
    //这里设置要与UNNotificationCategory中一样
    content.categoryIdentifier = @"categoryidentify";
    content.badge = @2;
    //通知内容
    content.body = @"This is iOS10 new Notification Body";
    //默认通知提示音
    content.sound = [UNNotificationSound defaultSound];
    //副标题 - 新特性
    content.subtitle = @"this is subTitle";
    //标题
    content.title = @"this is title";
    //启动图片 - 新特性
    content.launchImageName = @"launchImg";
    //设置延迟执行
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
    //创建Request
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"requestIdentifier" content:content trigger:trigger];
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"addNotificationCompletionHandler");
    }];


----------

>按钮通知模板
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) {
}];


#通知回调处理
UserNotification的回调处理是通过UNUserNotificationCenterDelegate协议方法来实现的,我们成为通知代理并实现下面两个方法:

pragma mark - UNUserNotificationCenterDelegate

//当App在前台活跃时,通知将要弹出时会调用(后台不会调用)
//设置completionHandler的UNNotificationPresentationOptions可以使App在前台活跃状态下依然弹出通知消息
//UNNotificationPresentationOptionBadge = (1 << 0), 修改badge
//UNNotificationPresentationOptionSound = (1 << 1), 通知音效
//UNNotificationPresentationOptionAlert = (1 << 2), 通知框
//UNNotificationPresentationOptionNone 啥也不干

  • (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{

}

//当接收到通知后,用户点击通知之后就会调用,(无论是前台还是后台,只要点击就会调用)

  • (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{

}


#UNErrorCode - 错误代码介绍

//通知不被允许
UNErrorCodeNotificationsNotAllowed = 1,
//附件无效URL
UNErrorCodeAttachmentInvalidURL = 100,
//附件类型不被识别
UNErrorCodeAttachmentUnrecognizedType,
//附件大小无效
UNErrorCodeAttachmentInvalidFileSize,
//附件数据错误
UNErrorCodeAttachmentNotInDataStore,
//附件数据存储失败
UNErrorCodeAttachmentMoveIntoDataStoreFailed,
//附件错误
UNErrorCodeAttachmentCorrupt,
//通知日期无效
UNErrorCodeNotificationInvalidNoDate = 1400,
//通知内容无效
UNErrorCodeNotificationInvalidNoContent,

#UNNotification
>注意是readOnly只读
![Paste_Image.png](https://img.haomeiwen.com/i1212794/22a432a70d8355b3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

// 通知触发具体时间
*date;
// 通知请求对象
*request


#UNNotificationSetting

相关文章

网友评论

      本文标题:### iOS10 UserNotification

      本文链接:https://www.haomeiwen.com/subject/pwmhyttx.html