Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the remote notification in your app?

Tags:

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.

like image 301
Tedha Avatar asked May 21 '15 03:05

Tedha


People also ask

What are remote notifications in Android?

Remote Notifications are notifications sent to a mobile device using a data-channel from a service provider in real-time.

What is remote push notification?

Overview. Use remote notifications (also known as push notifications) to push small amounts of data to devices that use your app, even when your app isn't running. Apps use notifications to provide important information to users. For example, a messaging service sends remote notifications when new messages arrive.


1 Answers

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

like image 82
Naveed Ahmad Avatar answered Oct 07 '22 16:10

Naveed Ahmad