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

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

Related

Programmatically delete Remote Notifications from Notification Centre

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

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

Handling user acting on UILocalNotification

My goal is to open a specific screen when user clicks on a UILocalNotification from the iOS Notification Center.
Now if the app is being resumed from the background when user acts on the Notification, I notice didReceiveLocalNotification is called:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
Now I can get the userInfo dictionary from the UILocalNotification object and opens the proper screen.
Unfortunately, this same method is called also when my app publishes a UILocationNotification while the app is running in the foreground:
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
How would I distinguish between the 2 cases?
It passes to you the application object that contains the state in its .applicationState property.
Consequently, you can make sure it is not active by testing...
if (application.applicationState != UIApplicationStateActive){
// do your stuff
}
And that's it!

How to stop alert notifications in (ios 7) objective c

I have implemented remote notification in objective-c . But my problem is I want to stop alert notification .Blow my code which I am using
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
IRemoteNotificationTypeSound ];
but I still getting banner notification . and didreceiveremotenotificationmethod is successfully called
but when I change the code to
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
IRemoteNotificationTypenone ];
Then I receive no alert, no sound, and also didreceiveremotenotification not called. I want that app not show alert banner. But it must call didreceiveremotenotification in background mode, because in didreceiveremotenotification I am receiving necessary data
.

Showing and deleting number on badge depending upon the Apple Push Notifications

I have an app where I am using apple push notifications. Now I need to show the badge number depending upon the notifications, and I also need to decrease the badge number accordingly.
I know this method is used for showing badge, but I am getting how to implement it.
[UIApplication sharedApplication].applicationIconBadgeNumber = badgeNumer;
Can anyone help me with this?
You can use your code inside this method to show badge number.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
It will show until you set it to zero and you can do it with the following code:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]
It is more common to set the badge number as you receive the notification, in either application:didReceiveRemoteNotification: or application:didFinishLaunchingWithOptions: methods of your UIApplicationDelegate class.
You can read more about it in the Local and Push Notification Programming Guide

Resources