Man*_*ain 3 notifications android 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();
}
Run Code Online (Sandbox Code Playgroud)
在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);
Run Code Online (Sandbox Code Playgroud)
Pra*_*gar 10
从官方文档
collapse_key的
一个任意字符串,用于在设备脱机时折叠一组类似的消息,以便只将最后一条消息发送到客户端.这是为了避免在重新联机时向手机发送过多消息.请注意,由于无法保证发送消息的顺序,"最后"消息实际上可能不是应用程序服务器发送的最后一条消息.
因此,如果您希望设备接收所有消息,则可以为每条消息使用不同的折叠键.
EDIT1:通知问题
以下是您的通知代码:
notificationManager.notify(0, note);
Run Code Online (Sandbox Code Playgroud)
根据notifiy()方法的java doc .它发布要在状态栏中显示的通知.如果您的应用程序已经发布了具有相同ID的通知但尚未取消,则它将被更新的信息替换.
因此,使用不同的ID在notificationManager.notify()方法调用发布多个notification在notification酒吧..
| 归档时间: |
|
| 查看次数: |
4786 次 |
| 最近记录: |