2011-06-03 33 views
0

我在我的應用程序中有2個活動,我希望活動X彈出一個通知 - 一旦它的點擊活動Y打開並從通知中讀取參數。如何在通知內傳遞額外信息?

所以我寫了這一點:

 String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

    CharSequence tickerText = NOTIFY_TICKER_NAME; 
    long when = System.currentTimeMillis(); 

    Notification notification = new Notification(R.drawable.icon, tickerText, when); 

    Context context = getApplicationContext(); 
    CharSequence contentTitle = "My notification"; 
    CharSequence contentText = "Hello World!"; 
    Intent notificationIntent = new Intent(this, PopTask.class); 
    notificationIntent.putExtra("task", "http://www.google.com"); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

    mNotificationManager.notify(1, notification); 

問題是,當我嘗試讀取演員 - 我得到空。

我該怎麼辦?

感謝

回答

3

oshafran,

爲了putExtra()在這種情況下,你也可以選擇使用setAction()工作。我不完全確定爲什麼會出現這種情況...似乎是一些怪癖。在setAction()中傳遞的值可以是任何值。

嘗試:

Intent notificationIntent = new Intent(this, PopTask.class); 
notificationIntent.putExtra("task", "http://www.google.com"); 
notificationIntent.setAction("MyIntent"); 
+0

THX使用PendingIntent.FLAG_UPDATE_CURRENT!這是我的缺失環節,使他們工作! :-)投了! – 2014-06-04 23:58:15

+0

真的很奇怪的修復,但感謝它的工作。 – Neil 2014-10-14 11:34:00

0

也許,好一點的解決辦法是,當你創建你的PendingIntent

Intent intent = new Intent(context, YourActivity.class); 
intent.putExtra(YOUR_EXTRA_KEY, "Your Extra"); 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);