即使在实施了ngCordova(cordova-plugin-badge)和phonegap-plugin-push之后,Android应用图标上的徽章仍未显示 [英] Badge on app icon for Android doesn't show even after implementing ngCordova (cordova-plugin-badge) and phonegap-plugin-push

查看:185
本文介绍了即使在实施了ngCordova(cordova-plugin-badge)和phonegap-plugin-push之后,Android应用图标上的徽章仍未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人能帮我解决这个困扰我几天的问题,我将非常感激。

I will really appreciate if anyone can help me with this issue that has been bugging me for days.

我有一个使用Ionic框架创建的混合应用程序,我已经实现了推送通知(通过phonegap-plugin-push)。推送通知工作正常,但我想要的是推送通知(即GCM有效负载)将徽章计数/数字发送到应用程序,应用程序将获取该计数/数字并将其显示为应用程序旁边的徽章图标。我的代码适用于iOS设备,因为徽章已经内置,但我在Android平台上实现相同的想法(徽章)时遇到了困难。

I have a hybrid app created using the Ionic framework, which I have implemented push notifications on (via phonegap-plugin-push). The push notifications work fine, but what I want is for the push notification (i.e. GCM payload) to send a badge count/number over to the application, and the application will take that count/number and displays it as a badge beside the app icon. My code works perfectly for iOS devices given that badges are already inbuilt, but I have difficulties with implementing the same idea (badges) on the Android platform.

我知道由于徽章没有内置到Android平台,有些设备可能不受支持,但至少我希望它适用于某些设备(即三星,索尼)。我做了很多研究,最突出的是:

