Change the app badge when receiving a push notification in background

You must Login before you can answer or comment on any questions.

Hi all, I have a little problem here, I want to modify the app badge on my iphone titanium based application. I was able to do it when the app is focused (foreground), but when I do it in backgound ... it won't do it I do send in the json of the push notification the alert message, the sound and the badge number... it ignores the badge number the push notification work fine...except badge

my push notification structure:

{"aps":{"badge":"3","alert":"help","sound":"sound.caf"}}
My event :
Titanium.Network.registerForPushNotifications({
    types: [
        Titanium.Network.NOTIFICATION_TYPE_BADGE,
        Titanium.Network.NOTIFICATION_TYPE_ALERT,
        Titanium.Network.NOTIFICATION_TYPE_SOUND
    ],
    success:function(e)
    {
    },
    error:function(e)
    {
      //  label.text = "Error during registration: "+e.error;
    },
    callback:function(e)
    {
 
    }
    Titanium.UI.iPhone.appBadge = e.data.badge;
 
        Ti.Media.vibrate();
 
    }
});

— asked 9 months ago by Cristian Alexandru
5 Comments
  • my code is in the callckck.. and it doesn't change the badge when in background

    Titanium.Network.registerForPushNotifications({
        types: [
            Titanium.Network.NOTIFICATION_TYPE_BADGE,
            Titanium.Network.NOTIFICATION_TYPE_ALERT,
            Titanium.Network.NOTIFICATION_TYPE_SOUND
        ],
        success:function(e)
        {
        },
        error:function(e)
        {
          //  label.text = "Error during registration: "+e.error;
        },
        callback:function(e)
        {
     
        }
        Titanium.UI.iPhone.appBadge = e.data.badge;
     
            Ti.Media.vibrate();
     
        }
    });

    — commented 9 months ago by Cristian Alexandru

  • callback:function(e)
        {
        Titanium.UI.iPhone.appBadge = e.data.badge;
     
            Ti.Media.vibrate();
        }

    — commented 9 months ago by Cristian Alexandru

  • And you're sure the callback is being called? Does the device vibrate? Can you dump the contents of e to the log and post as well?

    — commented 9 months ago by Anthony Decena

  • Show 2 more comments

3 Answers

Solved! the badge needs to be numeric,

//php code
$body['aps']['badge'] = (int)$badge;
$body['aps']['alert'] =  $message;
$body['aps']['sound'] = $sound;
 
$payload = json_encode($body);
so.. unless you don't do in your server part of the code (i am using php)

it won't work

When in background the callback isn't called at all, you need to figure out the values from the server implementation

— answered 9 months ago by Cristian Alexandru
answer permalink
1 Comment
  • In my search for finding how to increment the badge count when the app is closed or in the background has finally come to end. I tried what Cristian suggested for passing the badge count and it worked like a charm! My app badge now displays the count when a push notification is received.

    Cheers!

    — commented 2 months ago by Tracy Hayman

Look at your app badge code. It is using the e object to access the badge number, but the appBadge set is NOT in a callback. You need to set the appBadge inside one of the callbacks in order to use the e.data.badge property. Otherwise e doesn't even exist.

— answered 9 months ago by Anthony Decena
answer permalink
1 Comment
  • sorry about that.. the code is actually inside the callback block, it still doesn't do anything.. i can even set a random value, it won't change the badge

    — commented 9 months ago by Cristian Alexandru

Ok, Im putting this in a separate answer as to not confuse it with previous entries. Can you try deleting the app from your device and reinstalling. I read a Jira ticket where this happened in a dev environment and the issue was resolved by removing the app from device.

Your Answer

Think you can help? Login to answer this question!