设为首页 加入收藏

TOP

iOS10--消息通知的基本使用(二)
2019-08-26 07:00:27 】 浏览:80
Tags:iOS10-- 消息 通知 基本 使用
@[@“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":1 } }
    具体请参考官方文档
  • 将通知请求添加到通知中心(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:UNNotificationCategoryOptionCustom
    首页 上一页 1 2 3 下一页 尾页 2/3/3
    】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
    上一篇.gitignore中添加的某个忽略文件.. 下一篇iOS:WKWebView(19-05-30更)

    最新文章

    热门文章

    Hot 文章

    Python

    C 语言

    C++基础

    大数据基础

    linux编程基础

    C/C++面试题目