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

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

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

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

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

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

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

详细的代码实现如下

-(void)btnClicked:(UIButton *)sender
{
    //创建两个用于测试的消息体
    UNMutableNotificationContent *content1 = [[UNMutableNotificationContent alloc]init];
    content1.title = @"1";
    content1.body = @"通知1";
    
    UNMutableNotificationContent *content2 = [[UNMutableNotificationContent alloc]init];
    content2.title = @"2";
    content2.body = @"通知2";
    
    switch (sender.tag) {
        case 1001:
        {
            //发送  取消
            UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
            NSString *identifier = @"SendAndCancle";
            UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content1 trigger:trigger];
            
            [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
                //
            }];
            
            //延迟2秒之后执行
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[identifier]];
            });
            break;
        }
        case 1002:
        {
            //发送  更新
            UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
            NSString *identifier = @"SendAndModify";
            UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content1 trigger:trigger];
            
            [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
                //
            }];
            
            //延迟2秒之后执行
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                
                //用相同的标识再次发送即可覆盖
                UNTimeIntervalNotificationTrigger *triggerNew = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
                
                UNNotificationRequest *requestNew = [UNNotificationRequest requestWithIdentifier:identifier content:content2 trigger:triggerNew];
                
                [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:requestNew withCompletionHandler:^(NSError * _Nullable error) {
                    //
                }];
            });

            break;
        }
        case 1003:
        {
            //发送  删除
            UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
            NSString *identifier = @"deliveredSendAndRemove";
            UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content1 trigger:trigger];
            
            [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
                //
            }];
            
            //延迟4秒之后执行
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0/*延迟执行时间*/ * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[identifier]];
            });
            
            break;
        }
        case 1004:
        {
            //发送 修改
            UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
            NSString *identifier = @"deliveredSendAndModify";
            UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content1 trigger:trigger];
            
            [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
                //
            }];
            
            //延迟4秒之后执行
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0/*延迟执行时间*/ * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                
                //用相同的标识再次发送即可覆盖
                UNTimeIntervalNotificationTrigger *triggerNew = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
                
                UNNotificationRequest *requestNew = [UNNotificationRequest requestWithIdentifier:identifier content:content2 trigger:triggerNew];
                
                [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:requestNew withCompletionHandler:^(NSError * _Nullable error) {
                    //
                }];
                
            });
            break;
        }

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值