I would like to let the badge number increasing automatically when receiving push notifications, for example: if I receive two notifications let the number displays as 2, if I receive more, the number increases. And I want to let the number returns zero when I open the application. So what I am doing is the following: the code in application did finish launching
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
otherwise, the code in did Receive Remote Notification is
application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];
In the server, in the php file:
$payload = '{"aps": {"alert":"'.$message.'", "sound":"default","badge":"+1"}}';
But, unfortunately, the number doesn't incremented, please correct me if the code above is wrong, thank you per advance.
Check System Settings Go to System Settings → Notifications. Find Things in the list of apps. Toggle on Allow Notifications (if it's on, turn it off and back on). Toggle on Badge application icon (if it's on, toggle it off and on again).
The app icon badge tells you there's a new notification or message waiting for you when you open the app, and the badge count tells you how many notifications there are.
The iOS badge count displays the number of unread notifications within your application, taking the form of a red circle in the upper-right hand corner of the app icon. In recent years, badging has come to be an effective means for re-engaging app users.
Definition: A push notification is an automated message that is sent to the user by the server of the app that's working in the background (i.e., not open.) Another way to describe it is a push notification is a message that's displayed outside of the app.
There is not option in push notifications to auto-increment the badges number. You will have to pass the number that will be shown on the badge.
Also the application:didReceiveRemoteNotification:
will only be called if the app is running foreground. Here you could calculate the new badge number, but this will not help you if you app is not in the foreground.
You will need to track the badge number server side as well.
you can do something like following
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo {
NSLog(@"userInfo:%@",userInfo);
badge_value+=[[[userInfo objectForKey:@"aps"] objectForKey:@"badge"]intValue];<br>
NSLog(@"Totoal badge Value:%d",badge_value);<br>
for (id key in userInfo) {
NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
}
[UIApplication sharedApplication].applicationIconBadgeNumber = badge_value;}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With