UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody =[NSString stringWithFormat:@"meeting schedule from %@ ",[namelist objectAtIndex:i]];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName = @"meetting.mp3";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[[NSNotificationCenter defaultCenter] postNotificationName:@"timerInvoked" object:self];
我正在使用上面的代码来创建本地通知。但以前的通知并未清除。它会在触发新通知时再次触发。
我使用[[UIApplication sharedApplication] cancelAllLocalNotifications];
通知不会在后台触发。如何在触发后删除通知?
答案 0 :(得分:0)
cancelAllLocalNotifications
删除待处理的通知。一旦被解雇,我认为您不能从通知中心删除通知。用户设置(通过“设置”应用)他们想要保留的通知数量,一旦数量超过该限制,它们就会自动删除。
答案 1 :(得分:0)
当应用程序收到本地通知时,将调用以下方法。把它放在app delegate中:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplication *app = [UIApplication sharedApplication];
NSInteger badgeNumber = [app applicationIconBadgeNumber];// Take the current badge number
badgeNumber--; // decrement by one
[app setApplicationIconBadgeNumber:badgeNumber]; // set ne badge number
[app cancelLocalNotification:notification]; // cancel the received notification. It will clear the notification from banner alos
}
答案 2 :(得分:0)
如果您希望能够在解雇后删除特定通知,则需要存储它们。
后:
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
您还可以将其保存在NSUserDefaults中,使用NSKeyedArchiver将其转换为NSData。
NSData *notificationData = [NSKeyedArchiver archivedDataWithRootObject:localNotification];
[[NSUserDefaults standardUserDefaults] setObject:notificationData forKey:[NSString stringWithFormat:@"%d", someKeyOfYourChoice]];
要将其作为UILocalNotification取回,请使用NSKeyedUnarchiver。然后你可以使用cancelLocalNotification方法删除它。
进一步解释here(Swift版本+原始Obj-C解决方案的链接)