为了账号安全,请及时绑定邮箱和手机立即绑定

iOS应用程序:如何清除通知?

iOS应用程序:如何清除通知?

守着一只汪 2019-10-05 14:41:54
我有一个iOS应用程序,其中向其中发送了一些推送通知。我的问题是,在点击邮件/通知后,它们会留在iOS的通知中心中。下次打开应用程序时,如何在通知中心中为我的应用程序删除通知?我碰到过有人呼吁setApplicationIconBadgeNumber零值清除通知的帖子。这对我来说似乎很奇怪,所以我相信也许存在另一种解决方案?编辑1:我在清除通知时遇到了一些问题。请在这里查看我的代码:- (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已运行。但是通知仍然挂在通知中心中。为什么?
查看完整描述

3 回答

?
湖上湖

TA贡献2003条经验 获得超2个赞

很可能是因为Notification Center是一个相对较新的功能,Apple并不一定要推动全新的范例来清除通知。因此,他们有多种用途[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];来清除上述通知。这似乎有点怪异,Apple将来可能会提供一种更直观的方式来执行此操作,但目前这是官方方式。


我自己,使用以下代码段:


[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];

[[UIApplication sharedApplication] cancelAllLocalNotifications];

永远不会从通知中心清除该应用程序的所有通知。


查看完整回答
反对 回复 2019-10-05
?
慕桂英546537

TA贡献1848条经验 获得超10个赞

iOS 10更新(Swift 3)

为了清除iOS 10应用中的所有本地通知,您应该使用以下代码:


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

} else {

    UIApplication.shared.cancelAllLocalNotifications()

}

此代码处理针对iOS 10.x和所有先前版本的iOS的本地通知的清除。您将需要import UserNotificationsiOS 10.x代码。


查看完整回答
反对 回复 2019-10-05
  • 3 回答
  • 0 关注
  • 900 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信