Joseph Francis
Posts: 0
Joined: Thu Sep 04, 2014 8:52 pm

setApplicationIconBadgeNumber not resetting

I have followed code hinted at in the support site here to the letter in my application, but I do not seem to be able to reset the badge count.

myAlert(data.aps.alert, "Notification", "OK"); /* handle notification /
myAlert("notification clearing badges"); / debug only /
var pushNotification = window.plugins.pushNotification; / per specification /
pushNotification.setApplicationIconBadgeNumber(null, null, 0); / per cordova spec */

furthermore, if I try to reset the badge on a "device ready" event, the "pushNotification" object is undefined:

myAlert("device ready clearing badges");
var pushNotification = window.plugins.pushNotification;
try {
pushNotification.setApplicationIconBadgeNumber(function(){myAlert("Badge Cleared");},
function(){myAlert("Badge Cleared Error");}, 0);
}
catch(err) {
myAlert(err.message);
}

this gives me simply an

"undefined is not an object (evaluationg '|.setApplicationIconBadgeNumber')"

error on my phone. the object "pushNotification" would not seem to exist on the plugins list.

Any help from the team - ?

Joseph Francis
Posts: 0
Joined: Thu Sep 04, 2014 8:52 pm

setApplicationIconBadgeNumber not resetting

Kateryna, with all due respect, I don't need a redirect to a past post, I've read all of them, and you and the rest of staff (example, Evgene, and Oleg) give conflicting answers. I need a definitive answer on how to use the function. No prior answer works.

None work for my application, an I have an extremely simple need.
On "Device Ready" trigger I execute the following javascript, which is referenced by no fewer than five posts:

---

var pushNotification = window.plugins.pushNotification;
pushNotification.setApplicationIconBadgeNumber(0, function(){});

---

Nothing happens of course (I don't need to give a list of postings where other people say exactly the same thing.)

You have said in a previous post

var pushNotification = window.plugins.pushNotification;
pushNotification.setApplicationIconBadgeNumber(successHandler, 0);

===

this cannot work because it doesn't match the definition of the plugin.

Let's look at the plugin:

// Call this to set the application icon badgePushNotification.prototype.setApplicationIconBadgeNumber = function(badge, callback) {
cordova.exec(callback, callback, "PushNotification", "setApplicationIconBadgeNumber", [{badge: badge}]);
};

therefore you must call the function with the badge first, then the callback.

===

I create a function triggered on callback to see what is going on:

[quote:]function myHandleReady() { alert("device ready clearing badges");


Code: Select all

setTimeout(
    
    function() {
        
        var pushNotification = window.plugins.pushNotification;
        
        alert("device id " + localStorage.getItem('pushNotificationDeviceID'));
        alert("token " + localStorage.getItem('pushNotificationToken'));
        
        var pushNotification = window.plugins.pushNotification;
        alert("pushNotification " + window.plugins.pushNotification);
        
        alert('Try like Oleg');
        try {
            pushNotification.setApplicationIconBadgeNumber(0);
        } catch (err) {
            alert("created error " + err);
        }
        
        alert("Try like Kateryna");
        try {
            pushNotification.setApplicationIconBadgeNumber(function() {}, 0);
        } catch (err) {
            alert("created error " + err);
        }
        
        alert("Try like Evgene");
        try {
            pushNotification.setApplicationIconBadgeNumber(0, function() {});
        } catch (err) {
            alert("created error " + err);
        }
                    
    }, 1000);

}[/quote]
ImageImageImageImage

You'll notice that, while I'm registered for push, and have a token, there is no window.plugins.pushNotification object, therefore none of the subsequent calls work because there is no object to call with.

So, back to my original question:

What is the definitive way to reset a badge when you use push notifications?

none of the varieties of calls in all postings on the subject work because I have no "pushNotification" object in my plugins.

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

setApplicationIconBadgeNumber not resetting

Hello Joseph,

Please try this:
prePushNotification.setApplicationIconBadgeNumber(0, function () {});/pre

Joseph Francis
Posts: 0
Joined: Thu Sep 04, 2014 8:52 pm

setApplicationIconBadgeNumber not resetting

Thanks Evgene - this is the definitive answer and works in my application.

Perhaps there's a way to publish this and supercede all the other answers which cause confusion.

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

setApplicationIconBadgeNumber not resetting

Hi Joseph,

Glad it works for you!
Thank you for a suggestion, marked as official answer.

Return to “Issues”