【发布时间】:2013-02-14 07:26:36
【问题描述】:
目前我正在使用 3 台设备进行测试:Samsung S7500、Galaxy Mexus、HTC explore。如果设备处于活动状态,那么我会在所有三个设备中收到推送通知。如果它处于空闲状态(锁定/睡眠),那么我也会在所有三个设备中收到推送通知。但是,如果在发送推送消息时它处于脱机状态,那么我上线的时间应该会根据文档收到所有待处理的通知。但在这种情况下,我没有在所有设备中收到通知。特别是在 Galaxy Nexus 中,有时我会收到它是否处于空闲状态,有时不是。我哪里错了?谁能帮帮我?
从第三方服务器(.Net)端发布到 GCM 服务器的字符串:其中“i”是当前时间。
String postdata= "collapse_key=score_update"+i+"&time_to_live=2419200&delay_while_idle=1&data.message="+ message + "&data.time=" + System.DateTime.Now.ToString()
+ "®istration_id=" + deviceToken + "";
根据文档(包括唤醒锁)在清单中提到了所有必需的权限。这是我在 GCMIntentService 类中的通知代码:
int icon = R.drawable.notif;
long when = System.currentTimeMillis();
NotificationManager notificationManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
Intent notificationIntent = new Intent(context, DemoActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, (int) when,
notificationIntent,0);
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.notify);
contentView.setImageViewResource(R.id.ImageViewpnotify, icon);
contentView.setTextViewText(R.id.TextViewpnotify, message);
notification.contentView = contentView;
notification.contentIntent= intent;
notification.icon = icon;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.ledARGB = 0xffff0000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledOnMS = 100;
notification.ledOffMS = 100;
notificationManager.notify((int)when, notification);
【问题讨论】:
-
为什么要指定 delay_while_idle=1 ?
-
来自文档,delay_while_idle :如果包含,表示如果设备空闲,则不应立即发送消息。
-
根据developer.android.com/google/gcm/adv.html 中的文档,如果设备空闲,则延迟_while_idle 标志应该为真。因此,我已将其指定为 1(true)。如果您可以进行测试,请提供值
-
"如果设备已连接但空闲,消息仍将立即传递,除非 delay_while_idle 标志设置为 true。" 请指定 "delay_while_idle" : false
-
另外,如果你看过,是否需要增加collapse_key。因为我已将增量值“i”附加到值
标签: android google-cloud-messaging android-notifications galaxy-nexus