我也遇到过同样的问题。简单的说,PushPlugin
目前不支持该功能。但您可以很容易地修改它以满足您的需要
现在如何运作
当插件收到消息时GCMIntentService
, 消息上 https://github.com/phonegap-build/PushPlugin/blob/master/src/android/com/plugin/gcm/GCMIntentService.java#L59叫做。此方法获取传递给它的所有附加参数,并将它们保存在extras
。此后,如果应用程序位于前台,则此函数只需传递extras
通过致电给您sendExtras
。否则它会调用createNotification
,这实际上在状态栏中创建了通知。
你的问题
根据我从您的问题中了解到的情况,如果在状态栏中收到通知后,您打开应用程序而不触摸通知,则不会收到警报。
发生的情况是这样的:
-
GCMIntentService
收到一条消息
- 由于您的应用程序位于
background
, PushPlugin 调用createNotification
- 当您启动应用程序时,它不会收到任何警报,因为您尚未触摸状态栏中的通知
如果您触摸了通知,您就会收到警报,因为通知意图会唤醒您的应用程序。当您的应用程序唤醒时,它会寻找缓存的额外内容 https://github.com/phonegap-build/PushPlugin/blob/master/src/android/com/plugin/gcm/PushPlugin.java#L74然后再次调用将它们传递给您的应用程序sendExtras
.
解决方案
我还没有测试过这个,但我坚信,如果你编辑你的插件,sendExtras
在您致电之前(或之后)createNotification
,无论您是触摸通知还是将其滑开,都将为您的应用程序缓存数据。
protected void onMessage(Context context, Intent intent) {
Log.d(TAG, "onMessage - context: " + context);
// Extract the payload from the message
Bundle extras = intent.getExtras();
if (extras != null)
{
// if we are in the foreground, just surface the payload, else post it to the statusbar
if (PushPlugin.isInForeground()) {
extras.putBoolean("foreground", true);
PushPlugin.sendExtras(extras);
}
else {
extras.putBoolean("foreground", false);
// Send a notification if there is a message
if (extras.getString("message") != null && extras.getString("message").length() != 0) {
createNotification(context, extras);
}
}
}
}
将上面的部分修改为:
protected void onMessage(Context context, Intent intent) {
Log.d(TAG, "onMessage - context: " + context);
Bundle extras = intent.getExtras();
if (extras != null)
{
if (PushPlugin.isInForeground()) {
extras.putBoolean("foreground", true);
}
else {
extras.putBoolean("foreground", false);
if (extras.getString("message") != null && extras.getString("message").length() != 0) {
createNotification(context, extras);
}
}
// call sendExtras always
PushPlugin.sendExtras(extras);
}
}