3

我成功了,如何在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应用端代码

是如下的onMessage(上下文为arg0,意图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); 

回答

10

从官方documentation

collapse_key

当设备处于脱机状态时,用于折叠一组类似消息 的任意字符串,以便只有最后一条消息被发送到客户端 。这样做的目的是为了避免在联机时向手机发送太多的信息给 手机。请注意,因为不保证消息发送的顺序 ,所以“最后”消息实际上可能不是应用服务器发送的最后一个消息。

所以,你可以使用不同的折叠密钥为每个消息如果你想要设备接收所有的消息..

EDIT1:通知问题

以下是您的通知代码:

notificationManager.notify(0, note); 

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

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

+0

感谢您的回复Praful。但正如你可以在我的服务器端代码中发现的那样,我每次都使用唯一密钥(GUID)。但还是有我的通知栏只有一个消息时,设备成为在线 –

+0

检查我的更新答案... –

+0

wippy,为实现这一目标,按您的方向更改** notificationManager.notify(计数,注意); **感谢paraful这是我想要的,谢谢...!?! –

0

我有同样的问题,当我试图执行的GCM我的应用程序。

我用C#作为我的第三方应用程序。起初,我对每个通知使用了相同的 collapse_key。这就是为什么我只收到最后一个 之一。

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

+0

感谢答复ZIM,但你可以看到我的服务器端编码在C#中使用GUID作为collapse_key。所以每次都是独一无二的。 –