【发布时间】:2014-04-29 06:57:34
【问题描述】:
我有 IIS 服务器并通过 GoogleCloudMessaging 向手机发送通知。在 android 设备上接收消息大约需要 10 分钟。这是我的项目的巨大时间。你知道如何减少时间吗?
那是服务器 C# 代码(使用 PushSharp)
var push = new PushBroker();
//Wire up the events for all the services that the broker registers
/*NotificationSent s = new NotificationSent()
push.OnNotificationSent += "NotificationSent";
push.OnChannelException += "ChannelException";
push.OnServiceException += "ServiceException";
push.OnNotificationFailed += "NotificationFailed";
push.OnDeviceSubscriptionExpired += "DeviceSubscriptionExpired";
push.OnDeviceSubscriptionChanged += "DeviceSubscriptionChanged";
push.OnChannelCreated += "ChannelCreated";
push.OnChannelDestroyed += "ChannelDestroyed";
*/
push.RegisterGcmService(new GcmPushChannelSettings("MY API KEY"));
push.QueueNotification(new GcmNotification().
ForDeviceRegistrationId("PHONE REGISTRATION ID")
.WithJson(@"{""alert"":""Name !"",""badge"":7,""sound"":""sound.caf""}"));
//Stop and wait for the queues to drains
push.StopAllServices();
这是我的接收器,
公共类 GcmBroadcastReceiver 扩展 WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
【问题讨论】:
-
这取决于造成延迟的原因。如果这是网络问题(例如网络覆盖率低),您唯一的解决方案是等待获取它或连接到更好的网络。也可能是您的服务器没有立即发送推送消息。基本上,它可能有很多不同的东西,没有你的代码,我们不知道你的问题是什么(或者即使有,或者你只是对网络有一个基本的误解,我并不是说这是案例...)
-
@Guardanis 我已经更新了代码,见上文
-
所以现在问题变成了:“QueueNotification()”是让它等待,还是立即发送推送?
-
@Guardanis 在调试服务器代码时,它立即转到下一行 - push.StopAllServices();在此之后,在我的 Android 应用程序上,它会在 7 到 10 分钟后进入 OnReceive 方法:(
-
仅仅因为它进入下一行并不意味着它会自动发送。该行将其添加到队列中,这意味着它可能在一个完全独立的池线程上运行,该线程将按照您的服务器及其设置确定的间隔发送它。
标签: android performance time notifications push-notification