Clearing badge number Appcelerator studio, arrowdb and iOS?

937 views Asked by At

So i send push notifications using the arrowdb and cloud service with Appcelerator. This seems to work well and the iphone gets the notification. What i wonder is if there is some easy way to clear the badge numer to 0? It needs to be reset at the arrowdb server as well (or apple) i guess.

I want the badge to be reset every time the app is opened.

What i did was adding a resume listener in alloy.js

Ti.App.addEventListener("resumed", function(e){

In the event listener i first do (to reset the badge locally):

Cloud.PushNotifications.resetBadge({
        device_token: device_token
    }, function (e) {
        if (e.success) {
            Ti.API.info('Badge Reset!');
        }
        else {
            Ti.API.error(e);
        }
    });

Then i also send a push to clear the badge on the server like this by sending a push to ourself:

var push_payload = {
        badge:0,
        sound:"",
        alert:"",
        title:"",
        message_text:"",
        push_type:"clear",
        from_user:Alloy.Globals.currentUser.id,
        vibrate:false
    };

    Cloud.PushNotifications.notify({
        channel: 'private_message',
        payload: push_payload,
        to_ids: Alloy.Globals.currentUser.id
    }, function (e) {
        if (e.success) {
            Ti.API.info('Success sending push notification');
        } else {
            Ti.API.info('Error sending push: '+((e.error && e.message) || JSON.stringify(e)));
        }
    });

The event listener seems to be called, and the push is sent. But somehow the badge still remains on the iphone device... Should I do this differently?

3

There are 3 answers

7
Juan Carlos Salinas Ojeda On

Did you try this ?

Titanium.UI.iOS.appBadge = 0
2
Patrick De Marta On

to clean the badge number, in a project I did some times ago, I successfully used

Ti.UI.iPhone.setAppBadge(0);

Hope it will work for you too.

1
Claudio On

For Android use: Ti.Android.NotificationManager.cancelAll()

This command clears the notification and badge