经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
iOS10--消息通知的基本使用
来源:cnblogs  作者:久依  时间:2019/1/25 9:18:27  对本文有异议

官方将通知单独放在了UserNotifications.framework,使用时需要导入框架。
UserNotifications.framework主要类文件:

  1. UNCalendarNotificationTrigger
  2. UNLocationNotificationTrigger
  3. UNMutableNotificationContent
  4. UNNotification
  5. UNNotificationAction
  6. UNNotificationAttachment
  7. UNNotificationCategory
  8. UNNotificationContent
  9. UNNotificationRequest
  10. UNNotificationResponse
  11. UNNotificationServiceExtension
  12. UNNotificationSettings
  13. UNNotificationSound
  14. UNNotificationTrigger
  15. UNPushNotificationTrigger
  16. UNTextInputNotificationAction
  17. UNTextInputNotificationResponse
  18. UNTimeIntervalNotificationTrigger
  19. UNUserNotificationCenter

UNUserNotificationCenter的应用:

  • 请求用户授权:
    1. UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    2. // 请求授权
    3. /*
    4. UNAuthorizationOptionBadge = (1 << 0),
    5. UNAuthorizationOptionSound = (1 << 1),
    6. UNAuthorizationOptionAlert = (1 << 2),
    7. UNAuthorizationOptionCarPlay = (1 << 3),
    8. */
    9. [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {  if(granted){

       if(granded)
         //同意
       }else{
         //不同意
       }

    1. }];

    补充:获取授权设置信息
    1. // 获取通知授权和设置
    2. [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
    3. /*
    4. UNAuthorizationStatusNotDetermined : 没有做出选择
    5. UNAuthorizationStatusDenied : 用户未授权
    6. UNAuthorizationStatusAuthorized :用户已授权
    7. */
    8. if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined)
    9. {
    10. NSLog(@"未选择");
    11. }else if (settings.authorizationStatus == UNAuthorizationStatusDenied){
    12. NSLog(@"未授权");
    13. }else if (settings.authorizationStatus == UNAuthorizationStatusAuthorized){
    14. NSLog(@"已授权");
    15. }
    16. }]
  • 创建本地通知:

    1. // 创建一个本地通知
    2. UNMutableNotificationContent *content_1 = [[UNMutableNotificationContent alloc] init];
    3. // 主标题
    4. content_1.title = [NSString localizedUserNotificationStringForKey:@"title" arguments:nil];
    5. // 副标题
    6. content_1.subtitle = [NSString localizedUserNotificationStringForKey:@"subtitle" arguments:nil];
    7. content_1.badge = [NSNumber numberWithInteger:1];
    8. content_1.body = [NSString localizedUserNotificationStringForKey:@"title_message_for_yan" arguments:nil];
    9. content_1.sound = [UNNotificationSound defaultSound];
    10. // 设置触发时间
    11. UNTimeIntervalNotificationTrigger *trigger_1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
    12. // 创建一个发送请求
    13. UNNotificationRequest *request_1 = [UNNotificationRequest requestWithIdentifier:@"my_notification" content:content_1 trigger:trigger_1];

    补充:

    • UserNotifications提供了三种触发器:
      1. UNTimeIntervalNotificationTrigger :一定时间后触发
      2. UNCalendarNotificationTrigger 在某月某日某时触发
      3. UNLocationNotificationTrigger 在用户进入或是离开某个区域时触发
    • @“my_notification”请求的标识符可以用来管理通知:
      1. - 移除还未展示的通知
      2. [center removePendingNotificationRequestsWithIdentifiers: @[@“my_notification”]];
      3. [center removeAllPendingNotificationRequests]; // - (void)cancelAllLocalNotifications;
      4. - 移除已经展示过的通知
      5. [center removeDeliveredNotificationsWithIdentifiers:@[@“my_notification”]];
      6. [center removeAllDeliveredNotifications];
      7. - 获取未展示的通知
      8. [center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
      9. NSLog(@"%@",requests);
      10. }];
      11. - 获取展示过的通知
      12. [center getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
      13. NSLog(@"%@",notifications);
      14. }];
    • 远程通知的格式:
      1. { "aps":{ "alert":{ "title":"I am title", "subtitle":"I am subtitle", "body":"I am body" }, "sound":"default", "badge":1 } }
      具体请参考官方文档
  • 将通知请求添加到通知中心(UNUserNotificationCenter):

    1. [center addNotificationRequest:request_1 withCompletionHandler:^(NSError * _Nullable error) {

      }];

     

         
 
 
