iOS10--消息通知的基本使用
官方将通知单独放在了UserNotifications.framework,使用时需要导入框架。
UserNotifications.framework主要类文件:
UNCalendarNotificationTrigger
UNLocationNotificationTrigger
UNMutableNotificationContent
UNNotification
UNNotificationAction
UNNotificationAttachment
UNNotificationCategory
UNNotificationContent
UNNotificationRequest
UNNotificationResponse
UNNotificationServiceExtension
UNNotificationSettings
UNNotificationSound
UNNotificationTrigger
UNPushNotificationTrigger
UNTextInputNotificationAction
UNTextInputNotificationResponse
UNTimeIntervalNotificationTrigger
UNUserNotificationCenter
UNUserNotificationCenter的应用:
- 请求用户授权:
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
// 请求授权
/*
UNAuthorizationOptionBadge = (1 << 0),
UNAuthorizationOptionSound = (1 << 1),
UNAuthorizationOptionAlert = (1 << 2),
UNAuthorizationOptionCarPlay = (1 << 3),
*/
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { if(granted){if(granded)
//同意
}else{
//不同意
}}];
补充:获取授权设置信息// 获取通知授权和设置
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
/*
UNAuthorizationStatusNotDetermined : 没有做出选择
UNAuthorizationStatusDenied : 用户未授权
UNAuthorizationStatusAuthorized :用户已授权
*/
if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined)
{
NSLog(@"未选择");
}else if (settings.authorizationStatus == UNAuthorizationStatusDenied){
NSLog(@"未授权");
}else if (settings.authorizationStatus == UNAuthorizationStatusAuthorized){
NSLog(@"已授权");
}
}] 创建本地通知:
// 创建一个本地通知
UNMutableNotificationContent *content_1 = [[UNMutableNotificationContent alloc] init];
// 主标题
content_1.title = [NSString localizedUserNotificationStringForKey:@"title" arguments:nil];
// 副标题
content_1.subtitle = [NSString localizedUserNotificationStringForKey:@"subtitle" arguments:nil];
content_1.badge = [NSNumber numberWithInteger:];
content_1.body = [NSString localizedUserNotificationStringForKey:@"title_message_for_yan" arguments:nil];
content_1.sound = [UNNotificationSound defaultSound];
// 设置触发时间
UNTimeIntervalNotificationTrigger *trigger_1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval: repeats:NO];
// 创建一个发送请求
UNNotificationRequest *request_1 = [UNNotificationRequest requestWithIdentifier:@"my_notification" content:content_1 trigger:trigger_1];补充:- UserNotifications提供了三种触发器:
UNTimeIntervalNotificationTrigger :一定时间后触发 UNCalendarNotificationTrigger : 在某月某日某时触发 UNLocationNotificationTrigger : 在用户进入或是离开某个区域时触发
- @“my_notification”请求的标识符可以用来管理通知:
- 移除还未展示的通知
[center removePendingNotificationRequestsWithIdentifiers: @[@“my_notification”]];
[center removeAllPendingNotificationRequests]; // - (void)cancelAllLocalNotifications; - 移除已经展示过的通知
[center removeDeliveredNotificationsWithIdentifiers:@[@“my_notification”]];
[center removeAllDeliveredNotifications]; - 获取未展示的通知
[center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
NSLog(@"%@",requests);
}]; - 获取展示过的通知
[center getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
NSLog(@"%@",notifications);
}]; - 远程通知的格式:
{ "aps":{ "alert":{ "title":"I am title", "subtitle":"I am subtitle", "body":"I am body" }, "sound":"default", "badge": } }具体请参考官方文档
- UserNotifications提供了三种触发器:
将通知请求添加到通知中心(UNUserNotificationCenter):
[center addNotificationRequest:request_1 withCompletionHandler:^(NSError * _Nullable error) { }];
处理通知:
设置UNUserNotificationCenterDelegate:注意:UNUserNotificationCenter 的 delegate 必须在 application:willFinishLaunchingWithOptions: orapplication:didFinishLaunchingWithOptions: 方法中实现;center.delegate = self;
应用内展示通知:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
// 如果不想显示某个通知,可以直接用空 options 调用 completionHandler: // completionHandler([])
completionHandler(UNNotificationPresentationOptionBadge + UNNotificationPresentationOptionSound);
}- 在用户与你推送的通知进行交互时被调用:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
completionHandler();
NSLog(@"userInfo--%@",response.notification.request.content.userInfo);
}
UNNotificationCategory的应用:
- 创建一个 category:
/* UNNotificationActionOptionAuthenticationRequired = (1 << 0), UNNotificationActionOptionDestructive = (1 << 1), 取消 UNNotificationActionOptionForeground = (1 << 2), 启动程序 */ UNTextInputNotificationAction *textAction = [UNTextInputNotificationAction actionWithIdentifier:@"my_text" title:@"text_action" options:UNNotificationActionOptionForeground textInputButtonTitle:@"输入" textInputPlaceholder:@"默认文字"]; UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"my_action" title:@"action" options:UNNotificationActionOptionDestructive]; UNNotificationAction *action_1 = [UNNotificationAction actionWithIdentifier:@"my_action_1" title:@"action_1" options:UNNotificationActionOptionAuthenticationRequired]; /* UNNotificationCategoryOptionNone = (0), UNNotificationCategoryOptionCustomDismissAction = (1 << 0), UNNotificationCategoryOptionAllowInCarPlay = (2 << 0), */ UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"my_category" actions:@[textAction,action,action_1] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; NSSet *setting = [NSSet setWithObjects:category, nil]; [center setNotificationCategories:setting];
- 在创建 UNNotificationContent 时把 categoryIdentifier 设置为需要的 category id 即可:
content.categoryIdentifier = @"my_category";
远程推送也可以使用 category,只需要在 payload 中添加 category 字段,并指定预先定义的 category id 就可以了
- 处理category的通知:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ completionHandler(); NSLog(@"userInfo--%@",response.notification.request.content.userInfo); // 获取通知中心的所有的Categories [center getNotificationCategoriesWithCompletionHandler:^(NSSet<UNNotificationCategory *> * _Nonnull categories) { for (UNNotificationCategory *category in categories) { if ([category.identifier isEqualToString:@"my_category"] && [response.notification.request.content.categoryIdentifier isEqualToString:@"my_category"]) { for (UNNotificationAction *textAction in category.actions) { if ([textAction.identifier isEqualToString:@"my_text"]) { UNTextInputNotificationAction *text = (UNTextInputNotificationAction *)textAction; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:text.textInputButtonTitle preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]]; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil]; } } } } }]; }
iOS 10 中被标为弃用的 API
UILocalNotification
UIMutableUserNotificationAction
UIMutableUserNotificationCategory
UIUserNotificationAction
UIUserNotificationCategory
UIUserNotificationSettings
handleActionWithIdentifier:forLocalNotification:
handleActionWithIdentifier:forRemoteNotification:
didReceiveLocalNotification:withCompletion:
didReceiveRemoteNotification:withCompletion:
iOS10--消息通知的基本使用的更多相关文章
- UWP消息通知
在Windows 10通常是使用Toast通知方式进行的消息通知,但是在应用通知是不需要通知带有音效的,但是又不能在系统通知中心留下记录,那么需要监听ToastNotification实例的Dismi ...
- Redis系列二之事务及消息通知
一.事务 Redis中的事务是一组命令的集合.一个事务中的命令要么都执行,要么都不执行. 1.事务简介 事务的原理是先将一个事务的命令发送给Redis,然后再让Redis依次执行这些命令.下面看一个示 ...
- Redis笔记(六)Redis的消息通知
Redis的消息通知可以使用List类型的LPUSH和RPOP(左进右出),当然更方便的是直接使用Redis的Pub/Sub(发布/订阅)模式. >>使用List实现队列 使用列表类型的L ...
- 使用 Windows10 自定义交互消息通知
消息通知是最常用的应用功能之一了,但是由于平台的差异,IOS Android 以及 Windows 都有其特殊性,Android开发者在国内常常都是使用三方的一些推送服务,或者是使用自建的服务器为应用 ...
- Android中的消息通知(NotificationManager和Notification)
下面来谈谈notification,这个notification一般用在电话,短 信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这 ...
- Android消息通知(notification)和PendingIntent传值
通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...
- cordova的android notify消息通知插件
最近在学习用CORDOVA(PHONEGAP)结合SENCHA TOUCH开发应用,想实现一个安卓下的消息通知功能,这个可以通过CORDOVA的插件来实现. 插件目录结构如下: notifyplugi ...
- Unity3D研究院之IOS本地消息通知LocalNotification的使用
原地址:http://www.xuanyusong.com/archives/2632 现在的游戏里一般都会有本地消息,比如每天定时12点或者下午6点告诉玩家进入游戏领取体力.这种东西没必要服务器 ...
- HTML 5的消息通知机制
译文来源:http://www.ido321.com/1130.html 原文:HTML 5 Notification 译文:HTML 5 的消息通知机制 译者:dwqs HTML 5 已经被应用到W ...
- iOS8新特性之基于地理位置的消息通知UILocalNotification
苹果在WWDC2014上正式公布了全新的iOS8操作系统. 界面上iOS8与iOS7相比变化不大,只是在功能方面进行了完好. ...
随机推荐
- 润乾在jetty应用服务器下的JNDI配置一
一. 此处绑定的数据源是以 DBCP 为实现.首先必须将数据库驱动(这里用了MYSQL数据库)和DBCP所需要的 Jar 包复制到 Jetty 根目录的 lib 目录下.DBCP主要需要以下3个 ...
- Android使用xml文件中的array资源
Android中有种使用数组的非常简单的用法,在xml文件中获取. 创建数组资源 在value目录下创建arrays.xml文件 然后在arrays.xml文件中使用<string-array& ...
- ahjesus wp-autopost破解版,亲测可用
在funtion.php里 把fetchUrl 这个函数的判断去掉 直接执行判断为真的结果下面是修改后的函数 function fetchUrl($_var_22){ global $w ...
- web项目启动时,自动执行代码的几种方式
在项目开发过程中,往往需要一些功能随着项目启动而优先启动,下面我总结几种方式(非spring boot) spring boot的参考 spring boot 学习之路9 (项目启动后就执行特定方法) ...
- Datastage重启服务
使用DS开发job时,偶尔一个Job出现卡死现象,导致工作不能进展下去,有时候需要重启datastage服务才可以. DataStage在正常运行时候有以下两个主要的进程: (1)dsrpcd(DS的 ...
- Hibernate 集成 Ehcache 开启二级缓存
一.将 Ehcache.xml 放到 classpath 下 <?xml version="1.0" encoding="UTF-8"?> < ...
- Spring MVC 4常用的那些注解
Spring从2.5版本开始在编程中引入注解,用户可以使用@RequestMapping, @RequestParam, @ModelAttribute等等这样类似的注解.到目前为止,Spring的版 ...
- 渐变显示渐变消失的BackgroundView
渐变显示渐变消失的BackgroundView 效果如下: 源码: BackgroundView.h 与 BackgroundView.m // // BackgroundView.h // Test ...
- 我们是如何拿下Google和Facebook Offer的?
http://posts.careerengine.us/p/57c3a1c1a09633ee7e57803c 大家好,我是小高,CMU CS Master,来Offer第一期学员,2014年初在孙老 ...
- Mosquitto安装调试实录
1. 安装 以Centos 7.x为例,先安装依赖项(笔者使用阿里云资源,部分依赖项可能未列出): yum install gcc-c++ yum install openssl-devel yum ...