Ios5 我是否可以通过编程方式清除我的应用程序';来自iOS 5通知中心的s通知?
我想删除我的应用程序从iOS 5通知中心发出的旧通知。我能做这个吗?如果是,如何删除?要从通知中心删除通知,只需将图标徽章编号设置为零即可Ios5 我是否可以通过编程方式清除我的应用程序';来自iOS 5通知中心的s通知?,ios5,Ios5,我想删除我的应用程序从iOS 5通知中心发出的旧通知。我能做这个吗?如果是,如何删除?要从通知中心删除通知,只需将图标徽章编号设置为零即可 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; 这仅在号码更改时有效,因此如果您的应用程序未使用徽章号码,则必须先设置,然后重置它 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
这仅在号码更改时有效,因此如果您的应用程序未使用徽章号码,则必须先设置,然后重置它
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
对我来说,它只适用于发送带有以下徽章的本地通知:
if([UIApplication sharedApplication].applicationIconBadgeNumber == 0) {
UILocalNotification *singleLocalPush = [[UILocalNotification alloc] init];
singleLocalPush.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
singleLocalPush.hasAction = NO;
singleLocalPush.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:singleLocalPush];
[singleLocalPush release];
} else {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
在方法上,
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
我可以再次将徽章设置为0。是的,您可以通过呼叫取消特定或所有本地通知
[[UIApplication sharedApplication] cancelLocalNotification:...];
或
我使用的一种更直接的方法(不需要徽章)是将定时本地通知数组重置为自身,如下所示:
UIApplication* application = [UIApplication sharedApplication];
NSArray* scheduledNotifications = [NSArray arrayWithArray:application.scheduledLocalNotifications];
application.scheduledLocalNotifications = scheduledNotifications;
这会使所有计划通知保持有效,同时会删除通知中心中的所有“旧”通知。然而,它也有可能在未来的iOS版本中发生变化的感觉,因为我还没有看到任何关于这种行为的文档
当然,如果要删除所有通知,只需执行以下操作:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
如果要清除swift和iOS 10.0中的通知
import UserNotifications
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests() // To remove all pending notifications which are not delivered yet but scheduled.
center.removeAllDeliveredNotifications() // To remove all delivered notifications
}
这是有效的…您必须将其设置为某个值,然后重置为0…谢谢@voidStern@voidStern:你太棒了……:)回答得好,我什么都试过了,效果很好。如果更适合您,也可以使用点语法:)
[UIApplication sharedApplication].ApplicationOnBadgeNumber=1;[UIApplication sharedApplication].applicationIconBadgeNumber=0代码>在iOS>=7上仍然需要此功能吗?
import UserNotifications
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests() // To remove all pending notifications which are not delivered yet but scheduled.
center.removeAllDeliveredNotifications() // To remove all delivered notifications
}