How can I remove previously delivered notifications when a new notification arrives with UNUserNotificationCenterDelegate in iOS 10?

后端 未结 2 1697
太阳男子
太阳男子 2020-12-17 18:46

This question is about the new UserNotifications framework in iOS 10.

I have an app that schedules a local notification every half hour after the user d

相关标签:
2条回答
  • 2020-12-17 18:52

    You can check this demo。

    I think you want to achieve the function is called "update notification".

    iOS 10 allow update the notification.All you just do——keep the notifications have the same identifier.

    Let's see a demo:

    1. first notification:

        NSURL * imageUrl = [[NSBundle mainBundle] URLForResource:@"dog" withExtension:@"png"];
        UNNotificationAttachment *imgAtt = [UNNotificationAttachment attachmentWithIdentifier:@"image" URL:imageUrl options:nil error:&error];
        
        NSURL * mp4Url = [[NSBundle mainBundle] URLForResource:@"media" withExtension:@"mp4"];
        UNNotificationAttachment *mediaAtt = [UNNotificationAttachment attachmentWithIdentifier:@"image" URL:mp4Url options:nil error:&error];
        
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];
        //在通知中心显示的总是第一个多媒体资源
        content.attachments = @[imgAtt,mediaAtt];
        content.badge = @1;
        content.title = @"Wake Up";
        content.subtitle = @"First time";
        content.body = @"next time。。。 ";
        content.categoryIdentifier = @"wakeup";
        content.launchImageName = @"dog";
        content.sound = [UNNotificationSound defaultSound];
    //    content.threadIdentifier = @"";
        content.userInfo = @{@"first":@"5:00 am",@"second":@"6:00"};
        
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5.0 repeats:NO];
        
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"com.junglesong.pushtestdemo.wakeup" content:content trigger:trigger];
        
        [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            NSLog(@"wake up message has been deliverd!");
        }];

    1. update the first notification:

        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];
        content.badge = @1;
        content.title = @"Update!dear,wake up";
        content.subtitle = @"Update! dear,please";
        content.body = @"Update!shall we have breakfast?";
        content.categoryIdentifier = @"wakeup";
        content.launchImageName = @"dog";
        content.sound = [UNNotificationSound defaultSound];
        //    content.threadIdentifier = @"";
        content.userInfo = @{@"first":@"5:00 am",@"second":@"6:00"};
        
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5.0 repeats:NO];
        
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"com.junglesong.pushtestdemo.wakeup" content:content trigger:trigger];
        
        [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            NSLog(@"wake up message has been updated!");
        }];

    Now, you add two notification.But the system treats them as the same.So there is just only one.And the sencond replaces the first one.In iOS 10,call this update.

    The property "identifier" is the id of UNNotificationRequest,which can differentiate notifications.

    0 讨论(0)
  • 2020-12-17 19:03

    You can just use removeDeliveredNotifications(withIdentifiers:)

    0 讨论(0)
提交回复
热议问题