How to remove badges on springboard icon when application is relaunched? - iphone

I am developing an http message related application. In that application when I get a new message from the server, the message count will show on the springboard icon. After reading, the message count will decrease. Now the problem is that my messages count is 2. In that time I removed the application and installed it again. This time the badges count is showing removed application count like 2.
How do I remove the badge count when the application is installed newly?

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

in the app delegate class in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions function put this line of code
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
Hope this helps

Related

how to retrieve and handle remote push notification content in iOS app

I was trying to fetch remote notification info when the app was not running,so I was told that I can get from :
UILocalNotification *localNotification = [launchOptions
objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]
in method:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
,but still can't get info.
Then I use alertView to show the info on iPhone(launch without Xcode),still can't get the info.
Any other issue would cause this? Please let me know if you have any ideas.
How to retrieve and handle remote notifications:
app is running
The userInfo in below method already includes the push notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
}
app not running
The value for key in launchOptions includes the push notification,under two circumstance:
1.screen is locked,when receive the remote push notification,screen is lighted and user unlock the screen then directly launch the app.
2.user tap on the notification on the drop-down menu to launch the app.
If user tap on the app directly,then the notification will be gone and missed.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey] description];
}
That key is only in the launch options when the user starts your app from the notification (e.g. taps on it in notification center). Incidentally, I don't think a remote notification would be the class you're using (UILocalNotification).
If your app wasn't in the foreground when the device received the push, and the user didn't launch your app from the notification, the notification is gone. You have to check your own servers to see if you missed anything.

Clear push notification badge after increment

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.

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];
}

badge number count increament

I have implemented the local notification concept, it is working properly but there is a problem in badge number it is not increamenting automatically as notifications occurs. I have got a link where solution for this problem is given but don't know how to use it in app delegate. Following is the link... If someone know how to auto increament the badge number ,please provide me some solution.
https://github.com/csheldrick/UILocalNotification
Thanku very much.
When you create your local notification then before registring it with the system add this line of code.
localNotif.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber+ 1;
and then you register you notification with the system
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
localNotif is object of UILocalNotification
Hope this helps
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[UIApplication sharedApplication].applicationIconBadgeNumber=application.applicationIconBadgeNumber+1;
}
i hope it will help ful to you

setApplicationIconBadgeNumber when called multiple times does not update the badge

I am working on an application where in I have to update the badge shown in the app icon multiple times. However, what I have noted is that, the setApplicationIconBadgeNumber api just works once during the lifetime of the app. I have tries using the UILocalNotification, and it works then but, I dont want to follow that route. Have you guys faced a similar problem. If yes, any pointers?
Regards
Nitin
This is a bug in iOS. It's still present today in 6.0.1, where I just fixed it with a work-around:
// Clear app badge number. Work-around for bug in iOS.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
Might the problem be from where you are calling it?
Wrong:
// This is only called once during application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
Correct:
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}