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;
    }
}
最終的效果圖可以在實現之後自行查看,這裏就不上傳介紹了


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章