官方将通知单独放在了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": } }

      具体请参考官方文档

  • 将通知请求添加到通知中心(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];
    
    }
    
    }
    
    }
    
    }
    
    }];
    
    }
          

长按 3D touch 效果图

          

进入应用
iOS 10 中被标为弃用的 API
UILocalNotification
UIMutableUserNotificationAction
UIMutableUserNotificationCategory
UIUserNotificationAction
UIUserNotificationCategory
UIUserNotificationSettings
handleActionWithIdentifier:forLocalNotification:
handleActionWithIdentifier:forRemoteNotification:
didReceiveLocalNotification:withCompletion:
didReceiveRemoteNotification:withCompletion:

来源

iOS10--消息通知的基本使用的更多相关文章

  1. UWP消息通知

    在Windows 10通常是使用Toast通知方式进行的消息通知,但是在应用通知是不需要通知带有音效的,但是又不能在系统通知中心留下记录,那么需要监听ToastNotification实例的Dismi ...

  2. Redis系列二之事务及消息通知

    一.事务 Redis中的事务是一组命令的集合.一个事务中的命令要么都执行,要么都不执行. 1.事务简介 事务的原理是先将一个事务的命令发送给Redis,然后再让Redis依次执行这些命令.下面看一个示 ...

  3. Redis笔记(六)Redis的消息通知

    Redis的消息通知可以使用List类型的LPUSH和RPOP(左进右出),当然更方便的是直接使用Redis的Pub/Sub(发布/订阅)模式. >>使用List实现队列 使用列表类型的L ...

  4. 使用 Windows10 自定义交互消息通知

    消息通知是最常用的应用功能之一了,但是由于平台的差异,IOS Android 以及 Windows 都有其特殊性,Android开发者在国内常常都是使用三方的一些推送服务,或者是使用自建的服务器为应用 ...

  5. Android中的消息通知(NotificationManager和Notification)

    下面来谈谈notification,这个notification一般用在电话,短 信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这 ...

  6. Android消息通知(notification)和PendingIntent传值

    通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...

  7. cordova的android notify消息通知插件

    最近在学习用CORDOVA(PHONEGAP)结合SENCHA TOUCH开发应用,想实现一个安卓下的消息通知功能,这个可以通过CORDOVA的插件来实现. 插件目录结构如下: notifyplugi ...

  8. Unity3D研究院之IOS本地消息通知LocalNotification的使用

    原地址:http://www.xuanyusong.com/archives/2632   现在的游戏里一般都会有本地消息,比如每天定时12点或者下午6点告诉玩家进入游戏领取体力.这种东西没必要服务器 ...

  9. HTML 5的消息通知机制

    译文来源:http://www.ido321.com/1130.html 原文:HTML 5 Notification 译文:HTML 5 的消息通知机制 译者:dwqs HTML 5 已经被应用到W ...

  10. iOS8新特性之基于地理位置的消息通知UILocalNotification

              苹果在WWDC2014上正式公布了全新的iOS8操作系统. 界面上iOS8与iOS7相比变化不大,只是在功能方面进行了完好.                             ...

随机推荐

  1. JAVA视频网盘分享

    JAVA视频网盘分享 [涵盖从java入门到深入架构,Linux.云计算.分布式.大数据Hadoop.ios.Android.互联网技术应有尽有] 1.JavaScript视频教程 链接: http: ...

  2. 关于 eval 的报错 Uncaught ReferenceError: False is not defined

    var obj ={'id': 16, 'name': '管理员', 'delflag': False, 'grade': 1000000.0}VM3614:1 Uncaught ReferenceE ...

  3. PS制作gif动图以及背景透明与消除残影

    摘要: 用Photoshop制作gif动画的要点:在窗口菜单中找到“时间轴”选中打开时间轴,单击一帧,设置该帧显示持续时间在图层里将该帧要显示的图层显示,并将不该显示的层隐藏,新建一帧,接下来就是重复 ...

  4. Activity被回收导致fragment的getActivity为空

    在编写含有Fragment代码的时候,经常会遇到这种情况,假如app长时间在后台运行,再点击进入会crash,而且fragment页面有重叠的现象. 如果系统内存不足.或者切换横竖屏.或者app长时间 ...

  5. 数组也继承了Object类

    C++ 最根上的类有好多,也可以随便地定义.

  6. JS DOM节点增删改查 属性设置

    一.节点操作 增 createElement(name)创建元素 appendChild();将元素添加   删 获得要删除的元素 获得它的父元素 使用removeChild()方法删除 改 第一种方 ...

  7. Html.Partial()传值的问题

    @Html.Partial("Test", Model, new ViewDataDictionary { { "a", "b" } }); ...

  8. Oracle GoldenGate OGG管理员手册(较早资料)

    第一章 系统实现简述 前言 编写本手册的目的是为系统管理员以及相关操作人员提供 Oracle  Goldengat  软 件的日常维护和使用的技术参考: 3 ORACLE 第二章 OGG 日常维护操作 ...

  9. 浅析Oracle 12c中Data Guard新特性

    浅析Oracle 12c中Data Guard新特性   写在前面 无论是做Oracle运维的小伙伴还是老伙伴,想必对Oracle数据库的数据级灾备核心技术—Data Guard是再熟悉不过了!这项从 ...

  10. Tomcat 安全设置 及 内存修改

    1.删除%tomcatRoot%/webapps目录下的examples.docs文件夹 2.修改%tomcatRoot%/conf/tomcat-users.xml <?xml version ...