安卓推送通知(慢)

5

我有一个IIS服务器,并使用GoogleCloudMessaging向手机发送通知。在安卓设备上接收消息需要大约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);

}

}


这取决于延迟出现的原因。如果是网络问题(例如,网络覆盖范围低),您唯一的解决方案是等待或连接更好的网络。也可能是您的服务器没有立即发送推送消息。基本上,可能有很多不同的问题,而没有您的代码,我们不知道您的问题是什么(甚至不知道是否存在问题,或者您只是对网络有基本误解,我并不是在说这种情况……) - Cruceo
现在的问题是: "QueueNotification()" 会导致等待,还是立即发送 Push? - Cruceo
1
仅仅因为代码换行了并不意味着它会自动发送。该行代码将其添加到队列中,这意味着它可能在完全独立的池线程上运行,并且将根据您的服务器及其设置确定的时间间隔发送。 - Cruceo
@Guardanis,好的,在哪里可以更改设置? - Misha Akopov
也许你应该问建立这个库的人。或者尝试自己查看代码。 - Cruceo
显示剩余2条评论
1个回答

4
问题是许多wifi路由器会关闭与服务器的连接,当wifi关闭连接后设备需要时间重新连接到Google服务器。
解决方案是每3-4分钟发送心跳包以保持永久连接。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接