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。所以每次都是獨一無二的。 –