我已经实现了一个简单的 GCM 客户端,但在更新每次收到新通知时都会启动的 Activity 时遇到了这个问题。每次我发送带有一组参数的通知时,只有我为第一个通知发送的参数出现在 UI 中。当我尝试更改参数时,它们没有显示(再次显示相同的数据)。
这是我的 GcmIntentService 类
public class GcmIntentService extends IntentService {
private static final String TAG = "GcmIntentService";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
Log.d(TAG, "GCM error -> MESSAGE_TYPE_SEND_ERROR");
sendNotification("Send error: " + extras.toString(), "", "",
"", "");
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
Log.d(TAG, "GCM error -> MESSAGE_TYPE_DELETED");
sendNotification(
"Deleted Messages on server: " + extras.toString(), "",
"", "", "");
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
Log.d(TAG,
"GCM success. Received data: fromUser="
+ extras.getString("fromUser") + " message="
+ extras.getString("message") + " srcLat="
+ extras.getString("srcLat") +" srcLng="
+ extras.getString("srcLng") + " dstLat="
+ extras.getString("dstLat") + " dstLng="
+ extras.getString("dstLng"));
sendNotification(
extras.getString("fromUser") + " "
+ extras.getString("message"),
extras.getString("srcLat"), extras.getString("srcLng"),
extras.getString("dstLat"), extras.getString("dstLng"));
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg, String srcLat, String srcLng,
String dstLat, String dstLng) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, ShowUser.class);
intent.putExtra("message", msg);
intent.putExtra("srcLat", srcLat);
intent.putExtra("srcLng", srcLng);
intent.putExtra("dstLat", dstLat);
intent.putExtra("dstLng", dstLng);
Log.d(TAG, "-> sendNotification -> Received parameters:\nmessage:"
+ msg + "\nsrcLat=" + srcLat + "\nsrcLng=" + srcLng
+ "\ndstLat=" + dstLat + "\ndstLng=" + dstLng);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("New Taxi Request Received")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
这是我的 Activity,它在点击通知时启动。
public class ShowUser extends Activity {
private TextView messageTextView;
private TextView showUsernameTextView;
private TextView srcLatTextView;
private TextView srcLngTextView;
private TextView dstLatTextView;
private TextView dstLngTextView;
private SharedPreferences prefs;
private String username;
private static final String TAG = "ShowUser";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_user_details);
Intent intent = getIntent();
String message = intent.getStringExtra("message");
String srcLat = intent.getStringExtra("srcLat");
String srcLng = intent.getStringExtra("srcLng");
String dstLat = intent.getStringExtra("dstLat");
String dstLng = intent.getStringExtra("dstLng");
Log.d(TAG, "ReceivedExtras: message=" + message
+ "srcLat=" + srcLat + "srcLng=" + srcLng
+ "dstLat=" + dstLat + "dstLng=" + dstLng);
prefs = getSharedPreferences(DriverLoginActivity.USER_CREDENTIALS, Context.MODE_PRIVATE);
username = prefs.getString(DriverLoginActivity.USERNAME, null);
if(username!= null) {
messageTextView = (TextView) findViewById(R.id.messageTextView);
showUsernameTextView = (TextView) findViewById(R.id.showUsernameTextView);
srcLatTextView = (TextView) findViewById(R.id.sourceLatTextView);
srcLngTextView = (TextView) findViewById(R.id.sourceLngTextView);
dstLatTextView = (TextView) findViewById(R.id.destinationLatTextView);
dstLngTextView = (TextView) findViewById(R.id.destinationLngTextView);
showUsernameTextView.setText("Welcome " + username);
messageTextView.setText(message);
srcLatTextView.setText("Source Latitude: " + srcLat);
srcLngTextView.setText("Source Longitude: " + srcLng);
dstLatTextView.setText("Destination Latitude: " + dstLat);
dstLngTextView.setText("Destination Longitude: " + dstLng);
}
}
}
我认为它可能与 PendingIntent
有关,但我不确定这里发生了什么,因为我对使用 PendingIntent
还比较陌生和 Android 中的通知。谁能告诉我发生了什么事?
最佳答案
在 sendNotification
中,您可以使用以下代码获取 PendingIntent
:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, 0);
第一次执行此操作时,Android 将使用您提供的作为此调用的第三个参数的 Intent
创建一个新的 PendingIntent
。以后您执行此操作时,Android 将不会创建新的 PendingIntent
,但会返回一个指向您创建的第一个 PendingIntent
的 token 。
如果你想让Android在你下次执行这段代码的时候替换原来的PendingIntent
上的“extras”,你需要使用PendingIntent.FLAG_UPDATE_CURRENT
作为第4个getActivity()
调用中的参数。这仍然会返回一个标记,该标记引用您创建的第一个 PendingIntent
,但它还会用当前的“extras”替换原始 PendingIntent
中的所有“extras”在 Intent
中作为第三个参数传递。
关于android - 为什么我的 Activity 总是显示来自 GCM 通知的同一组数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25197027/