我有一个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];
}
Run Code Online (Sandbox Code Playgroud)
我正在通过Xcode运行App.当应用程序最小化并且我使用通知中心中的通知启动应用程序时,我可以在日志didReceiveRemoteNotification中看到,调用并使用我可以看到的断点,clearNotifications已经运行了.但通知仍然在通知中心挂起.为什么?
我正在使用UNUserNotificationCenter来发送通知,如下所示:
UNUserNotificationCenter.current().getDeliveredNotifications { (notifications) in
self.array = notifications
}
Run Code Online (Sandbox Code Playgroud)
然后在viewWillDisappear上我清除applicationIconBadgeNumber,如下所示:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.applicationIconBadgeNumber = 0
}
Run Code Online (Sandbox Code Playgroud)
这样做不会让我的通知持续很长时间,是的,我希望看到通知后徽章编号为0,但我希望这些编号可以持续24-48小时....我怎么能完成这个?
我已经阅读了几个帖子(比如这个https://forums.developer.apple.com/thread/7598)
application.applicationIconBadgeNumber = -1
Run Code Online (Sandbox Code Playgroud)
on applicationDidBecomeActive或applicationWillEnterForeground允许我清除徽章编号而不从通知中心删除通知.当我设置application.applicationIconBadgeNumber为负数(如-1)时,它会清除我的所有通知Notification Center.有没有其他方法可以清除徽章编号而不删除notifications?