尝试使用 PushPlugin处理推送通知. 以下是我的代码. onNotificationGCM: function(e) { switch( e.event ) { case 'registered': if ( e.regid.length 0 ) { console.log("Regid " + e.regid); //alert('registration id = '+e.regid); sDev
以下是我的代码.
onNotificationGCM: function(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
console.log("Regid " + e.regid);
//alert('registration id = '+e.regid);
sDeviceId = e.regid;
//alert(sDeviceId);
}
break;
case 'message':
// this is the actual push notification. its format depends on the data model from the push server
alert('message = '+e.message);
alert('message = '+e.message+' msgcnt = '+e.msgcnt);
if ( e.foreground )
{
alert("Notification Received");
}
else
{ // otherwise we were launched because the user touched a notification in the notification tray.
if ( e.coldstart )
{
alert("coldstart");
}
else
{
alert("other than coldstart");
}
}
break;
case 'error':
alert('GCM error = '+e.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
}
所以一切都在发挥作用.
>当应用程序在前台时,我收到警报.
>当收到消息时单击通知应用程序
打开,我收到警报.(冷启动)
>当应用程序在后台然后单击时
通知应用程序进入前台,我得到了
警报.
但是当我将应用程序保留到后台并且当我将应用程序带到前面而没有单击通知时推送通知到达时我没有得到警报.那么如何处理这种情况呢?
我遇到了同样的问题.简而言之,PushPlugin现在不支持此功能.但您可以非常轻松地修改它以满足您的需求它现在如何运作
当插件的GCMIntentService收到消息时,将调用onMessage.此方法获取传递给它的所有其他参数,将它们保存在附加内容中.在此之后,如果应用程序位于前台,此函数只需通过调用sendExtras将额外内容传递给您.否则,它会调用createNotification,它实际上会在状态栏中创建通知.
你的问题
根据我从您的问题中理解的情况,如果在状态栏中收到通知后您打开应用程序而未触及通知,则不会收到警报.
这是发生的事情:
> GCMIntentService收到一条消息
>由于您的应用程序位于后台,因此PushPlugin会调用createNotification
>当您启动应用程序时,它不会收到警报,因为您尚未触摸状态栏中的通知
如果您触摸了通知,则会收到警报,因为通知意图会唤醒您的应用.当您的应用程序唤醒时,它会查找cached extras,然后再次调用sendExtras将它们传递给您的应用程序.
解决方案
我还没有对此进行过测试,但我坚信如果你编辑你的插件,在调用createNotification之前(或之后)sendExtras,无论你是否触摸通知或刷掉它,都会为你的应用程序缓存数据.
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);
}
}
