How to clear the remote notification in your app?

最后都变了- 提交于 2019-12-03 05:30:19
Icaro

You need to set the IconBadgeNumber to 0 and cancel the current notifications. I never did in swift but I think the code for it would be as bellow:

application.applicationIconBadgeNumber = 0 
application.cancelAllLocalNotifications()

In iOS 10, above all solutions are depreciated

'cancelAllLocalNotifications()' was deprecated in iOS 10.0: Use UserNotifications Framework's -[UNUserNotificationCenter removeAllPendingNotificationRequests]

Use the below code to cancel notification and reset Badge count

For iOS 10, Swift 3.0

cancelAllLocalNotifications deprecated from iOS 10.

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

You will have to add this import statement,

import UserNotifications

Get notification center. And perform the operation like below

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.

If you want to remove single or multiple specific notifications, you can achieve it by below method.

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

Hope it helps..!!

I have to increment then decrement badge count in order for it to work:

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

Swift 3

In your AppDelegate.swift file under didFinishLaunchingWithOptions add:

application.applicationIconBadgeNumber = 0

On the launch of your app this will remove the iOS badge (red circle at the top right corner of the app icon).

any one looking for swift 4 and above code

application.applicationIconBadgeNumber = 0
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!