5

완벽하게 작동하는 GCM 알림 구현이 있습니다. 그러나 문제는 일단 수신 된 메소드의 의도에서 메시지가 수신되면 표시되는 메시지는 항상 이전 메시지입니다. 그것은 'extras.getString ("payload")'는 항상 이전 메시지를 보여줍니다. 나는 그 문제가 무엇인지 알아낼 수 없다.GCM android 푸시 알림에 항상 이전 메시지가 표시됩니다. 의도가 잘못됨

GCM 통지를 전송 클래스이다

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 

public class C2DMMessageReceiver extends BroadcastReceiver { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
          String action = intent.getAction(); 
          Log.w("C2DM", "Message Receiver called"); 
          if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) { 
              Log.w("C2DM", "Received message"); 
              String payload = intent.getStringExtra("payload"); 
              Log.d("C2DM", "dmControl: payload = " + payload); 
              // TODO Send this to my application server to get the real data 
              // Lets make something visible to show that we received the message 
              createNotification(context, payload); 
          } 
      } 



      public void createNotification(Context context, String payload) { 
          NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
          Notification notification = new Notification(R.drawable.icon, 
                  "Message sent!", System.currentTimeMillis()); 
          // Hide the notification after its selected 
          //notification.flags |= Notification.FLAG_AUTO_CANCEL; 
          notification.ledARGB = 0xff00ff00; 
          notification.ledOnMS = 300; 
          notification.ledOffMS = 1000; 
          notification.flags |= Notification.FLAG_SHOW_LIGHTS; 


          Intent intent = new Intent(context, MessageReceivedActivity.class); 
          intent.putExtra("payload", payload); 
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
          intent.putExtra("NotifID", 1); 
          PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent, 0); 
          notification.setLatestEventInfo(context, "Message","Message Recieved", pendingIntent); 
          notificationManager.notify(0, notification); 
      } 

}

통지 메시지를 접수에 클래스는 다음

import android.app.Activity; 
import android.app.NotificationManager; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.widget.TextView; 

public class MessageReceivedActivity extends Activity { 
      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
          setContentView(R.layout.activity_result); 
          NotificationManager notificationManager = (NotificationManager) this 
                  .getSystemService(Context.NOTIFICATION_SERVICE); 
          //---cancel the notification--- 
          int id=getIntent().getExtras().getInt("NotifID"); 
          notificationManager.cancelAll();  
          Bundle extras = getIntent().getExtras(); 
          if (extras != null) { 
              String message = extras.getString("payload"); 
              if (message.equals("call")) { 
                  Intent intent = new Intent(Intent.ACTION_CALL); 
                  intent.setData(Uri.parse("tel:9916261960")); 
                  startActivity(intent); 
              } else if (message.equals("camera")) { 
                  Intent cameraIntent = new Intent(
                          MediaStore.ACTION_IMAGE_CAPTURE); 
                  startActivity(cameraIntent); 
              } else { 
                  if (message != null && message.length() > 0) { 
                      TextView view = (TextView) findViewById(R.id.result); 
                      view.setText(message); 
                  } 
              } 
          } 
          super.onCreate(savedInstanceState); 
      } 

}

여기서, 기타 .getString ("payload"); 항상 처음 보낸 알림 메시지를 보유합니다.

답변

12

보류중인 의도를 만드는 무효 onNewIntent 보호

@Override 내부 의도를 가져 그렇지 않으면 기존의 목적은 새로운 엑스트라없이 재사용하는 FLAG_UPDATE_CURRENT

PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

에게

+0

작동. 고맙습니다. – jasdmystery

+0

동일한 문제가 있습니다. "FLAG_UPDATE_CURRENT"이 (가) 나를 위해 작동하지 않습니다. 나는 여전히 옛 메시지를 받고있다. – viji

+0

나를 위해 작동, 감사합니다 – TheMan

1

시도를 사용 (의도 의도) {

super.onNewIntent(intent); 

    Bundle extras = intent.getExtras(); 
    fromScreen = getIntent().getIntExtra("FROMSCREEN", 
      Config.SHARE_SCREEN_TAG); 
//enter code here 

}

관련 문제