• iOS10通知(三)--通知的取消和修改


    iOS- 实现APP前台、后台、甚至杀死进程下收到通知后进行语音播报(金额)

    https://www.jianshu.com/p/601426a247e6

    iOS10通知(三)--通知的取消和修改

     https://blog.csdn.net/xttxqjfg/article/details/68926335

    在创建通知时,我们可以指定标识符。这个标识符可以用来管理通知。

    在 iOS 10 之前,我们很难取消掉某一个特定的通知,也不能主动移除或者更新已经展示的通知。

    iOS 10 中,UserNotifications 框架提供了一系列管理通知的 API,你可以做到

    1、取消还未展示的通知
    2、修改还未展示的通知
    3、删除已经展示过的通知
    4、修改已经展示过的通知

    其中关键就在于在创建请求时使用同样的标识符。

    取消和修改目前还不能用于远程推送,我还没有查找到相关可以实现的资料,如有了解的可以留言告诉我

    详细的代码实现如下

    1.  
      -(void)btnClicked:(UIButton *)sender
    2.  
      {
    3.  
      //创建两个用于测试的消息体
    4.  
      UNMutableNotificationContent *content1 = [[UNMutableNotificationContent alloc]init];
    5.  
      content1.title = @"1";
    6.  
      content1.body = @"通知1";
    7.  
       
    8.  
      UNMutableNotificationContent *content2 = [[UNMutableNotificationContent alloc]init];
    9.  
      content2.title = @"2";
    10.  
      content2.body = @"通知2";
    11.  
       
    12.  
      switch (sender.tag) {
    13.  
      case 1001:
    14.  
      {
    15.  
      //发送 取消
    16.  
      UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
    17.  
      NSString *identifier = @"SendAndCancle";
    18.  
      UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content1 trigger:trigger];
    19.  
       
    20.  
      [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    21.  
      //
    22.  
      }];
    23.  
       
    24.  
      //延迟2秒之后执行
    25.  
      dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));
    26.  
       
    27.  
      dispatch_after(delayTime, dispatch_get_main_queue(), ^{
    28.  
      [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[identifier]];
    29.  
      });
    30.  
      break;
    31.  
      }
    32.  
      case 1002:
    33.  
      {
    34.  
      //发送 更新
    35.  
      UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
    36.  
      NSString *identifier = @"SendAndModify";
    37.  
      UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content1 trigger:trigger];
    38.  
       
    39.  
      [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    40.  
      //
    41.  
      }];
    42.  
       
    43.  
      //延迟2秒之后执行
    44.  
      dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));
    45.  
       
    46.  
      dispatch_after(delayTime, dispatch_get_main_queue(), ^{
    47.  
       
    48.  
      //用相同的标识再次发送即可覆盖
    49.  
      UNTimeIntervalNotificationTrigger *triggerNew = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
    50.  
       
    51.  
      UNNotificationRequest *requestNew = [UNNotificationRequest requestWithIdentifier:identifier content:content2 trigger:triggerNew];
    52.  
       
    53.  
      [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:requestNew withCompletionHandler:^(NSError * _Nullable error) {
    54.  
      //
    55.  
      }];
    56.  
      });
    57.  
       
    58.  
      break;
    59.  
      }
    60.  
      case 1003:
    61.  
      {
    62.  
      //发送 删除
    63.  
      UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
    64.  
      NSString *identifier = @"deliveredSendAndRemove";
    65.  
      UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content1 trigger:trigger];
    66.  
       
    67.  
      [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    68.  
      //
    69.  
      }];
    70.  
       
    71.  
      //延迟4秒之后执行
    72.  
      dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0/*延迟执行时间*/ * NSEC_PER_SEC));
    73.  
       
    74.  
      dispatch_after(delayTime, dispatch_get_main_queue(), ^{
    75.  
      [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[identifier]];
    76.  
      });
    77.  
       
    78.  
      break;
    79.  
      }
    80.  
      case 1004:
    81.  
      {
    82.  
      //发送 修改
    83.  
      UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
    84.  
      NSString *identifier = @"deliveredSendAndModify";
    85.  
      UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content1 trigger:trigger];
    86.  
       
    87.  
      [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    88.  
      //
    89.  
      }];
    90.  
       
    91.  
      //延迟4秒之后执行
    92.  
      dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0/*延迟执行时间*/ * NSEC_PER_SEC));
    93.  
       
    94.  
      dispatch_after(delayTime, dispatch_get_main_queue(), ^{
    95.  
       
    96.  
      //用相同的标识再次发送即可覆盖
    97.  
      UNTimeIntervalNotificationTrigger *triggerNew = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
    98.  
       
    99.  
      UNNotificationRequest *requestNew = [UNNotificationRequest requestWithIdentifier:identifier content:content2 trigger:triggerNew];
    100.  
       
    101.  
      [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:requestNew withCompletionHandler:^(NSError * _Nullable error) {
    102.  
      //
    103.  
      }];
    104.  
       
    105.  
      });
    106.  
      break;
    107.  
      }
    108.  
       
    109.  
      default:
    110.  
      break;
    111.  
      }
    112.  
      }

    最终的效果图可以在实现之后自行查看,这里就不上传介绍了

  • 相关阅读:
    flock对文件锁定读写操作的问题 简单
    hdu 2899 Strange Fuction(二分)
    hdu 2199 Can you solve this equation? (二分)
    poj 3080 Blue Jeans (KMP)
    poj 2823 Sliding Window (单调队列)
    poj 2001 Shortest Prefixes (trie)
    poj 2503 Babelfish (trie)
    poj 1936 All in All
    hdu 3507 Print Article (DP, Monotone Queue)
    fzu 1894 志愿者选拔 (单调队列)
  • 原文地址:https://www.cnblogs.com/itlover2013/p/14235454.html
Copyright © 2020-2023  润新知