如何清除应用中的远程通知?
How to clear the remote notification in your app?
从 iPhone 屏幕顶部向下滑动时,是否可以清除通知横幅中的远程通知。我尝试将徽章编号设置为零:
application.applicationIconBadgeNumber = 0
在委托 didFinishLaunchingWithOptions
和 didReceiveRemoteNotification
中,但它没有清除通知。谢谢
您需要将IconBadgeNumber 设置为0 并取消当前通知。我从来没有在 swift 中做过,但我认为它的代码如下所示:
application.applicationIconBadgeNumber = 0
application.cancelAllLocalNotifications()
Swift 5
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
我必须递增然后递减徽章计数才能正常工作:
application.applicationIconBadgeNumber = 1
application.applicationIconBadgeNumber = 0
application.cancelAllLocalNotifications()
Swift 3
在您的 AppDelegate.swift
文件中 didFinishLaunchingWithOptions
添加:
application.applicationIconBadgeNumber = 0
在您的应用启动时,这将删除 iOS 标志(应用图标右上角的红色圆圈)。
在iOS10,上面所有的解决方案都折旧了
'cancelAllLocalNotifications()' was deprecated in iOS 10.0: Use UserNotifications Framework's -[UNUserNotificationCenter removeAllPendingNotificationRequests]
使用以下代码取消通知并重置徽章计数
对于iOS10,Swift3.0
cancelAllLocalNotifications
从 iOS 10.
弃用
@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"])
希望对您有所帮助..!!
任何正在寻找 swift 4 及以上代码的人
application.applicationIconBadgeNumber = 0
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
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()
}
//------
}
这适用于应用程序被用户强制终止的情况:
首先发送非零徽章当您想通过推送通知向用户发送生日提醒通知时,例如:
{
"aps": {
"alert": {
"title": "Hey! Urgent Reminder",
"body": "Do not forget my wife SURPRISE BIRTHDAY PARTY"
},
"badge": 1
}
}
之后,当不需要在设备中显示通知时,您可以发送带有 零徽章 的静默通知,即使应用 被用户强制终止,但didReceiveRemoteNotification
不会被调用,因为应用程序已终止。
静默推送通知的负载:
{
"aps" : {
"content-available" : 1,
"badge" : 0,
"Priority" : 10
}
}
发送后,有效负载将自动清除徽章并从通知中心删除推送通知。
不是。如果在发送静默通知之前徽章为零,则不会清除通知。
Swift 4 & 5
import UserNotifications
...
...
...
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
从 iPhone 屏幕顶部向下滑动时,是否可以清除通知横幅中的远程通知。我尝试将徽章编号设置为零:
application.applicationIconBadgeNumber = 0
在委托 didFinishLaunchingWithOptions
和 didReceiveRemoteNotification
中,但它没有清除通知。谢谢
您需要将IconBadgeNumber 设置为0 并取消当前通知。我从来没有在 swift 中做过,但我认为它的代码如下所示:
application.applicationIconBadgeNumber = 0
application.cancelAllLocalNotifications()
Swift 5
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
我必须递增然后递减徽章计数才能正常工作:
application.applicationIconBadgeNumber = 1
application.applicationIconBadgeNumber = 0
application.cancelAllLocalNotifications()
Swift 3
在您的 AppDelegate.swift
文件中 didFinishLaunchingWithOptions
添加:
application.applicationIconBadgeNumber = 0
在您的应用启动时,这将删除 iOS 标志(应用图标右上角的红色圆圈)。
在iOS10,上面所有的解决方案都折旧了
'cancelAllLocalNotifications()' was deprecated in iOS 10.0: Use UserNotifications Framework's -[UNUserNotificationCenter removeAllPendingNotificationRequests]
使用以下代码取消通知并重置徽章计数
对于iOS10,Swift3.0
cancelAllLocalNotifications
从 iOS 10.
@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"])
希望对您有所帮助..!!
任何正在寻找 swift 4 及以上代码的人
application.applicationIconBadgeNumber = 0
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
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()
}
//------
}
这适用于应用程序被用户强制终止的情况:
首先发送非零徽章当您想通过推送通知向用户发送生日提醒通知时,例如:
{
"aps": {
"alert": {
"title": "Hey! Urgent Reminder",
"body": "Do not forget my wife SURPRISE BIRTHDAY PARTY"
},
"badge": 1
}
}
之后,当不需要在设备中显示通知时,您可以发送带有 零徽章 的静默通知,即使应用 被用户强制终止,但didReceiveRemoteNotification
不会被调用,因为应用程序已终止。
静默推送通知的负载:
{
"aps" : {
"content-available" : 1,
"badge" : 0,
"Priority" : 10
}
}
发送后,有效负载将自动清除徽章并从通知中心删除推送通知。
不是。如果在发送静默通知之前徽章为零,则不会清除通知。
Swift 4 & 5
import UserNotifications
...
...
...
UNUserNotificationCenter.current().removeAllDeliveredNotifications()