How to clear the remote notification in your app?
Asked Answered
T

8

24

Is there a way to clear the remote notification from the notification banner when swiping down from the top of the iPhone screen. I tried setting the badge number to zero:

application.applicationIconBadgeNumber = 0 

in delegate didFinishLaunchingWithOptions, and didReceiveRemoteNotification, but it did not clear the notifications. Thanks.

Twentyfourmo answered 21/5, 2015 at 3:58 Comment(0)
W
23

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()

Swift 5

UNUserNotificationCenter.current().removeAllDeliveredNotifications()
Winch answered 21/5, 2015 at 6:9 Comment(8)
Use cancelAllLocalNotifications? This is remote notification. Thanks for comments.Twentyfourmo
Ops sorry I got confuse with the local notifications as I normally use both at the same time.Winch
Or maybe this can help too #10972325Winch
try the approach suggested by @icaroNZ it can help to remove notifications from notification bar.Midgut
Using applicationIconBadgeNumber works for me. Thanks.Twentyfourmo
It's possible now for iOS 10 and above. Check my answer: https://mcmap.net/q/544163/-how-to-clear-the-remote-notification-in-your-appAposiopesis
'cancelAllLocalNotifications()' was deprecated in iOS 10.0: Use UserNotifications Framework's -[UNUserNotificationCenter removeAllPendingNotificationRequests]Sanctify
application.applicationIconBadgeNumber = 0 Working perfectly with UNUserNotificationCenter.current().removeAllDeliveredNotifications()Dissertation
S
32

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..!!

Sanctify answered 29/10, 2017 at 8:1 Comment(0)
W
23

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()

Swift 5

UNUserNotificationCenter.current().removeAllDeliveredNotifications()
Winch answered 21/5, 2015 at 6:9 Comment(8)
Use cancelAllLocalNotifications? This is remote notification. Thanks for comments.Twentyfourmo
Ops sorry I got confuse with the local notifications as I normally use both at the same time.Winch
Or maybe this can help too #10972325Winch
try the approach suggested by @icaroNZ it can help to remove notifications from notification bar.Midgut
Using applicationIconBadgeNumber works for me. Thanks.Twentyfourmo
It's possible now for iOS 10 and above. Check my answer: https://mcmap.net/q/544163/-how-to-clear-the-remote-notification-in-your-appAposiopesis
'cancelAllLocalNotifications()' was deprecated in iOS 10.0: Use UserNotifications Framework's -[UNUserNotificationCenter removeAllPendingNotificationRequests]Sanctify
application.applicationIconBadgeNumber = 0 Working perfectly with UNUserNotificationCenter.current().removeAllDeliveredNotifications()Dissertation
B
2

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

application.applicationIconBadgeNumber = 1
application.applicationIconBadgeNumber = 0
application.cancelAllLocalNotifications()
Brazilein answered 5/2, 2016 at 20:0 Comment(0)
P
2

any one looking for swift 4 and above code

application.applicationIconBadgeNumber = 0
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
Photina answered 10/9, 2019 at 7:12 Comment(0)
P
1

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).

Pennywise answered 25/10, 2016 at 16:3 Comment(0)
M
1

This for scenario when the app is forcefully terminated by the user :

First of all send nonzero badge when you want to send Birthday reminders notifications to the users via push notifications , For example :

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

After that , When there is no need of showing notifications in the device , you can send silent notification with zero badge that will clear badge and notifications even if app is forcefully terminated by the user but didReceiveRemoteNotification will not called because app is terminated. payload for silent push notification :

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

After send that payload will automatically clear badge and delete push notification from Notification Center.

Not. If badge was zero before sending silent notification , will not clear notifications.

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

Macaroni answered 24/2, 2020 at 14:11 Comment(0)
A
0

In Nov, 2019 below is the working solution for me with Swift 4. First you have to check the device version to clear all notifications but do not need to check to reset badge count.

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()    
  }        

  //------
}
Ashbey answered 26/11, 2019 at 7:5 Comment(0)
H
0

Swift 4 & 5

import UserNotifications

...
...
...

UNUserNotificationCenter.current().removeAllDeliveredNotifications()
Hephaestus answered 8/8, 2020 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.