Reducing badge number with Push notifications - iphone

Hi in my application when i receive push notification from server my application badge get incremented by one.and when i open app and close it did not get reduce.so my question is how to reduce badge icon on application icon when user see the notification

You can set it to any value you like. Setting it to 0 removes the badge.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

I had this same issue, so I thought, "Why not just GET the badge number?" Then, you can do whatever you wish with it.
If you are in your AppDelegate.m file, you could use this in your application: didFinishLaunchingWithOptions: method:
int badgeNumber = [application applicationIconBadgeNumber] -1;
[application setApplicationIconBadgeNumber:badgeNumber];
This will reduce the icon badge number by one. However, be mindful that your user may have multiple notifications from your application that are still not yet viewed. Thus, you should probably set this inside of a particular method that will handle the pushes. Or, depending how your app is set up, just set the badge icon number to 0.
Hope this helps.

Related

Remove LocalNotification Badge Number from launcher icon

I am trying to add a feature in app in which whenever i made a note, it will Call a UILocalNotification after few days (Hardcoded). Now, on launcher Icons i can see a red badge with marker "1". How can I remove this badge?
Best Regards
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
Add that line in whatever code handles the user's interaction with the local notification. If you want the badge to be cleared when the local notification fires, you can't (because setting the notification's applicationIconBadgeNumber property to 0 would simply mean "don't change the existing badge.")

how to remove badge notification symbol from app icon in iPhone

I am facing a problem with removing the badge number which always shows a red "1" on app icon notification symbol where there is no notification pending.
How can I solve it?
Use the below in applicationDidBecomeActive, some any methods in app life cycle..
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
When it is set to zero it should not show any badges.

banner badges iPhone

Is there a way to have the banners/badges for my app only update to 1? For example, if I have three notifications/updates for my app, instead of showing the launch icon on the homepage with the number 3, could I just show the number 1?
I guess in essence, I'm asking can I set the max of a badge for the app I am developing for the iPhone?
You set the application badge yourself, so you can determine the algorithm for the number you display. So if you want to just set the badge to 1 if there are any messages, but no badge otherwise, just use something like this:
- (void)applicationWillResignActive:(UIApplication *)application {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:(numberOfMessages > 0) ? 1 : 0];
}

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
}

Badges, Update item number?

Is it possible to update the badge number without sending the application a push notification?
Such as when the application is loaded you can change the badge number?
Yes. You can use this:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:3];
This will update the value without using a push notification.