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(); 
} 

다음과 같다

공식 documentation

에서의 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

다음과 같다 collapse_key

메일이 일 때 오프라인 일 때 그룹을 축소하는 데 사용되는 임의의 문자열로, 마지막 메시지 만 클라이언트에 에게 전송됩니다. 이는 온라인으로 돌아올 때 전화로 너무 많은 메시지를 보내지 않도록하기위한 것입니다. 메시지가 전송되는 순서의 보장이 없기 때문에 "마지막"메시지는 실제로 응용 프로그램 서버에서 보낸 마지막 메시지가 아닐 수 있습니다.

: 알림 문제

다음 알림 코드 : 당신이 장치가 모든 메시지를 수신 할 경우

그래서 당신은

EDIT1

가 .. 각 메시지에 대해 서로 다른 붕괴의 키를 사용할 수 있습니다
notificationManager.notify(0, note); 

notifiy() 방법의 java 문서에 따라. 상태 표시 줄에 알림을 게시합니다. 동일한 ID를 사용하는 알림이 애플리케이션에서 이미 게시되었지만 아직 취소되지 않은 경우 업데이트 된 정보로 대체됩니다. 내 응용 프로그램 GCM을 구현하는 시도 할 때

그래서 저도 같은 문제를 가지고 있습니다 .. notification 줄에

+0

답장을 보내 주셔서 감사합니다. 하지만 내 서버 측 코드에서 매번 고유 키 (GUID)를 사용하고 있음을 알 수 있습니다. 그러나 장치가 온라인 상태가 될 때 알림 표시 줄에 하나의 메시지가 계속 표시됩니다. –

+0

내 업데이트 된 답변 확인 ... –

+0

wippy, 귀하의 지시에 따라 변경 ** notificationManager.notify (개수, 메모); ** 감사합니다 paraful this 내가 원하는 건 고마워 ...!?!? –

0

여러 notification를 게시하는 notificationManager.notify() 메서드 호출에 다른 ID를 사용합니다.

제 3 부 응용 프로그램에 C#을 사용했습니다. 처음에는 각 알림마다 동일한 collapse_key를 사용했습니다. 그래서 내가 마지막으로 을 받았다.

그래서, 당신은 GCM 서버를 통해 장치에 알림을 보내 함수를 호출 할 때, 다른 collapse_key와를하려고합니다. 예를 들어 curentTime을 사용해보십시오.

+0

답장 zim을 보내 주셔서 감사합니다. 내 서버 쪽 C#에서 코딩 그것은 GUID를 collapse_key로 사용합니다. 그래서 매번 독특합니다. –