Push Notifications - Get Badge Count

3 posts, 0 answers
  1. Joe
    Joe avatar
    8 posts
    Member since:
    Oct 2016

    Posted 29 Mar Link to this post

    I'm using the push notification plugin (http://docs.telerik.com/platform/backend-services/javascript/push-notifications/push-getting-started).

    Everything is working great so far, I can submit new notifications from the Backend Services REST Api, the notification displays on the device and the app icon badge number is set.

    I need to be able to get the badge count and display how many notifications a user has when they log into the app.

    I noticed that there are push.setBadgeNumber and push.clearBadgeNumber functions, but there is no push.getBadgeNumber function.

    http://docs.telerik.com/platform/backend-services/javascript/apireference/JavascriptSDK/Push/push.setBadgeNumber

    I decided to try out the Badge Cordova plugin (http://plugins.telerik.com/cordova/plugin/badge), but it isn't working, it always returns zero, even if the icon has a number.

    Here's my code for getting the badge count:

    if (window.cordova) {
            document.addEventListener("deviceready", function () {
                if (navigator && navigator.splashscreen) {
                    navigator.splashscreen.hide();
                }           
                bootstrap();
                if (!window.navigator.simulator) {
                    window.screen.lockOrientation("portrait");
                    cordova.plugins.notification.badge.get(function (count) {                   
                        console.log("Current badge: " + count);
                        if (count > 0) {
                            $("#badgeCount").text(count.toString());
                            $("#badgeContainer").show();
                        }
                        else {
                            $("#badgeContainer").hide();
                        }
                    });
                }
            }, false);
        }
        else {
            bootstrap();
        }

    Any suggestions?

     

  2. Anton Dobrev
    Admin
    Anton Dobrev avatar
    605 posts

    Posted 03 Apr Link to this post

    Hi Joe,

    When sending a push notification to iOS you can set the IOS.aps.badge number in the payload.
    It will be set on the app icon. In the notification received callback you will receive a badge property with the value of the badge at the moment. Make the required calculations and set the device badge count from the app using the methods for controlling the badge count.

    var onNotificationAPN = function(e) {
        alert(JSON.stringify(e));
        var badgeCount = parseInt(e.badge);
        // alter the badge count here
    };

    I hope this helps.

    Regards,
    Anton Dobrev
    Telerik by Progress
     

    Visit the Telerik Verified Plugins Marketplace and get the custom Cordova plugin you need, already tweaked to work seamlessly with AppBuilder.

     
  3. Joe
    Joe avatar
    8 posts
    Member since:
    Oct 2016

    Posted 03 Apr in reply to Anton Dobrev Link to this post

    Anton ,

    Thanks a lot, this is exactly what I needed!

Back to Top