Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

auto-increment badges number when receiving push notifications from APNS

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.

like image 443
Fatima Avatar asked Aug 23 '13 07:08

Fatima


People also ask

Why are my app badges not showing numbers iPhone?

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).

What is notification badge count?

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.

What is iOS notification badge count?

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.

Are push notifications automatic?

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.


2 Answers

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.

like image 115
rckoenes Avatar answered Sep 18 '22 00:09

rckoenes


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;}
like image 26
wasim Avatar answered Sep 21 '22 00:09

wasim