如何清除应用中的远程通知?

时间:2015-05-21 03:58:14

标签: ios swift remote-notifications

从iPhone屏幕顶部向下滑动时,是否有办法从通知横幅中清除远程通知。我尝试将徽章编号设置为零:

application.applicationIconBadgeNumber = 0 

在委托didFinishLaunchingWithOptionsdidReceiveRemoteNotification中,但它没有清除通知。感谢。

8 个答案:

答案 0 :(得分:18)

在iOS 10中,以上所有解决方案均已折旧

  

在iOS 10.0中不推荐使用“cancelAllLocalNotifications()”:使用UserNotifications Framework - [UNUserNotificationCenter removeAllPendingNotificationRequests]

使用以下代码取消通知并重置徽章计数

对于iOS 10,Swift 3.0

从iOS 10推荐

cancelAllLocalNotifications

@available(iOS, introduced: 4.0, deprecated: 10.0, message: "Use UserNotifications Framework's -[UNUserNotificationCenter removeAllPendingNotificationRequests]")
open func cancelAllLocalNotifications()

您必须添加此导入语句

import UserNotifications

获取通知中心。并执行如下操作

application.applicationIconBadgeNumber = 0 // For Clear Badge Counts
let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications() // To remove all delivered notifications
center.removeAllPendingNotificationRequests() // To remove all pending notifications which are not delivered yet but scheduled.

如果要删除单个或多个特定通知,可以通过以下方法实现。

center.removeDeliveredNotifications(withIdentifiers: ["your notification identifier"])

希望它有所帮助.. !!

答案 1 :(得分:12)

您需要将IconBadgeNumber设置为0并取消当前通知。我从未在swift中做过,但我认为它的代码如下:

application.applicationIconBadgeNumber = 0 
application.cancelAllLocalNotifications()

答案 2 :(得分:1)

我必须增加然后递减徽章计数才能使其工作:

application.applicationIconBadgeNumber = 1
application.applicationIconBadgeNumber = 0
application.cancelAllLocalNotifications()

答案 3 :(得分:1)

Swift 3

AppDelegate.swift下的didFinishLaunchingWithOptions文件中添加:

application.applicationIconBadgeNumber = 0

在您的应用启动时,这将删除iOS徽章(应用图标右上角的红色圆圈)。

答案 4 :(得分:1)

任何需要快速提供4及以上代码的人

application.applicationIconBadgeNumber = 0
UNUserNotificationCenter.current().removeAllDeliveredNotifications()

答案 5 :(得分:1)

这是当应用程序被用户强行终止时的情况:

首先,当您想通过推送通知向用户发送生日提醒通知时,发送非零徽章,例如:

 {
  "aps": {
    "alert": {
      "title": "Hey! Urgent Reminder",
      "body": "Do not forget my wife SURPRISE BIRTHDAY PARTY"
    },
    "badge": 1
  }
} 

此后,当不需要在设备中显示通知时,您可以发送带有零徽章的静默通知,即使应用被强行终止,该通知也会清除徽章和通知 strong>由用户执行,但didReceiveRemoteNotification不会调用,因为应用已终止。 静默推送通知的有效负载:

 {
   "aps" : {
      "content-available" : 1,
        "badge" : 0,
        "Priority" : 10
   }
}

发送后,有效负载将自动清除徽章并从通知中心删除推送通知。

不是。如果在发送静默通知前徽章为零,则不会清除通知。

https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html

答案 6 :(得分:0)

下面是2019年11月使用Swift 4为我工作的解决方案。首先,您必须检查设备版本以清除所有通知,而无需检查以重置徽章计数。

override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?  ) -> Bool {

  //--------

  application.applicationIconBadgeNumber = 0
  if #available(iOS 10.0, *) {        
     let center = UNUserNotificationCenter.current() 
     center.removeAllDeliveredNotifications() 
     center.removeAllPendingNotificationRequests()    
  } else {        
     application.cancelAllLocalNotifications()    
  }        

  //------
}

答案 7 :(得分:0)

快速4和5

import UserNotifications

...
...
...

UNUserNotificationCenter.current().removeAllDeliveredNotifications()