0

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?

Emil
  • 137
  • 1
  • 10

3 Answers3

4

Did you try this ?

Titanium.UI.iOS.appBadge = 0
  • It seems we've been answering almost at same time, sorry Juan, I didn't mean to give just another slightly different solution! :) – Patrick De Marta Jun 06 '16 at 20:56
  • jajaja, looks like but there are no problem..as long as the answer can help @Emil – Juan Carlos Salinas Ojeda Jun 06 '16 at 21:30
  • Actually your is correct! As in the latest docs, setAppBadge is in Titanium.UI.iOS, no more in Titanium.UI.iPhone (I checked now and I've been using it in a quite old project on 3.5.2 SDK) – Patrick De Marta Jun 06 '16 at 21:38
  • But this just sets it locally? The badge number will remain on servers? Also need the same for Android eventually I guess, anyone have a solution for that too? – Emil Jun 07 '16 at 06:06
  • `Titanium.UI.iOS.setAppBadge()` sets the badge value locally; if the argument is 0 the badge will disappear from your app icon. When the app will receive a new notification along with a new badge value, this value will be set as the new badge number. Instead Android does not natively use similar badge numbers on the app icon. – Patrick De Marta Jun 07 '16 at 08:03
  • I tried this now too. The badge keeps being "1" for the app icon. Seems it does not reset for some reason... – Emil Jun 07 '16 at 08:06
  • I see, I remember being somehow confused too while attempting to clean the app badge locally. While I'm aware of what the documentation for Cloud.PushNotifications.resetBadge states, I couldn't have it working that way. In my app I had a section where users get custom made notification messages (i.e. about being followed, or some of their content liked by other users), so I went to clean app badge when that particular view got focus, in the callback of a "focus" eventListener I added to the view's controller. It sounds tricky, and it actually is, but I got to clean the badge in this way. Hth. – Patrick De Marta Jun 07 '16 at 12:07
0

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.

0

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

This command clears the notification and badge

static_cast
  • 1,174
  • 1
  • 15
  • 21
Claudio
  • 1
  • 1