Android 点击通知后应用程序在后台时不会触发推送

Android push does not trigger when app in background after notification clicked

我有一个 Cordova 应用程序,使用 push plugin
在服务器上,我使用 node-gcm.
发送 GCM 消息 我收到 iOS 和 Android 的通知,它们会在单击时打开应用程序,但是在 Android 上,'notification' 事件不会触发。

push.on('notification', function(data) {
    console.log(data);
    alert('got PUSH!');
});

如果应用程序 运行 在前台,那么我会收到 'got PUSH!' 警报,但是当应用程序在后台并且我在推送通知到达时单击它时,它会打开应用程序但不提醒 'got PUSH!'.

如何在应用打开后触发 'notification' 事件?


这是我发送给设备的 gcm 消息:

{
  "params": {
    "notification": {
      "title": "SO post demo",
      "body": "THIS IS A TEST!",
      "icon": "ic_launcher",
      "sound": true
    }
  }
}

我认为我需要在通知负载中加入一些东西来解决这个问题,但我没有尝试在应用程序从后台打开时触发该事件。


以下是我尝试过的一些方法:

使用“内容可用”(建议here),使用message.addData

{
  "params": {
    "notification": {
      "title": "SO post demo",
      "body": "THIS IS A TEST!",
      "icon": "ic_launcher",
      "sound": true
    },
    "data": {
      "info": "super secret info",
      "content-available": "1"
    }
  }
}

同样,但将我的参数放在 json 中,我用来制作消息:

{
  "params": {
    "notification": {
      "title": "SO post demo",
      "body": "THIS IS A TEST!",
      "icon": "ic_launcher",
      "sound": true,
      "info": "super secret info",
      "content-available": "1"
    }
  }
}

在有条不紊地创建这个问题的过程中,我找到了答案。
我需要我的负载看起来像这样:

{
  "params": {
    "data": {
      "title": "SO post demo",
      "body": "THIS IS A TEST!",
      "icon": "ic_launcher",
      "sound": true
    }
  }
}

这会弄乱显示的通知图标(它是放大版本),但在这个阶段,我可以接受。