接收通知
  • 处理通知:
    设置UNUserNotificationCenterDelegate
    注意:UNUserNotificationCenter 的 delegate 必须在 application:willFinishLaunchingWithOptions: orapplication:didFinishLaunchingWithOptions: 方法中实现;

    1. center.delegate = self;
    • 应用内展示通知:

      1. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
      2. // 如果不想显示某个通知,可以直接用空 options 调用 completionHandler: // completionHandler([])
      3. completionHandler(UNNotificationPresentationOptionBadge + UNNotificationPresentationOptionSound);
      4. }

       

    • 在用户与你推送的通知进行交互时被调用:
      1. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
      2. completionHandler();
      3. NSLog(@"userInfo--%@",response.notification.request.content.userInfo);
      4. }

UNNotificationCategory的应用:

  • 创建一个 category:
    1. /*
    2. UNNotificationActionOptionAuthenticationRequired = (1 << 0),
    3. UNNotificationActionOptionDestructive = (1 << 1), 取消
    4. UNNotificationActionOptionForeground = (1 << 2), 启动程序
    5. */
    6. UNTextInputNotificationAction *textAction = [UNTextInputNotificationAction actionWithIdentifier:@"my_text" title:@"text_action" options:UNNotificationActionOptionForeground textInputButtonTitle:@"输入" textInputPlaceholder:@"默认文字"];
    7. UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"my_action" title:@"action" options:UNNotificationActionOptionDestructive];
    8. UNNotificationAction *action_1 = [UNNotificationAction actionWithIdentifier:@"my_action_1" title:@"action_1" options:UNNotificationActionOptionAuthenticationRequired];
    9. /*
    10. UNNotificationCategoryOptionNone = (0),
    11. UNNotificationCategoryOptionCustomDismissAction = (1 << 0),
    12. UNNotificationCategoryOptionAllowInCarPlay = (2 << 0),
    13. */
    14. UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"my_category" actions:@[textAction,action,action_1] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
    15. NSSet *setting = [NSSet setWithObjects:category, nil];
    16. [center setNotificationCategories:setting];

     

  • 在创建 UNNotificationContent 时把 categoryIdentifier 设置为需要的 category id 即可:
    1. content.categoryIdentifier = @"my_category";

    远程推送也可以使用 category,只需要在 payload 中添加 category 字段,并指定预先定义的 category id 就可以了

  • 处理category的通知:
    1. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
    2. completionHandler();
    3. NSLog(@"userInfo--%@",response.notification.request.content.userInfo);
    4. // 获取通知中心的所有的Categories
    5. [center getNotificationCategoriesWithCompletionHandler:^(NSSet<UNNotificationCategory *> * _Nonnull categories) {
    6. for (UNNotificationCategory *category in categories) {
    7. if ([category.identifier isEqualToString:@"my_category"] && [response.notification.request.content.categoryIdentifier isEqualToString:@"my_category"]) {
    8. for (UNNotificationAction *textAction in category.actions) {
    9. if ([textAction.identifier isEqualToString:@"my_text"]) {
    10. UNTextInputNotificationAction *text = (UNTextInputNotificationAction *)textAction;
    11. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:text.textInputButtonTitle preferredStyle:UIAlertControllerStyleAlert];
    12. [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]];
    13. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
    14. }
    15. }
    16. }
    17. }
    18. }];
    19. }
          
长按 3D touch 效果图

          
进入应用
iOS 10 中被标为弃用的 API
  1. UILocalNotification
  2. UIMutableUserNotificationAction
  3. UIMutableUserNotificationCategory
  4. UIUserNotificationAction
  5. UIUserNotificationCategory
  6. UIUserNotificationSettings
  7. handleActionWithIdentifier:forLocalNotification:
  8. handleActionWithIdentifier:forRemoteNotification:
  9. didReceiveLocalNotification:withCompletion:
  10. didReceiveRemoteNotification:withCompletion:

 

来源

原文链接:http://www.cnblogs.com/jiuyi/p/10314551.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号