为什么我的Activity始终显示来自GCM通知的相同数据集?

时间:2014-08-08 06:10:08

标签: 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());

}

}

这是我点击通知后启动的活动。

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有关,但我不确定这里发生了什么,因为我在Android中使用PendingIntent和通知相对较新。谁能告诉我发生了什么事?

1 个答案:

答案 0 :(得分:5)

sendNotification中,您可以使用以下代码获取PendingIntent

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        intent, 0);

首次执行此操作时,Android会使用您提供的PendingIntent作为此调用的第3个参数创建新的Intent。在您执行此操作时,Android不会创建新的PendingIntent,但会返回引用您创建的第一个PendingIntent的令牌。

如果您希望Android在下次执行此代码时替换原始PendingIntent上的“额外内容”,则需要使用PendingIntent.FLAG_UPDATE_CURRENT作为调用{{1}中的第4个参数}}。这仍然会返回一个令牌,该令牌会引用您创建的第一个getActivity(),但它也会替换原始PendingIntent中的所有“附加内容”,PendingIntent中存在“附加内容” }作为第3个参数传递。