How to handle App Icon Badge Count on receiving push notification? - ios

I want to increase the badge count when a notification is received and decrease when user tap or open the app.
I also try this code but it doesn't work.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
}

As per my knowledge application is showing number as badge count which is sent by you in this { "aps" : { "badge" : 9 } } dictionary as badge value.
What you are setting in this method
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
}
is set when you open application by tapping on remote notification which is logically not write because when you are opening application that time you are setting badge count.
You must have to pass badge value in your aps dictionary and you may reset badge count when application opened (in didFinishLaunchingwithOption method).
Apart from this, there is no method you can write in your application code that set badge count as soon as you receive push notification and your application is already in kill mode(not open at all).

Related

iOS silent push should update application badge count

Is it possible to update application badge count by receiving a silent push.
When the application is not running in the background, this method is not called.
My guess is this method get called even if the app is not running in the background. Am I wrong?
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
int currentBadge = [UIApplication sharedApplication].applicationIconBadgeNumber;
DLog(#"%#, badge = %i", userInfo, currentBadge);
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
completionHandler(UIBackgroundFetchResultNoData);
}
I just want to increase badge number with any received notification. The notification payload do not have a "badge' field.
Is it possible?
And if the app does not run background, UIApplicationExitsOnSuspend = YES, I wonder in this situation will silent push work or not?

Tap on local notification iOS

First I want to know, which specific method is called when i tap on local notification. I want to open a url upon tap on notification. below is code app delegate.
Now the issue is, url opens automatically even if i don't tap on notification. Please guide me if u know that. Thank you
- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notifyAlarm
{
application.applicationIconBadgeNumber = 0;
NSLog(#"Notification tapped :) ");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com.pk"]]; }
- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notifyAlarm
This methos is called every time when notification fire.
To open url when tap on notification you have to check state of the app.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState appState = UIApplicationStateActive;
if ([application respondsToSelector:#selector(applicationState)])
appState = application.applicationState;
if (appState == UIApplicationStateActive)
{
// Don't open Url.
}
else
{
// Open Url.
}
}
Have a look at the Local and Remote Notification Programming Guide from Apple, section "Handling Local and Remote Notifications".
If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).
Later is says
[...] get the value of the applicationState property and evaluate it. If the value is UIApplicationStateInactive, the user tapped the action button; if the value is UIApplicationStateActive, the application was frontmost when it received the notification.
Try to check application.applicationState. If the app isn't active in the foreground , then not open URL.

How to handle user clicked local notification

I am using local notification in my app, the only thing i care about is what exact notification the user clicked.
the method
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
triggered when the notification is received but i need to handle the notification when the user clicked on it so this is useless to me.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
triggered only when app was not running, also not what i need
i searched the net but found only handles if the app is active or not running. how can i handle it when the app is on the background?
what i need in other words is to recognise the exact notification that the user clicked when the app was running on the background
When creating your UILocalNotification, you can set the userInfo to set any associated data/unique identifiers.
Ex.
UILocalNotification *someNotification = [[UILocalNotification alloc] init];
[someNotification setUserInfo:#{ kSomeUniqueIdentifierKey : #"identifier" }];
and then,
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([notification.userInfo[kSomeUniqueIdentifierKey] isEqualToString:#"identifier"]) {
// We know what notification was responded to based on userInfo
}
}
The above method fires either immediately upon receiving the notification while the app was running or when the user taps the notification that fired while your app was in the background.
If you want to ignore these notifications while the app is running, you could always check the state of the application to determine if it's responding to the notification while running or in the background.
I am working with iOS 9, and the solution is to check the launchOptions within the AppDelegate didFinishLaunchingWithOptions. As follows:
// Were we launched from a local notification?
if let lo = launchOptions
{
if let ln = lo[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification
{
// Do our thing...
}
}

ios - local notification not updating badge number when application is closed

I have noticed that when a local notification is being received in an ios device, the notification appears in the Notification Center but the app badge number is not updated when the app is closed.
I need to touch the notification in the Notification Center for the local push message to be transferred to the app.
Is this the normal behavior? Can this be solved by using remote push notifications?
You can utilize the applicationIconBadgeNumber parameter in a UILocalNotification object.
Basically:
localNotificationObject.applicationIconBadgeNumber++;
Example:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:20];
localNotification.alertBody = #"Some Alert";
//the following line is important to set badge number
localNotification.applicationIconBadgeNumber++;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
But the issue with this is that the badge number doesn't increment on subsequent (multiple) local notifications (there's a scenario here but for simplicity sake, lets just say the badge stays 1 even after 2 or more, back to back, local notifications).
In this case, Yes... Push Notification seems to be the way to go
(but be aware that Push Notifications aren't always reliable... check: link)
Well... to use Push Notifications for proper badge number updates, you should know that you can send a badge count in the Push Notification's payload.
When this push notification is received, the badge count is changed by iOS to the badge count specified in the Push Notification (& the app need not be open for this).
Example (continued):
Set applicationIconBadgeNumber to 0 as it helps in certain scenarios (optional)
- (void)applicationWillResignActive:(UIApplication *)application {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
Extra:
You can also manually set the badge number when you terminate/close or resign the application.
Generally... in any or all of the following methods:
-applicationWillResignActive
-applicationDidEnterBackground
-applicationWillTerminate (set badgeNumber when app closes)
Example:
- (void)applicationWillResignActive:(UIApplication *)application {
//Called when the application is about to move from active to inactive state.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[[UIApplication sharedApplication] scheduledLocalNotifications] count]];
//...
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[[UIApplication sharedApplication] scheduledLocalNotifications] count]];
//...
}
iPhone: Incrementing the application badge through a local notification
It is not possible to update dynamically the badge number with local notifications while your app is in the background. so You have to use push notifications. You can only increment badge while application is running in foreground and look for alternative solution you can go with here
iPhone: Incrementing the application badge through a local notification

How can I tell if a local notification was fired while the application was in the background?

I need to run specific code if a local notification was fired while the application was in the background and has now entered the foreground. One way to do this is to get the badge count, is there a better way?
Check out the docs at http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html
Your App Delegate can detect notifications when the app is in the background
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
NSString *itemName = [notif.userInfo objectForKey:ToDoItemKey]
[viewController displayItem:itemName]; // custom method
application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1;
}

Resources