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 - ?
1 person has
this problem
+1
This topic is no longer open for comments or replies.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated

  • 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:

    function myHandleReady() {    alert("device ready clearing badges");
        
        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);
    }




    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.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated

  • Hello Joseph,

    Please try this:
    PushNotification.setApplicationIconBadgeNumber(0, function () {});
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated

  • 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.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited indifferent, undecided, unconcerned

  • Hi Joseph,

    Glad it works for you!
    Thank you for a suggestion, marked as official answer.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated