android - 为什么我的 Activity 总是显示来自 GCM 通知的同一组数据?

标签 android notifications google-cloud-messaging android-pendingintent

我已经实现了一个简单的 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/

相关文章:

java - Android float 通知对话框

Android + Xamarin 推送通知自定义声音不会播放

java - Android GCM Java Server - 没有消息数据的推送通知

android - PRODUCT_ID/SenderID 是否只是 Google Cloud Messaging 中的唯一标识符

java - Android Eclipse - 看不到注释处理选项

android - 在应用程序类中保存静态上下文变量

linux - 如何阻止appimage "Desktop Integrate - AppImageLauncher"通知(提示)?

cordova - 在AppBrowser中更改cordova的url

android - 我可以在 genymotion 中添加任何 mi 设备虚拟设备吗

android - 将调试/日志语句放入代码中的有效方法 - 因此它们不会影响运行时