2013-08-24 6 views
1

У меня есть GCMIntentService, где я устанавливаю детали для намерения и все, когда пользователь нажимает на уведомление. Мой фрагмент кода отношение к этим ниже:Intent теряет значения в дополнениях

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?

+0

Вы уверены, что 'if' условие всегда верно? – ChiefTwoPencils

+0

Является ли 'CustomTabActivity' уже запущенным в фоновом режиме, когда отправляется сообщение" notificationIntent "? – Vikram

+0

Если всегда верно, когда я запускал его с точками останова. «CustomTabActivity» иногда находится в фоновом режиме, а иногда нет. Но, несмотря на это, все еще не удается. – KVISH

ответ

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

Попробуйте:

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; 
Смежные вопросы