Cordova应用程序(混合应用程序)中的自动递增推送通知徽章

时间:2017-04-17 13:31:56

标签: angularjs ionic-framework

如何在Cordova App(混合应用程序)中自动增加推送通知徽章。 我正在使用Ionic框架来开发应用程序。

当我从服务器获得推送通知时。我想自动更新徽章计数。

1 个答案:

答案 0 :(得分:1)

$ $ rootScope申请();  需要反映您的更改。使用rootScope变量来反映通知计数。

$ionicPlatform.ready(function() {

    FCMPlugin.getToken(function(token) {
        console.log('Device Token : ' + token);
        DataSharingService.setItem('deviceToken', token);
    });

    //FCMPlugin.onNotification( onNotificationCallback(data), successCallback(msg), errorCallback(err) )
    //Here you define your application behaviour based on the notification data.
    FCMPlugin.onNotification(function(data) {

        console.log('Recived data : ' + JSON.stringify(data));
        **$rootScope.notificationCountValue = $rootScope.notificationCountValue + 1;**
        $rootScope.notificationArray.push(data);
        DataSharingService.setItem('notificationData', $rootScope.notificationArray);
        **$rootScope.$apply();**

        if (data.wasTapped) {
            //Notification was received on device tray and tapped by the user.
            console.log(JSON.stringify(data));
            App.go('notifications', { obj: data });

        } else {
            //Notification was received in foreground. Maybe the user needs to be notified.
            console.log(JSON.stringify(data));
        }
    });


  });