Clear push notification badge after increment - iphone

I am working on push notification in iphone. when i receive push notification, its showing 1 on my application icon, next time its 2,3,4. if i open application its 0. Next time its should be 1,2,3,4... but its showing last number and +1. i want reset push notification badge after open application. i am sending +1 from urban airship.
and its not working for me.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

I use this code in my app because the Urban Airship (UA) documentation said to
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
[[UAPush shared] resetBadge];
but it doesn't work, the badge on the app icon keeps incrementing. I saw a few posts on this very issue on UA's forums and they haven't given a clear answer.
EDIT #1:
I received the following response from a support technician at UA with the following suggestions, which worked great:
What you want to do, is ensure that in your didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method, you are calling the following:
[[UAPush shared] setAutobadgeEnabled:YES];
[[UAPush shared] resetBadge];//zero badge on startup
and also call [[UAPush shared] resetBadge]; in the following methods as well:
applicationDidBecomeActive:(UIApplication *)application
and
didReceiveRemoteNotification:(NSDictionary *)userInfo
The technician also mentioned that assigning 0 to applicationIconBadgeNumber is not necessary, so I took it out. Still works beautifully.
EDIT #2:
I ended up having to modify application:didReceiveRemoteNotification: to include a call to UA's handleNotification:applicationState: method:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// Get application state for iOS4.x+ devices, otherwise assume active
UIApplicationState appState = UIApplicationStateActive;
if ([application respondsToSelector:#selector(applicationState)])
{
appState = application.applicationState;
}
[[UAPush shared] handleNotification:userInfo applicationState:appState];
[[UAPush shared] resetBadge];
}
because I was having a problem with the following scenario:
User is in app
Push notification received
No badge was displayed on app icon when returning to home screen (as expected)
Another push notification is received
Badge displayed number greater than 1
With the modification above, this scenario is handled. I guess you have to tell UA that the notification is handled when one is received and the app is running in the foreground.

I have used Urban Airship and have had this problem before. You are not showing the code but i am assuming that when a notification is received, you are setting the application badge number to what urban airship is passing to you, don't do that. Just let the application self handle this, when a remote notification comes in, let it auto increment on its own. If that is not the case it could be that, on the urban airship side, you are setting a badge number to send with the push. Do not send a badge number with the push notification, leave that part out, iOS should auto increment your badge from its current badge number.

Related

iOS Push notification issue

I am doing a project in which push notification feature is one of the key feature.
It is working fine when I am in the app, I receive notification and handle that notification.
But the issue is when I am in background and notification receives I see badge on my app icon
and when I click on the icon my app is launching but the didReceiveRemoteNotification method is not called so I am unable to handle that notification.
And another issue is some times it shows the notification message in device notification list and some times it didn't .
When I am entering in my app through clicking on notification list item the didReceiveRemoteNotification calls and I am successfully able to handle notification.
I write following code in didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method
NSDictionary* remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif != nil)
{
NSLog(#"didFinishLaunchingWithOptions\nNotification recieved:\n%#",remoteNotif);
notificationData=[[NSDictionary alloc]initWithDictionary:remoteNotif];
[notif saveNotification:remoteNotif];
}
Help me to resolve this .
Thanks in advance.
Things which you are doing in didfinishlaunch method do in didReceiveRemoteNotification as well.
When you will come from background, didfinishlaunch won't be called.
- (void)application:(UIApplication*)application didReceiveRemoteNotification:
(NSDictionary*)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
//What you want to do when your app was active and it got push notification
}
else if (state == UIApplicationStateInactive)
{
//What you want to do when your app was in background and it got push notification
}
}
From Apple's Local And Push Notification programming guide
Handling Local and Remote Notifications
Let’s review the possible scenarios when the system delivers a local
notification or a remote notification for an application.
The notification is delivered when the application isn’t running in the foreground.
In this case, the system presents the notification, displaying an
alert, badging an icon, perhaps playing a sound.
As a result of the presented notification, the user taps the action
button of the alert or taps (or clicks) the application icon.
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).
If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information
about the notification . If the application icon is clicked on a
computer running OS X, the application calls the delegate’s
applicationDidFinishLaunching: method in which the delegate can
obtain the remote-notification payload.
The notification is delivered when the application is running in the foreground.
The application calls its delegate’s
application:didReceiveRemoteNotification: method (for remote
notifications) or application:didReceiveLocalNotification: method
(for local notifications) and passes in the notification payload or
the local-notification object.
So in your case, when application is running in background, and when you click the notification/alert, operating system brings your app into foreground. So it falls under second point.
You can implement application:didReceiveRemoteNotification: method to get the notification payload, if action button is tapped. But when the application icon is pressed instead of action message, the notification payload is not forwarded with the method. Your only option is to contact your server and sync the data. After all as per Apple's policy, push notification only tells that there is data on server, and either way you need to connect to server and get the data.
I did the same thing in one of my application. There is no way to handle notification when clicked on app icon. So, what you can do is to make a server call to get latestPushNotificationIdSync.
You must be storing your data somewhere on server so, you need to check on your server what is the latest latestPushNotificationId.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
notificationContentDict = launchOptions;
if([[launchOptions valueForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"]count]>0){
application.applicationIconBadgeNumber = 0;
NSString *key = [[launchOptions valueForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"] valueForKey:#"id"];
contentId = key;
////// you can use this content id ///// write your code here as per the requirement ..////
//// display your content on UI /// either get from server or local ...///
[self displayContent:[launchOptions valueForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"] application:application];
}
else
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(#"badge count ..%d", application.applicationIconBadgeNumber);
if( application.applicationIconBadgeNumber >0){
if(!notificationContentDict){
make a server call to get "latestPushNotificationIdSync"
application.applicationIconBadgeNumber = 0;
NSLog(#"badge count applicationDidBecomeActive.%d", application.applicationIconBadgeNumber);
}
}
}

iOS Push Notifications - update badge without alert?

Is there a way to update the number in the badge without showing an alert or opening the app?
I am writing an app that should always display the current number of unread messages in the icon badge, but I want to do so without displaying any alerts to the user.
I am developing for iOS 5.0+.
EDIT: To be more clear, I am asking about a way to do this when the app is not running. I want the server to push a new badge number without showing an alert.. Is this possible?
You can do it.
It is possible to send a push notification without an alert.
You can even register your application just to badge notifications, in which case the provider server won't even be able to send alerts or sounds.
The Notification Payload
Each push notification carries with it a payload. The payload
specifies how users are to be alerted to the data waiting to be
downloaded to the client application. The maximum size allowed for a
notification payload is 256 bytes; Apple Push Notification Service
refuses any notification that exceeds this limit. Remember that
delivery of notifications is “best effort” and is not guaranteed.
For each notification, providers must compose a JSON dictionary object
that strictly adheres to RFC 4627. This dictionary must contain
another dictionary identified by the key aps. The aps dictionary
contains one or more properties that specify the following actions:
An alert message to display to the user
A number to badge the application icon with
A sound to play
Note that it says one or more of the properties. The alert property is optional. You can even send a notification with an empty aps dictionary (i.e. send only custom properties).
Example 5. The following example shows an empty aps dictionary;
because the badge property is missing, any current badge number shown
on the application icon is removed. The acme2 custom property is an
array of two integers.
{
"aps" : {
},
"acme2" : [ 5, 8 ]
}
The only alert the user will see it the alert that asks him/her whether to allow push notifications. That alert will only be displayed the first time the app is launched after installation.
In this example you register to non alert notifications (badges and sounds only) :
Listing 2-3 Registering for remote notifications
- (void)applicationDidFinishLaunching:(UIApplication *)app {
// other setup tasks here....
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}
// Delegation methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
const void *devTokenBytes = [devToken bytes];
self.registered = YES;
[self sendProviderDeviceToken:devTokenBytes]; // custom method
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(#"Error in registration. Error: %#", err);
}
All quotes are taken from the Apple Local and Push notifications programming guide.
you should use applicationIconBadgeNumber for locally handling your app badge number
[UIApplication sharedApplication].applicationIconBadgeNumber = number_of_notifications;
I don't think it is possible to do without alert as far as adding badge counter from remote notification. You should read about APN Service, in your case you might register for UIRemoteNotificationTypeBadge you should read about Local & Push Notification Programming guide
You can use
[UIApplication sharedApplication].applicationIconBadgeNumber = aNumber;
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
use this method....this will help u.

iOS Push Notification - How to get the notification data when you click on the app icon instead of notification

Similar to this question: How do I access remote push notification data on applicationDidBecomeActive?
But the different is how can you access the notification data when you are inapplicationDidBecomeActive and if you have clicked on the app icon instead of the push notification.
The flow is: If you click on the push notification then didReceiveRemoteNotification will be triggered, but if you click on the original app icon, only applicationDidBecomeActive will be triggered and didReceiveRemoteNotification will not be called.
I am looking for the later case so how can I access the push notification data.
(Both case assuming the app is in background and not killed yet.)
You can't get remote push payload by launching app from homescreen.
If the push data is important for app use, load it from your server after app launched.
#fannheyward answer is absolutely correct. You cannot get payload when application is launched by tapping app icon.
I have an idea, what if you get to know that some notification is pending when app is launched by tapping app icon. With this knowledge your app can fetch payload from your server.
You can set "Badge" in every such notification and on applicationDidBecomeActive you can check [application applicationIconBadgeNumber] > 0 to know that some notification is active. After fetching payload from your server you can set it to 0 like below
[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
Please note: This means your app will have badge displayed over it when notification is received. I am not sure of the behaviour when badge is disabled by user from settings.
If your application target is over iOS7, you can do only if application is alive in backgroud.
In the capabilities settings at Xcode, you should enable Background Modes>Remote notifications, and write below code.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// save userInfo in NSUserDefaults
completionHandler( UIBackgroundFetchResultNoData );
}
If you want to test it, it will be helpful to use https://github.com/acoomans/SimulatorRemoteNotifications
From the server side, make sure to set content-available property with a value of 1
For this to work I also had to check the background fetch box.
You should get the notification in the launchWithOptions method in your appDelegate something like this:
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteNotif != nil){
//Handle your notification
}

get push notification while the app is not running iPhone

I am working on one iPhone app which involves a push notification. As I have seen in many documents and tutorials it suggests to register for push notification with in
application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
like the following:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert| UIRemoteNotificationTypeBadge| UIRemoteNotificationTypeSound];
...
}
now the question is, if the app was not running (even in background), when the push come, it cant process the push message, but if I use the push message again from the notification area and lunch the app again, I can get my message.
what I need to do to make my app get the push message even when it lunch for the first time?
You might be conflating the notion of registering for and receiving notifications. It is impossible for an app to receive a push notification before the registerForRemoteNotificationTypes: method is called the first time, since this method provides the push token that is used to send push notifications in the first place.
So, you must be talking about receiving notifications under the two separate situations in which they can be delivered: upon initial app launch, and during the execution of the program.
In order to handle notifications of the first type, you must inspect the options dictionary sent to application:didFinishLaunchingWithOptions:. The following code shows how to route a notification received at launch to the delegate method that is called when a push notification arrives while the app is already running.
Place this in your application:didFinishLaunchingWithOptions: override:
NSDictionary *pushNotificationPayload = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(pushNotificationPayload) {
[self application:application didReceiveRemoteNotification:pushNotificationPayload];
}

change Badge and push notification in iPhone SDK

I tried the push notification tutorial . It's working fine but problem is badge. When I click on the view, app is appear and the close it. it still red badge in app icon. How to remove it ?
Another question is
when I click the view, it will appear home screen. I want to show other view when coming from push notification.
This will reset the application badge number. If you set that value to zero, it will hide the badge.
[UIApplication sharedApplication].applicationIconBadgeNumber = iCount;
To handle your push notification with a separate view, you need to handle the following message in your application delegate:
- (void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
You can access the userInfo dictionary to get additional information about the push notification that resulted in the message callback.