android - 来自 android gcm 只收到最后的通知......?

标签 android notifications push-notification google-cloud-messaging

我在如何在 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);

最佳答案

来自官方documentation

collapse_key

An arbitrary string that is used to collapse a group of like messages when the device is offline, so that only the last message gets sent to the client. This is intended to avoid sending too many messages to the phone when it comes back online. Note that since there is no guarantee of the order in which messages get sent, the "last" message may not actually be the last message sent by the application server.

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

EDIT1:通知问题

以下是您的通知代码:

notificationManager.notify(0, note);

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

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

关于android - 来自 android gcm 只收到最后的通知......?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14134744/

相关文章:

android - 大量设备的推送通知

ios - 当应用程序进入前台时检测远程通知

android - 如何使用 mysql 和 android 应用程序之间的离线模式同步数据

android - 如何从 CalendarView 获取星期几

android - 使1个自定义 View 失效导致 View 组中所有自定义 View 失效?

android - 通知已触发时显示灯

Android 锁屏通知无法双击打开浏览器

android - 显示默认展开的 android 通知操作按钮

android - 推送通知提供商如何通过选择推送给用户?

Android 应用程序在使用 Proguard 和 Dagger 的 Release Build 上崩溃