How to find out the Icon Badge count? - ios

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

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

Remove single notification from notification center iOS

I want to implement same notification feature in my app just like in-built Mail application of iOS devices?
In Mail application single notification get removed out of many from notification center when user view it?
I have searched for same and got answer for removing all notifications by using
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
But I don't want to remove all at once, I want to implement it same as in-built Mail application. Can anybody help on this?
Try this code,
[UIApplication sharedApplication].applicationIconBadgeNumber = MAX([UIApplication sharedApplication].applicationIconBadgeNumber - 1, 0);

iOS Badge Number Doesn't Decrease - Parse

I am using parse to handle my push notifications. I send a notification to my app, with option "Increase Badge Number" selected. It sets the badge at 1. Then I call this in the app:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
This works as expected, clears badge number.
Then I send another push notification in parse with "Increase Badge Number" selected again. However this time the badge on the app shows 2. Again the code clears the badge, but I want it to show 1 at that point, am I missing some code? Or is this a parse issue?
You're clearing it in iOS, but you're not changing the value for the badge on the Installation object on Parse. So, if you just call increment, yeah the number will be bigger than you expect.
You could add this to your above example:
[[PFInstallation currentInstallation] setObject:#0 forKey:#"badge"];
[[PFInstallation currentInstallation] saveEventually];
So that the badge number gets cleared out on the Parse side and future increments will do what you expect.

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

iOS 7 Local notifications with no sound by default

When I run the app on my iOS 7 device, the app doesn't appear on the device notifications settings and doesn't sounds when a notification is being fires. Only after the first notification is being fired, I can see my app under the list of the notification settings with sounds turned off.
Why doesn't the app show in the notifications list initially?
Why are the sounds turned off by default?
On iOS 5&6 I don't have these problems. These are local notifications.
It looks like iOS 7.0.3 includes a fix for that.
Have you set the applicationIconBadgeNumber in "- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions"? Comment this code, and try again.....I don't why, but I got the same problem. After I commented this line, my app works correctly.
I got this same problem. What i found out was, i have a piece of code that caused this all along.
In my AppDelegate didFinishLaunchingWithOptions, i have implemented:
//remove this if you have it
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; //to reset the badge everytime the app loads
So what i did, I removed that code & redeploy the app. Now it has ON default value in Notification Center.
Phew.
Hmm,
Interestingly, I changed the order of
notification.SoundName = UILocalNotification.DefaultSoundName;
notification.ApplicationIconBadgeNumber = 1;
to
notification.ApplicationIconBadgeNumber = 1;
notification.SoundName = UILocalNotification.DefaultSoundName;
and it works now. When the app is running in background, the local notification fires and plays the default notification sound.

Resources