5

Tôi có triển khai thông báo GCM hoạt động hoàn hảo. Tuy nhiên, vấn đề là một khi thông điệp đã được nhận trong mục đích trong phương thức nhận được, thông điệp được hiển thị luôn là thông điệp cũ. Đó là 'extras.getString ("payload")' luôn hiển thị thông báo cũ. Tôi không thể tìm ra vấn đề là gì.Thông báo đẩy GCM cho Android luôn hiển thị thông điệp cũ. Ý định nhận được không chính xác

Lớp đó sẽ gửi các thông báo GCM là:

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

}

Lớp mà recieves thông điệp thông báo là:

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

}

Ở đây, tính năng bổ sung .getString ("tải trọng"); giữ lần đầu tiên gửi tin nhắn thông báo.

Trả lời

12

Khi tạo ý định cấp phát, sử dụng FLAG_UPDATE_CURRENT

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

Nếu không mục đích cũ được tái sử dụng mà không có tính năng bổ sung mới

+0

Hoạt động. Cảm ơn bạn. – jasdmystery

+0

tôi đang gặp vấn đề tương tự. nhưng "FLAG_UPDATE_CURRENT" không hoạt động đối với tôi. Tôi vẫn nhận được tin nhắn cũ. – viji

+0

phù hợp với tôi, cảm ơn bạn – TheMan

1

cố gắng để lấy ý định của bạn bên

@ Override protected void onNewIntent (Mục đích ý định) {

super.onNewIntent(intent); 

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

}

Các vấn đề liên quan