从 android gcm 只收到最后的通知...?

Posted

技术标签:

【中文标题】从 android gcm 只收到最后的通知...?【英文标题】:from android gcm receive last notification only...? 【发布时间】:2012-12-17 13:40:02 【问题描述】:

我成功地在 asp.net 中设置了 android gcm 和相关的服务器端工作。 但我的问题是,它只显示服务器发送的最后一条通知。

我希望显示的所有通知都来自同一个 collapse_key 或我在 gcm 代码或服务器端代码中更改的内容,以显示发送到特定设备的所有消息。

我的服务器端代码如下

public void SendCommandToPhone(String sCommand)

    String DeviceID = "";
    DeviceID = "APA91bF9SSDEp-UPU8UwvctGt5ogfrSw0UdilTWlCujqItHcr-eiPJ31ZDI-IeqTbLr92ZOPF31bXtB1_5gNEPHAq82N4ji0stm4cy9U0Yuk0Awhjl9PS02okuc9rRhJobkgOt-ofm5Br0KR-Y";
    WebRequest tRequest;
    tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
    tRequest.Method = "post";
    //tRequest.ContentType = "application/json";
    tRequest.ContentType = "application/x-www-form-urlencoded";
    tRequest.Headers.Add(string.Format("Authorization: key=0", "AIzaSyBgCKDhyHnRmmu8jCfmupHVJA8cVkIa-XEZS"));
    String collaspeKey = Guid.NewGuid().ToString("n");
    String postData = string.Format("registration_id=0&data.message=1&collapse_key=2", DeviceID, "YourMessage", collaspeKey);
    Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    tRequest.ContentLength = byteArray.Length;
    Stream dataStream = tRequest.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();
    WebResponse tResponse = tRequest.GetResponse();
    dataStream = tResponse.GetResponseStream();
    StreamReader tReader = new StreamReader(dataStream);
    String sResponseFromServer = tReader.ReadToEnd();
    tReader.Close();
    dataStream.Close();
    tResponse.Close();

在 android app 端代码如下 onMessage(Context arg0, Intent arg1)

NotificationManager notificationManager = (NotificationManager) arg0
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification note = new Notification(R.drawable.ic_launcher,
            "meaNexus Notification", System.currentTimeMillis());

    Intent notificationIntent = new Intent(arg0, Main.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(arg0, 0,
            notificationIntent, 0);
    note.setLatestEventInfo(arg0, String.valueOf(R.string.app_name), message, pendingIntent);
    note.number = count++;
    note.defaults |= Notification.DEFAULT_SOUND;
    note.defaults |= Notification.DEFAULT_VIBRATE;
    note.defaults |= Notification.DEFAULT_LIGHTS;
    note.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, note);

【问题讨论】:

【参考方案1】:

来自官方documentation

collapse_key

用于折叠一组类似消息的任意字符串 当设备离线时,只有最后一条消息被发送到 客户端。这是为了避免向 当它重新上线时打电话。请注意,由于无法保证 按照发送消息的顺序,“最后一条”消息可能不会 实际上是应用服务器发送的最后一条消息。

因此,如果您希望设备接收所有消息,您可以为每条消息使用不同的折叠键..

EDIT1:通知问题

以下是您的通知代码:

notificationManager.notify(0, note);

根据 notifiy() 方法的 java 文档。它发布要在状态栏中显示的通知。 如果您的应用程序已经发布了具有相同 id 的通知并且尚未取消,则它将替换为更新的信息。

所以在notificationManager.notify() 方法调用中使用不同的ID 在notification 栏中发布多个notification..

【讨论】:

感谢 Praful 的回复。但是您可以在我的服务器端代码中找到我每次都使用唯一键(GUID)。但是当设备上线时,我的通知栏中仍然只有一条消息 wippy,为了按照您的指示实现这一点,我更改了 notificationManager.notify(count, note); 谢谢 paraful 这就是我想要的,谢谢...!?! 请注意,GCM 一次最多只能保存 4 条消息,使用不同的折叠键。如果您在任何给定时间使用超过 4 个折叠键,您将丢失一些消息。 (哪些消息被丢弃是未定义的。) @praful Thanx,我也已经在 notify 方法中使用 count 克服了这个问题。但现在面临一个奇怪的问题。如果我的设备关闭或不在线,那么当它开启或在线时,它应该会收到之前发送的所有通知。但我没有得到。我已经包含了唤醒锁,并且同一设备正在接收其他应用程序的推送通知。你能帮帮我吗?????? 嗨@Arjit ..请将您的问题作为单独的帖子发布,以便其他人也可以帮助您解决问题,我也会对您的问题有更清晰的背景..正如 Lorne 在评论中所说“GCM 一次最多只能保存 4 条消息,使用不同的折叠键。如果您在任何给定时间使用超过 4 个折叠键,您将丢失一些消息。(哪些消息被丢弃是未定义的。)”【参考方案2】:

当我尝试在我的应用程序中实施 GCM 时遇到了同样的问题。

我将 C# 用于我的第三方应用程序。一开始我用的是一样的 每个通知的collapse_key。这就是为什么我只收到最后一个 一个。

因此,当您调用通过 GCM 服务器向您的设备发送通知的函数时,请尝试使用不同的 collapse_key。 例如尝试使用当前时间。

【讨论】:

感谢 zim 的回复,但正如您在我用 c# 编写的服务器端编码中看到的那样,它使用 GUID 作为 collapse_key。所以每次都是独一无二的。

以上是关于从 android gcm 只收到最后的通知...?的主要内容,如果未能解决你的问题,请参考以下文章

有时一些 android 用户没有通过 GCM 收到推送通知

发送 GCM 上游消息后收到空推送消息

适用于 Android 谷歌云消息 (GCM) 的 Azure 移动服务从不向设备发送通知

如何通过xmpp gcm将ack从android应用程序发送到服务器

某些设备在 android 中未收到推送通知

Android ICS 上的 GCM 通知不起作用