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
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:
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!");
}];
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.
You can just use removeDeliveredNotifications(withIdentifiers:)