iOS 应用程序:如何清除通知? [英] iOS application: how to clear notifications?
问题描述
我有一个 iOS 应用程序,一些推送通知会发送到其中.我的问题是,消息/通知在点击后仍保留在 iOS 的通知中心中.下次打开应用程序时,如何在通知中心删除我的应用程序的通知?
I've an iOS application where some Push Notification are sent to. My problem is, that the messages/notifications stays in the Notification Center in iOS after then are tapped. How can I remove a notification for my application in the Notification Center next time the application opens?
我看到有人将 setApplicationIconBadgeNumber
调用为零值以清除通知的帖子.这对我来说似乎很奇怪,所以我相信可能存在另一种解决方案?
I came across posts where people are calling setApplicationIconBadgeNumber
to a zero-value to clear the notifications. That's seems very weird to me, so I believe that maybe another solution exists?
我在清除通知时遇到了一些问题.请在此处查看我的代码:
I'm having some problems clearing the notifications. Please see my code here:
- (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 运行该应用程序.当应用程序最小化并使用通知中心的通知启动应用程序时,我可以在日志中看到 didReceiveRemoteNotification
被调用并使用了我可以看到的断点,clearNotifications
已经跑了.但通知仍然挂在通知中心.为什么?
I'm running the App through Xcode. When the App is minimized and I start the App using the notification in the Notification Center, I can see in the log, that the didReceiveRemoteNotification
is called and using breakpoints I can see, that the clearNotifications
has ran. But still the notification hangs in the Notification Center. Why?
推荐答案
很可能是因为通知中心是一个相对较新的功能,Apple 不一定想要推出全新的清除通知范式.所以相反,他们多用途 [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
来清除所述通知.这可能看起来有点奇怪,Apple 可能会在未来提供更直观的方式来执行此操作,但目前这是官方方式.
Most likely because Notification Center is a relatively new feature, Apple didn't necessarily want to push a whole new paradigm for clearing notifications. So instead, they multi-purposed [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
to clear said notifications. It might seem a bit weird, and Apple might provide a more intuitive way to do this in the future, but for the time being it's the official way.
我自己,我使用这个片段:
Myself, I use this snippet:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
从通知中心清除所有应用的通知.
which never fails to clear all of the app's notifications from Notification Center.
这篇关于iOS 应用程序:如何清除通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!