【问题标题】:Why am I unable to receive push Notification on Android by ionic framework为什么我无法通过 ionic 框架在 Android 上接收推送通知
【发布时间】:2015-11-18 22:37:19
【问题描述】:

我编写了这个小测试应用程序来测试新的 ionic push,并且在后端我使用的是 Android Google Cloud Messaging Service。我从前端的 android GCM 获得了成功

data: Object
canonical_ids: 0
failure: 0
multicast_id: 8830892721591033000
results: Array[1]
success: 1
__proto__: Object

这是前端代码

app.run(function($ionicPlatform, $ionicPush,$http) {
  $ionicPlatform.ready(function() {
   $ionicPush.init({
  "onNotification": function(notification) {
    var payload = notification.payload;
    console.log(payload);
    console.log(notification);
  },
  "onRegister": function(data) {
    console.log(data.token);
  }
});

$ionicPush.register(function(data){
  var reg = {
    regId : data.token
   };
  $http.post('http://app.example.com/sendPushNot', reg)
  .then(function(response){
    console.log(response);
  }, function (error){
    console.log(error);
  });
});

在我的后台

var message = new gcm.Message({
    collapseKey: 'demo',
    priority: 'high',
    contentAvailable: true,
    delayWhileIdle: true,
    timeToLive: 3,
    //restrictedPackageName: "somePackageName",
    dryRun: false,
    data: {
        key1: 'message1',
        key2: 'message2'
    },
    notification: {
        title: "Hello, World",
        icon: "ic_launcher",
        body: "This is a notification that will be displayed ASAP."
    }
});

    console.log(message);
    var sender = new gcm.Sender('AIzaSyB9Lz**********9mHJuH5if1m5k5JOVMw'); 

    var regTokens =  [];
    regTokens.push(req.body.regId);

        sender.send(message, { registrationTokens: regTokens }, function (err, result) {
            if (err) {
                console.error(err);
            }
            else {

                 console.log(result);
                 res.status(200);
                 res.set('Content-Type', 'text/html');
                 res.send(result);
            }
        });   

但我仍然无法收到任何推送通知。我不明白为什么?

任何其他人发现任何东西。我在后端使用node-gcm

编辑编辑编辑
问题可能出在任何服务器或前端。
我已经使用this 准确地缩小了问题的范围。现在使用它,因为没有有效负载,我可以在onNotification 侦听器中看到控制台日志。这意味着我的听众是正确的......部分是因为我能够通过仅使用数据字段将数据从服务器发送到前端。 现在的问题可能是 Ionic.Push 无法接收通知,或者可能是 node-gcm 无法正确发送插件 屏住呼吸,等待我和这些坏蛋插件之间的终极对决。

【问题讨论】:

  • 你安装了推送通知插件吗?
  • 是的,通过ionic plugin add phonegap-plugin-push 也做了ionic config set dev_push false ionic push --google-api-key AIzaSyB****************JOVMw ionic config set gcm_key 14*****55078

标签: android push-notification ionic-framework google-cloud-messaging


【解决方案1】:

你能确认发送令牌到服务器的http请求被执行了吗?如果我没记错的话,Android 令牌会在您的 onRegister 回调中通过 gcm 在手机的推送通知中收到:

app.run(function($ionicPlatform, $ionicPush,$http) {
  $ionicPlatform.ready(function() {
   $ionicPush.init({
  "onNotification": function(notification) {
    var payload = notification.payload;
    console.log(payload);
    console.log(notification);
  },
  "onRegister": function(data) {
    console.log(data.token);
    //works for android
    var reg  = { regId: data.token };        
      $http.post('http://app.example.com/sendPushNot', reg)
      .then(function(response){
        console.log(response);
      }, function (error){
        console.log(error);
      });
      }
    });

    $ionicPush.register(function(data){
       //works for ios 
      var reg = {
        regId : data.token
       };
      $http.post('http://app.example.com/sendPushNot', reg)
      .then(function(response){
        console.log(response);
      }, function (error){
        console.log(error);
      });
    });

【讨论】:

  • 它已执行,因此我也粘贴了响应。我会马上检查 onRegister 的事情。几分钟后见
  • 不出所料,它不起作用。它工作过一次。该死的我应该保存那个文件。s
  • 更新了我的答案。你能在你的服务器端代码中记录 req.body 并确认你现在收到一个令牌吗?尽管 gcm 将令牌发送到您的设备并显示在 console.log 中,但我认为您没有在服务器端代码中收到它...
  • 我在对方收到,执行sender.send函数,结果成功。 (这是我发布的第一个输出,我通过服务器验证了它)。
【解决方案2】:

好的,伙计们,在我最后的对决之后,我是赢家。我终于想通了。我没有输入app_id 字段。这与 ionic 完全相关,因此对于 android 人来说并非如此。这是一个小问题。如果我之前看过源代码。我早就知道了。但无论如何要解决四天,最后它的工作。 如果任何东西在 cmets 中不起作用,我会帮助你。

所以流程是这样的。

ionic config set dev_push false

ionic push --google-api-key AIzaSyB9L****************5if1m5k5JOVMw

ionic config set gcm_key 148*****5078

ionic config set app_id (some_alphanumeric_value found on ionic.io and is availble after you have done ionic io init)

【讨论】:

    猜你喜欢
    • 2017-06-13
    • 1970-01-01
    • 2019-05-15
    • 2016-08-10
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    相关资源
    最近更新 更多