从Android GCM仅接收最后一个通知...?

3

我成功地设置了Android GCM和相关的ASP.NET服务器端工作。

但是我的问题是,它只显示服务器发送的最后一条通知。

我希望所有通知都来自相同的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应用程序端,代码如下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);
2个回答

10

来自官方文档

collapse_key

一个任意的字符串,用于在设备离线时折叠一组类似的消息,以便只有最后一条消息发送到客户端。这旨在避免在手机重新联机时向其发送过多的消息。请注意,由于无法保证消息发送的顺序,“最后”消息可能实际上不是应用程序服务器发送的最后一条消息。

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

编辑1:通知问题

以下是您的通知代码:

notificationManager.notify(0, note);
根据 notifiy() 方法的 Java 文档,它会在状态栏中发布一条通知。如果您的应用程序已经发布了一个具有相同 ID 的通知并且尚未被取消,那么该方法将用更新后的信息替换该通知。

因此,请在 notificationManager.notify() 方法调用中使用不同的 ID 来发布多个通知到通知栏中。


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

0

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

我使用C#编写的第三方应用程序。起初,我对每个通知都使用了相同的折叠键(collapse_key),这就是为什么我只收到了最后一个通知。

所以,当您调用发送通知到您的设备的功能时,请尝试使用不同的collapse_key。 例如,尝试使用当前时间(currentTime)。


谢谢您的回复,zim。但是正如您在我的C#服务器端编码中所看到的,它使用GUID作为collapse_key。因此,每次都是唯一的。 - Manish Jain

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