I am aware that due to badges not being inbuilt into the Android platform, some devices might not be supported, but at least I want it to work for some devices (i.e. Samsung, Sony). I have done a lot of research, most prominently:


  1. cordova-plugin-badge( https://github.com/katzer/cordova-plugin-badge )文件中陈述假设适用于iOS和某些Android设备,但它根本不适用于任何Android设备。 请注意,我一直在测试的Android设备是Genymotion的模拟器,我已经设置了Google Play服务,并且能够接收推送通知和功能,就像真正的设备一样,这是一个问题吗?

  1. cordova-plugin-badge (https://github.com/katzer/cordova-plugin-badge) which stated in the documentation is supposed to work for both iOS and certain Android devices, but it doesn't work on any Android devices at all for me. Note that the Android devices I have been testing on are emulators from Genymotion, which I have set up Google Play Services on and are able to receive push notifications and function almost like a real device, will that be an issue?

ShortcutBadger( https:// github .com / leolin310148 / ShortcutBadger )其中只有原生 Android实现的文档,并且如上所述被cordova-plugin-badge使用,以扩展对混合Android应用程序的支持,但这有根本无法帮助我。

ShortcutBadger (https://github.com/leolin310148/ShortcutBadger) which only has documentation for native Android implementation and supposedly utilised by cordova-plugin-badge as stated above to extend the support to hybrid Android apps, but this has been unable to help me at all.

我的index.html:

My index.html:

<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="css/style.css" rel="stylesheet">

    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="lib/ionic-platform-web-client/dist/ionic.io.bundle.min.js"></script>

    <script src="js/ng-cordova.js"></script>
    <script src="cordova.js"></script>

    <script src="js/app.js"></script>

  </head>
  <body ng-app="starter">
    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">Trade Deals</h1>
      </ion-header-bar>
      <ion-content ng-controller="badgeController">
        <div>Number of deals pending now: </div>
        <div class="deals"></div>
        <button class="button" ng-click="setBadge(10)">Set badge 10</button>
        <button class="button" ng-click="hasPermission()">Show permission</button>
        <button class="button" ng-click="get()">Get badge count</button>
        <button class="button" ng-click="clear()">Clear badge count</button>
        <button class="button" ng-click="increase()">Increase by 1</button>
        <button class="button" ng-click="decrease()">Decrease by 1</button>
      </ion-content>
    </ion-pane>
  </body>
</html>

我的app.js:

angular.module('starter', ['ionic', 'ngCordova'])

/*
 * do a ionic.push register() every time the app launches for the first time
 * so that it is guaranteed that there is always a valid device token for GCM/APNS
 */
.run(function($ionicPlatform, $cordovaBadge) {
  $ionicPlatform.ready(function() {
    console.log("Device platform is "+device.platform);
    var push = new Ionic.Push({
      "debug": true,
      "onNotification": function(notification) {
        console.log("notification received!!!");
        var payload = notification.payload;
        var payloadStr = JSON.stringify(payload, null, 4);
        var notiStr = JSON.stringify(notification, null, 4);
        console.log("notification: "+notiStr);

        var countIndex = notiStr.indexOf("count");  // extracting badge count from the GCM payload
        var badgeIndex = countIndex + 9;
        var badgeNo;
        if (!isNaN(notiStr[badgeIndex+1])) {
          badgeNo = notiStr.substr(badgeIndex,badgeIndex+2);
        }
        else {
          badgeNo = notiStr[badgeIndex];
        }

        if (device.platform == "Android") {
          $cordovaBadge.set(badgeNo);
        }
      },
      "onRegister": function(data) {
        console.log(data.token);
      },
      "pluginConfig": {
        "android": {
          "sound": "true",
          "badge": "true",
          "icon": "icon",
          "iconColor": "lime"
        },
        "ios": {
          "alert": "true",
          "badge": "true",
          "sound": "true"
        },
      }
    });

    push.register(function(token) {
      console.log("My Device token:",token.token);
      //window.alert("The token is "+token.token);
      push.saveToken(token);  // persist the token in the Ionic Platform so that it doesn't change on multiple launches
    });

    $cordovaBadge.get().then(function(badge) {
      document.querySelector(".deals").innerHTML = badge;
    });
  });
})

.controller("badgeController", function($scope, $ionicPlatform, $cordovaBadge) {
    console.log("inside badgeController");

    $ionicPlatform.ready(function() {
        $ionicPlatform.on('resume', function() {
          $cordovaBadge.get().then(function(badge) {
            document.querySelector(".deals").innerHTML = badge;
          });
        });
        //$cordovaBadge.configure({autoClear: true});  // configure to clear all notifications whenever user opens the app
        $scope.setBadge = function(value) {
          console.log("inside setBadge");
          $cordovaBadge.hasPermission().then(function(result) {
              $cordovaBadge.set(value);
              window.alert("Badge count is "+value);
          }, function(error) {
              console.log(JSON.stringify(error));   // display error message
          });
        }

        $scope.hasPermission = function() {
          console.log("inside hasPermission");
          $cordovaBadge.hasPermission().then(function(result) {
              window.alert("User has permission: "+result);
              console.log("device has permission");
          }, function(error) {
              console.log(JSON.stringify(error));   // display error message
          });
        }

        $scope.get = function() {
          console.log("inside get");
          $cordovaBadge.hasPermission().then(function(result) {
              $cordovaBadge.get().then(function(badge) {
                if (badge>=0) {
                  document.querySelector(".deals").innerHTML = badge;
                }
              })
          }, function(error) {
              console.log(JSON.stringify(error));   // display error message
          });
        }

        $scope.clear = function() {
          console.log("inside clear");
          $cordovaBadge.hasPermission().then(function(result) {
              $cordovaBadge.clear();
              window.alert("Cleared badge count");
          }, function(error) {
              console.log(JSON.stringify(error));   // display error message
          });
        }

        $scope.increase = function() {
          console.log("inside increase");
          $cordovaBadge.hasPermission().then(function(result) {
              $cordovaBadge.increase();
              window.alert("Increased badge count");
          }, function(error) {
              console.log(JSON.stringify(error));   // display error message
          });
        }

        $scope.decrease = function() {
          console.log("inside decrease");
          $cordovaBadge.hasPermission().then(function(result) {
              $cordovaBadge.decrease();
              window.alert("Good job!");
              //window.alert("Decreased badge count");
          }, function(error) {
              console.log(JSON.stringify(error));   // display error message
          });
        }
    });
});

此外,是否必须将应用图标转换为小部件以使徽章有效?我不确定如何实现cordova-plugin-badge,并且说明没有说明Android所需的小部件。

Also, is the issue that the app icon has to be converted to a widget in order for the badges to work? I am not sure how the cordova-plugin-badge is implemented and the instructions didn't say anything about widgets being needed for Android.

谢谢你和任何帮助/提示非常感谢:)我已经对这个问题进行了几天的故障排除而且非常令人沮丧。

Thank you and any help/tips is appreciated :) I have been troubleshooting this for days and it's rather frustrating.

推荐答案

股票Android在此处不提供此功能标准发射器上的时刻。

Stock Android does not offer this functionality at the moment on the standard launcher.

某些制造商(例如三星)已将此功能纳入其定制的Android发射器中。此外,一些第三方发射器(例如Nova Launcher)已经包含了一个API来完成此任务。

Certain manufacturers (e.g. Samsung notably) have included this functionality into their customised Android launchers. Also some 3rd-party launchers (e.g. Nova Launcher) have included an API to accomplish this.

您可能需要查看以下链接以获得进一步的解释:

You may want to check following links for further explaination:


  1. Facebook如何在Android中的应用图标上添加徽章编号?

  2. 三星是否修改了它的Android ROM以获得徽章电子邮件和短信图标?

  3. 如何在Android上制作应用程序徽章?

  1. How does Facebook add badge numbers on app icon in Android?
  2. Does Samsung modifies it's Android ROMs to have badges on email and SMS icons?
  3. How to make application badge on android?

问候

这篇关于即使在实施了ngCordova(cordova-plugin-badge)和phonegap-plugin-push之后,Android应用图标上的徽章仍未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