Apple push notification not changing icon badge automatically
Asked Answered
C

3

13

I have seen the mail app in my iPhone (4S, iOS 5.1) automatically updates the badge count if new mail arrives, even when app is not running. So it is possible to achieve this behavior in my app also, right?

My application successfully registers for push notifications for all 3 types - Badge, Alert and Sound. Phone Settings is set ON for all 3 types of remote notifications for this application.

My app receives remote notifications and shows alert, plays sound, but it does not update the badge number. If I launch the app via the View button on the alert, then my app can read the badge value perfectly and I can change/remove/set the icon badge from code.

Any specific reason why iOS cannot change the icon badge of my app automatically when the notification arrives? I have seen all the similar posts, they are all discussing about either phone settings, or about notification types that it registered, or about checking the payload JSON includes badge or not.

Is there any other reason that might cause this problem?

Here is my code blocks:

Register for APNs every time app is launched -

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];

Device token received almost instantly. Token has been sent to provider, and I am receiving notifications. Alerts and Sounds works. But badge has no automatic effect until I launch the app and change it manually. Please help.

Candlewick answered 3/7, 2012 at 19:19 Comment(7)
is your badge set to +1 when you send the notification? or just 1?Furman
Out of curiosity, what are you using push notifications for?Unipod
set it to +1 or however many notifications you need to add.Furman
Actually it looks like that may only work on urbanairship. Other posts say you need to keep track of notifications server side or send the count to the server.Furman
@mkral, I will try but apple does not suggest to use +1 developer.apple.com/library/ios/#DOCUMENTATION/…Candlewick
I was wrong, you can do that on UrbanAirship but apple doesn't allow for it by default. You should keep track of the badge count on the server.Furman
If I keep the badge count at the server and then in turn send it to app again via APNs, it means the same. The badge will not change itself. I can only update via code. However, thanks for your answer. I will have to study a bit now...Candlewick
C
23

The issue is now fixed. The provider server was sending the badge number in quotation marks (as JSON String, i.e. "9"). Strange that APNs/iOS does not recognize it as integer. Now the quotes are removed and it works :)

Candlewick answered 6/7, 2012 at 14:15 Comment(0)
B
28

I also had this exact same problem. The badge icon was not quoted. After many hours of trying to tweak the JSON (after verifying app on phone showed badges, sounds, alerts) - the problem was I had added UIRemoteNotificationTypeNewsstandContentAvailability as one of the alert types my app was registering for. (I went crazy, just picking everything)

So, when UIRemoteNotificationTypeNewsstandContentAvailability is also in the mix, it seems to override the function of allowing aps/badge numbers to update an app icon. (Must be looking to update newsstand info)

Breed answered 26/11, 2012 at 19:52 Comment(5)
Thanks! "newsstand" turned out to be my issue too.Slobber
Thank you so a lot! spent half a day with this problem.Miki
Yup. Same exact problem and this fixed it. Thank you so very much.Rattlebrain
Thank you! I would spend a lot more time investigating without your tip!Gonzalo
Perfect, helped a lot!Karb
C
23

The issue is now fixed. The provider server was sending the badge number in quotation marks (as JSON String, i.e. "9"). Strange that APNs/iOS does not recognize it as integer. Now the quotes are removed and it works :)

Candlewick answered 6/7, 2012 at 14:15 Comment(0)
S
0

I had a similar problem, solved it by forcing integer in the badge number (using PHP with data coming from MySQL db):

$body['aps'] = array(
        'alert' => array(
        'action-loc-key'=> utf8_encode($r['button_action']),
        'body'          => utf8_encode($r['message']),
        'launch-image'  => ''
        ),
    'badge' => (int)$r['badge_number'],
    'sound' => 'default'
    );

Ps.: take a look at the utf8_encode command to avoid a json_encode error I faced earlier too.

Sorb answered 30/5, 2016 at 1:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.