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相比变化不大,只是在功能方面进行了完好. ...
随机推荐
- Flutter自定义标题栏之处理状态栏高度
App在很多情况下由于各种需求需要自定义标题栏,而在能够构建Android和IOS应用的Flutter中,如果不在Scaffold中使用AppBar会发现默认是沉浸式. 猜想:我们使用自定义标题栏好像 ...
- 程序控制结构及for循环、foreach循环、迭代器
结构化程序设计 三种基本控制结构:顺序结构.选择结构.循环结构. 在这种思想的指导下,发展出了面向过程编程方式.面向过程编程的核心是算法+数据结构.算法可以用顺序.选择.循环这三种基本控制结构来实现. ...
- 【Python】Java程序员学习Python(八)— 基本类型的基本运算
这一篇待写,毕竟基本运算都是通用的.
- Prometheus Node_exporter 之 Basic Net / Disk Info
1. Network Traffic Basic 每个接口的基本网络信息 type: GraphUnit: bytesrecv {{device}} 各个网络接口的下载量 recv lo: 本地环回接 ...
- python/numpy/pandas数据操作知识与技巧
pandas针对dataframe各种操作技巧集合: filtering: 一般地,使用df.column > xx将会产生一个只有boolean值的series,以该series作为dataf ...
- 阿里云ECS服务器环境搭建 ubuntu 16.04 图形界面的安装
https://blog.csdn.net/zwq912318834/article/details/80528374
- [翻译] CSStickyHeaderFlowLayout
CSStickyHeaderFlowLayout https://github.com/jamztang/CSStickyHeaderFlowLayout Parallax, Sticky Heade ...
- 获取INET4与INET6的信息
获取INET4与INET6的信息 参考书籍: 本人封装的源码: // // IPAddressInfo.h // YXNETWORK // // http://www.cnblogs.com/YouX ...
- 转载:从程序员的角度看ASCII, GB2312, UNICODE, UTF-8
以下内容转自博客:http://blog.chinaunix.net/uid-22670933-id-1771613.html. 一.字符编码是怎么回事 0. 概念 字节是计算机的最基本存储单位,一个 ...
- CSS学习摘要-数值和单位及颜色
在CSS中,值的类型有很多种,一些很常见,一些你却几乎没怎么遇到过.我们不会在这篇文档中面面俱到地描述他们,而只是这些对于掌握CSS可能最有用处的这些.本文将会涉及如下CSS的值: 数值: 长度值,用 ...