我有一个iOS应用程序,其中一些推送通知被发送到.我的问题是,在点击之后,消息/通知会留在iOS的通知中心.下次应用程序打开时,如何在通知中心删除应用程序的通知?
我遇到过一些帖子,人们用setApplicationIconBadgeNumber
对零值来清除通知.这对我来说似乎很奇怪,所以我认为可能存在另一种解决方案?
EDIT1:
我在清除通知时遇到了一些问题.请在这里查看我的代码:
- (void) clearNotifications {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSLog(@"Launched from push notification: %@", dictionary);
[self clearNotifications];
}
}
return YES;
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
NSLog(@"Received notification: %@", userInfo);
[self clearNotifications];
}
我正在通过Xcode运行应用程序.当应用程序最小化,我使用通知中心的通知启动应用程序时,我可以在日志(log)中看到,调用了didReceiveRemoteNotification
,使用断点可以看到,clearNotifications
已经运行.但通知仍然挂在通知中心.为什么?