iOS-本地推送之UserNotifications(iOS 10)
2016-09-23 14:27
441 查看
参考文档(iOS 10 UserNotifications 框架解析—-onevcat)
参考文档(兼容iOS 10 资料整理笔记—–判若两人丶)
iOS 10 苹果Notification进行了很大的重构,同时也让开发者体会到UserNotifications的易用,功能也变得非常强大,统一了本地推送和远程推送,而且可以自定义推送信息展示界面。
1.所有相关通知被统一到了UserNotifications.framework框架中。
2.通知不再区分类型(本地和远程)
3.细化了本地推送推送条件,可以按时间差,日期和区域进行推送。
二、注册通知,获取推送权限(iOS 10采用了全新的注册方式)
三、实现通知的代理方法,用于获取推送信息和处理点击推送信息后的界面跳转
三、配置本地推送信息,并将推送添加到推送中心
推送结果如下:
下拉展示通知中心:
例如当设置在 2016-10-01 08:00:00进行推送,设置如下:
当用于进入某个区域后进行推送,设置如下:
参考文档(兼容iOS 10 资料整理笔记—–判若两人丶)
iOS 10 苹果Notification进行了很大的重构,同时也让开发者体会到UserNotifications的易用,功能也变得非常强大,统一了本地推送和远程推送,而且可以自定义推送信息展示界面。
1.所有相关通知被统一到了UserNotifications.framework框架中。
2.通知不再区分类型(本地和远程)
3.细化了本地推送推送条件,可以按时间差,日期和区域进行推送。
这里只对本地推送进行讲解
一、导入通知新框架-UserNotifications,并设置代理#import <UserNotifications/UserNotifications.h> @interface AppDelegate () <UNUserNotificationCenterDelegate> @end
二、注册通知,获取推送权限(iOS 10采用了全新的注册方式)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // 使用 UNUserNotificationCenter 来管理通知 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; // 监听回调事件 center.delegate = self; //iOS 10 使用以下方法注册,才能得到授权,注册通知以后,会自动注册 deviceToken,如果获取不到 deviceToken,Xcode8下要注意开启 Capability->Push Notification。 /* UNAuthorizationOptionBadge = (1 << 0), UNAuthorizationOptionSound = (1 << 1), UNAuthorizationOptionAlert = (1 << 2), */ [center requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { NSLog(@"通知注册成功"); } }]; return YES; }
三、实现通知的代理方法,用于获取推送信息和处理点击推送信息后的界面跳转
#pragma mark - UNUserNotificationCenterDelegate // 点击通知进入程序页面是调用 // 在展示通知前进行处理,即有机会在展示通知前再修改通知内容。 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { NSLog(@"当前点击的通知 : %@",notification); } // 收到通知时调用 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { NSLog(@"收到通知时 : %@",response); }
三、配置本地推送信息,并将推送添加到推送中心
// 使用 UNUserNotificationCenter 来管理通知 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; //需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。 UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init]; // 标题 content.title = @"标题"; // 子标题 content.subtitle = @"子标题"; // 内容 content.body = @"内容"; // 标记个数 content.badge = @1; // 推送提示音 content.sound = [UNNotificationSound defaultSound]; // 指定音频文件 // content.sound = [UNNotificationSound soundNamed:@"音频文件名"]; // 启动图片(好像不起作用) content.launchImageName = @"Default"; // 附加信息 content.userInfo = @{@"key1":@"value1",@"key2":@"value2"}; // 添加附件 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"puff.jpg" ofType:nil]; UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"mazy" URL:[NSURL fileURLWithPath:filePath] options:0 error:nil]; content.attachments = @[attachment]; // UNTimeIntervalNotificationTrigger 通过时间差通知 // UNCalendarNotificationTrigger 通过日期通知 // UNLocationNotificationTrigger 通过位置信息通知 // 通过时间差,多少秒后推送本地推送 UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5.0 repeats:NO]; UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"Mazy" content:content trigger:trigger]; //添加推送成功后的处理! [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@"推送添加成功"); } }]; 注:其中的关于推送一些参数的设置需要通过下面方法继续设置 + (NSString *)localizedUserNotificationStringForKey:(NSString *)key arguments:(nullable NSArray *)arguments __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_PROHIBITED; 例如: content.body = [NSString localizedUserNotificationStringForKey:@"内容" arguments:nil]; content.title = [NSString localizedUserNotificationStringForKey:@"标题" arguments:nil]; content.subtitle = [NSString localizedUserNotificationStringForKey:@"子标题" arguments:nil];
推送结果如下:
下拉展示通知中心:
例如当设置在 2016-10-01 08:00:00进行推送,设置如下:
// 在 某个时间日期进行本地推送 NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; dateComponents.year = 2016; dateComponents.month = 10; dateComponents.day = 1; dateComponents.hour = 8; dateComponents.minute = 0; dateComponents.second = 0; UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:NO];
当用于进入某个区域后进行推送,设置如下:
// 在 进入某个区域后 推送本地推送(注意:此处用CLCircularRegion,不要用CLRegion) CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:CLLocationCoordinate2DMake(40, 116) radius:1000 identifier:@"mazy"]; UNLocationNotificationTrigger *trigger = [UNLocationNotificationTrigger triggerWithRegion:region repeats:NO];
相关文章推荐
- 玩转 iOS 10 推送 —— UserNotifications Framework(上)
- 玩转 iOS 10 推送 —— UserNotifications Framework(上)
- iOS 10 消息推送(UserNotifications)秘籍总结
- iOS 10 消息推送(UserNotifications)秘籍总结(一)
- iOS 10 消息推送(UserNotifications)秘籍总结(一)
- iOS 10 消息推送(UserNotifications)秘籍总结(一)
- iOS 10 消息推送(UserNotifications)秘籍总结(一,二)
- iOS 10 消息推送(UserNotifications)秘籍总结(一)
- iOS 10 消息推送(UserNotifications)秘籍总结(一)
- 玩转 iOS 10 推送 —— UserNotifications Framework(合集)
- iOS 10 消息推送(UserNotifications)秘籍总结(一)
- iOS 10 消息推送(UserNotifications)秘籍总结(一)
- 玩转 iOS 10 推送 —— UserNotifications Framework(合集)
- iOS 10 消息推送(UserNotifications)秘籍总结(二)
- iOS 10 消息推送(UserNotifications)秘籍总结(二)
- 玩转 iOS 10 推送 —— UserNotifications Framework(下)
- User Notifications Framework in iOS 10
- iOS 10本地推送
- iOS 10 添加本地推送(Local Notification)
- ios 10 适配系列-ATS、隐私数据、UserNotifications、UICollectionView汇总