Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何清除应用程序中的远程通知?_Ios_Swift_Remote Notifications - Fatal编程技术网

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

Ios 如何清除应用程序中的远程通知?,ios,swift,remote-notifications,Ios,Swift,Remote Notifications,当从iPhone屏幕顶部向下滑动时,是否有办法从通知横幅中清除远程通知。我尝试将徽章编号设置为零: application.applicationIconBadgeNumber = 0 在delegatedidFinishLaunchingWithOptions,以及DidReceiveMemotentification,但它没有清除通知。谢谢。您需要将IconBadgeNumber设置为0并取消当前通知。我从未使用过swift,但我认为其代码如下: application.applicat

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

application.applicationIconBadgeNumber = 0 

在delegate
didFinishLaunchingWithOptions
,以及
DidReceiveMemotentification
,但它没有清除通知。谢谢。

您需要将IconBadgeNumber设置为0并取消当前通知。我从未使用过swift,但我认为其代码如下:

application.applicationIconBadgeNumber = 0 
application.cancelAllLocalNotifications()
Swift 5

UNUserNotificationCenter.current().removeAllDeliveredNotifications()
import UserNotifications

...
...
...

UNUserNotificationCenter.current().removeAllDeliveredNotifications()

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

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

Swift 3

在您的
AppDelegate.swift
文件中的
didfishlaunchwithoptions
添加:

application.applicationIconBadgeNumber = 0

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

在iOS 10中,所有解决方案都会贬值

iOS 10.0中不推荐使用“cancelAllLocalNotifications()”:使用UserNotifications框架的-[UnuseNotificationCenter removeAllPendingNotificationRequests]

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

对于iOS 10,Swift 3.0

cancelAllLocalNotifications
已从iOS 10弃用

您必须添加此导入语句

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
  }
} 
之后,当不需要在设备中显示通知时,您可以发送带有零徽章的静默通知,该通知将清除徽章和通知,即使应用程序被用户强制终止,但不会调用
DidReceiveMemotentification
,因为应用程序已终止。 静默推送通知的有效负载:

 {
   "aps" : {
      "content-available" : 1,
        "badge" : 0,
        "Priority" : 10
   }
}
发送后,有效负载将自动清除徽章并从通知中心删除推送通知

不是。如果发送静默通知前标记为零,则不会清除通知


Swift 4&5

UNUserNotificationCenter.current().removeAllDeliveredNotifications()
import UserNotifications

...
...
...

UNUserNotificationCenter.current().removeAllDeliveredNotifications()

是否使用cancelAllLocalNotifications?这是远程通知。感谢您的评论。操作对不起,我与本地通知混淆了,因为我通常同时使用这两种通知。或者,这也可能有助于尝试@icaroNZ建议的方法,它可以帮助从通知栏中删除通知。使用applicationBadgeNumber对我很有用。谢谢