This is a migrated thread and some comments may be shown as answers.

Push Notifications - Get Badge Count

2 Answers 272 Views
General Discussion
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Joe
Top achievements
Rank 1
Joe asked on 29 Mar 2017, 03:51 PM

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 Answers, 1 is accepted

Sort by
0
Anton Dobrev
Telerik team
answered on 03 Apr 2017, 09:28 AM
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.

 
0
Joe
Top achievements
Rank 1
answered on 03 Apr 2017, 01:14 PM

Anton ,

Thanks a lot, this is exactly what I needed!

Tags
General Discussion
Asked by
Joe
Top achievements
Rank 1
Answers by
Anton Dobrev
Telerik team
Joe
Top achievements
Rank 1
Share this question
or