iOS10 新的通知框架 UserNotifications
来源:互联网 发布:数据分类汇总步骤文字 编辑:程序博客网 时间:2024/05/20 10:21
iOS10采用了新的通知框架,相比较原来的框架,功能丰富了不少,在使用前需引入框架
@import UserNotifications;
使用前需要用户授权
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; //设置代理 center.delegate = self; //获取用户的推送授权 iOS 10新方法 [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { }]; //获取当前的通知设置,UNNotificationSettings 是只读对象,readOnly,只能通过以下方法获取 [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { }]; }
委托方法,记得加入
<UNUserNotificationCenterDelegate>
#pragma mark - #pragma mark - UNUserNotificationCenterDelegate//在展示通知前进行处理,即有机会在展示通知前再修改通知内容。-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{ //1. 处理通知 //2. 处理完成后条用 completionHandler ,用于指示在前台显示通知的形式 completionHandler(UNNotificationPresentationOptionAlert);}- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler { // 点击通知的处理 NSLog(@"didReceiveNotificationResponse");}
如何发送通知:
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; // > 需创建一个包含待通知内容的 UNMutableNotificationContent 对象,可变 UNNotificationContent 对象,不可变 // > 通知内容 UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init]; // > 通知的title content.title = [NSString localizedUserNotificationStringForKey:[NSString stringWithFormat:@"From: %@",_info[@"name"]] arguments:nil]; // > 通知的要通知内容 content.body = [NSString localizedUserNotificationStringForKey:[NSString stringWithFormat:@"%@",_info[@"title"]] arguments:nil]; // > 通知的提示声音 content.sound = [UNNotificationSound defaultSound]; content.userInfo = @{@"content":_info}; // > 通知的延时执行 UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond" content:content trigger:trigger]; //添加推送通知,等待通知即可! [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { // > 可在此设置添加后的一些设置 // > 例如alertVC。。 }];
以上就是基本的使用流程了。
更多功能请参考:http://www.jianshu.com/p/b74e52e866fc
阅读全文
0 0
- iOS10 新的通知框架 UserNotifications
- iOS10通知框架UserNotifications学习
- iOS10更新_本地通知新框架UserNotifications
- iOS10的推送框架UserNotifications
- iOS10的推送框架UserNotifications以及UserNotificationsUI
- iOS10的推送框架UserNotifications以及UserNotificationsUI
- iOS10本地通知UserNotifications快速入门
- iOS10本地通知UserNotifications快速入门
- IOS 10 适配 系列_0_ iOS10 UserNotifications 框架解析
- 来搞一搞UserNotifications本地通知
- iOS10本地通知的探索
- ios10下的通知更新
- 模仿iOS10的下拉通知
- iOS10通知框架UserNotification理解与应用
- iOS10通知框架UserNotification理解与应用
- ios10的新内容
- iOS10的新特征
- AVFoundation拍摄照片(iOS10新框架)
- Android Studio Error:Connection timed out: connect.
- 自定义安全性高的验证机制
- vim常见使用命令
- 2017 年最佳开源网络监控工具
- Android:用GSON 五招之内搞定任何JSON数组
- iOS10 新的通知框架 UserNotifications
- ZOJ 1284 Perfection
- CTF安卓逆向练习第四弹
- 【JFinal】Field 'xxx' doesn't have a default value
- maven3 下载配置
- Anemometer安装
- Git使用教程
- 最近的一些交互工作
- SVN