1

当用户单击通知时,我有一个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()调用始终为空。为什么我无法从意图中获取值?

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

将字符串从一个 Activity 传递到 Android 中的另一个 Activity

Android - 我如何发送 GCM 推送通知,其中包含要加载哪个活动的说明?

4

2 回答 2

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当用户点击通知时被调用。

于 2013-08-24T04:19:59.047 回答
-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;
于 2013-08-24T04:44:11.760 回答