意图正在失去额外的价值

时间:2013-08-24 00:21:19

标签: android android-intent google-cloud-messaging

我有GCMIntentService我在其中设置了意图的详细信息以及用户点击通知时的所有内容。我的相关代码段如下:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, CustomTabActivity.class);
if (noteId != null && value != null) {
    notificationIntent.putExtra("noteId", noteId);
    notificationIntent.putExtra("value", value);
}

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification updateComplete = new NotificationCompat.Builder(context)
        .setContentTitle(title)
        .setContentText(msg)
        .setTicker(title)
        .setWhen(System.currentTimeMillis())
        .setContentIntent(contentIntent)
        .setDefaults(Notification.DEFAULT_SOUND)
        .setAutoCancel(true)
        .setSmallIcon(R.drawable.icon)
        .build();

notificationManager.notify(100, updateComplete);

每当打开CustomTabActivity.class时,getExtras()调用始终为空。为什么我无法从意图中获取值?

我为此阅读了以下内容,但无法解决:

Pass a String from one Activity to another Activity in Android

Android - how can I send a GCM push notification with instructions of which activity to load?

2 个答案:

答案 0 :(得分:1)

我在这里找到了大部分问题。事实证明,由于我有singleTop的活动,getIntent()的意图是旧的。此代码有效:

protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent.getExtras() != null) {
        String noteId = intent.getExtras().getString("noteId");
        String value = intent.getExtras().getString("value");
    }
}
当用户点击通知时,会调用

onNewIntent

答案 1 :(得分:-1)

试试这个:

private static int count = 1;

NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
            PendingIntent contentIntent = PendingIntent.getActivity(mContext, (int)(Math.random() * 100), new Intent(),PendingIntent.FLAG_UPDATE_CURRENT);

 long when = System.currentTimeMillis();

Notification notification = new Notification(icon, gcmData.getString("message"), when);
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notification.setLatestEventInfo(mContext, gcmData.getString("type"), intent.getExtras().getString("message"), contentIntent);
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(count, notification);
            count = count + 1;