Programmatically delete Remote Notifications from Notification Centre - ios

How can we pragmatically remove any pending remote notifications sent for my app from notification centre. I want to clear them up on app launch.
I have tried with [[UIApplication sharedApplication] cancelAllLocalNotifications]; API but its not helping.
PS: This question is specific to iOS 10 and old threads are not duplicates for this one.

Finally...
This one works like charm!
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];

You can clear all notifications from notification centre by using these simple lines of code
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
use wherever you want. From my side I have used when user pressed logout.
You can use in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method to clear notifications after app opens

AFAIK, you should do it by using the background mode for remote notifications, and then responding to these notifications by issuing a local notifications.
You can remove local notifications, but not remote notifications.

Reseting the application badge number also remove all the notifications (local & remote) from the notification center.
Objective C
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
Swift
UIApplication.shared.applicationIconBadgeNumber = 0

This can definitely be achieved by using removeDeliveredNotifications(withIdentifiers:) method available in UserNotifications.framework.
For a detailed tutorial, please follow this
https://medium.com/#sebastianosiski/implementing-removable-remote-notifications-on-ios-a17d74832bde

Related

objective c Push notification BackGround remove old one when receive new one in Notification center

I created a application. That receives push notifications from server.
This all works for me.
But the only thing is that i want that my old push notification in notification center will be removed when i receive a new one.
What do i need to do in this function
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo {
This doesn't solve my problem
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
doesn't work
It is for Background(when you are home screen or locked your phone).
Or is it possible to replace your old push notification for new one

How to find out the Icon Badge count?

I'm using push notifications on my iOS app and everything is working great.
The icon badge is working and when I open the app I clear it with this code:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
But before clearing it I would like to find out what is the badge count to make some changes on the app according to the number of notifications the user has when opening the app.
Is it possible to do get this number?
Thanks!
[UIApplication sharedApplication].applicationIconBadgeNumber
Try

How to clear notification in didreceiveremoteNotification method when app running in background?

I need to clear the notification in notification center , when i click notification in notification center or badge when app in background. I can able to get log in didreceiveremoteNotification . But not clearing the notification in notification center.
you could try these two lines which i am always using when receiving a notification
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
setApplicationIconBadgeNumber is a multipurpose property to set the badge number and also clear the notifications
If I understand you correctly, you are trying to clear the badge of the push notification.
If so, use this code in
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
applicationDidBecomeActive

How stop local notification sound after cancel local notification

In my application I scheduled local notification with custom sound. After I cancel it from code the sound continue playing. How can I stop it?
-(void)cancelNotification{
if(localNotification){
[[UIApplication sharedApplication] cancelLocalNotification:localNotification];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
}
Try changing your code like below, hope this works for you. I am adding this just for a try.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[[UIApplication sharedApplication] cancelLocalNotification:notification] ;
}
In didRecieveRemoteNotification set your application icon badge number. It will do the trick.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
}
I had the same problem when initially testing with an iPhone 4.
The problem appears to have gone away as of iOS 8 and now Local Notification Sounds stop when canceled after transitioning to the App.
iPhone4 [ iOS - 7.1.2 ] - Local Notification sound keeps playing in App no matter what
iPhone6 [ iOS - 8.1.1 ] - Local Notification sound stop when canceled programmatically
From what I can infer, it appears a fix exists somewhere in iOS 8.x
(Alas I didn't manage to find any documentation or release notes on the matter.)
The Problem becomes a non-issue for apps focusing on iOS 8
(provided you cancel the Local Notification coming in)
I used this same answer for a Similar Problem

Remove single remote notification from Notification Center

my app receives remote notification from Apple server.
Is there a way to remove a single remote notification from notification center (the drop-down menu available from iOs 5.0+), when the user taps on it?
Thanks!
There is no way to remove a specific notification as of iOS SDK 5.0. The way to remove all the notifications from your app so they don't show in the Notification Center when the user opens the app from one of them, is to set the app badge to 0, like this:
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
EDIT: on iOS 8, SpringBoard seems to be automatically dismissing a notification when you tap on it on the Notification Center to open the app.
Here is a suggestion, though it does have its flaws, and I haven't tried it myself:
Push a silent notification (contentAvailable:true), don't include a "alert" inside the push, place the alert text in a custom property of the push
Handle the incoming push and trigger a local notification, display it immediately
If the user clicks the local notification, use the [UIApplication cancelLocalNotification:] which should remove the notification from the notification center.
When you call the method:
[application cancelAllLocalNotifications];
inside the AppDelegate methods:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
and
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
All Local and Push Notifications will be remove on that for the particular app.

Resources