21

我想從通知發送信息調用活動,而從我的活動,我得到空。Android通知PendingIntent額外null

通知的代碼是:

private void showNotification() { 
Intent resultIntent = new Intent(this, MainActivity.class); 
if (D) 
    Log.d(TAG, "Id: " + Id); 
resultIntent.putExtra("ineedid", deviceId); 

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
stackBuilder.addParentStack(MeterActivity.class); 
stackBuilder.addNextIntent(resultIntent); 
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, 
    PendingIntent.FLAG_UPDATE_CURRENT); 
// Bundle tmp = resultIntent.getExtras(); 
// if (tmp == null) { 
// Log.d(TAG, "tmp bundle is null"); 
// } else { 
// long id = tmp.getLong("ineedid", -1); 
// Log.d(TAG, "tmp id : " + id); 
// } 
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
    BLEMessengerService.this) 
    .setSmallIcon(R.drawable.ic_action_search) 
    .setContentTitle("Event tracker") 
    .setContentText("Events received").setOngoing(true) 
    .setContentIntent(resultPendingIntent) 
    .setWhen(System.currentTimeMillis()); 

int mId = R.string.service_notification_start_service; 
mNM.notify(mId, mBuilder.getNotification()); 
} 

代碼獲得從主要活動意向信息;

Bundle extras = getIntent().getExtras(); 
if (extras != null) { 
    long deviceID = getIntent().getLongExtra("ineedid", 
     -1); 
    if (ID == -1) { 
    if (D) 
     Log.i(TAG_D, "Wrong Id received."); 
    finish(); 
    } else { 
    device = dataSource.getDeviceByID(deviceID); 
    if (D) 
     Log.i(TAG_D, "Get the id."); 
    } 
} else { 
    if (D) 
    Log.d(TAG_D, "Bundle is null"); 
    finish(); 
} 

我已在通知得到通知之前進行了驗證,捆綁包不爲空,並且它的id爲extras。 雖然,當我試圖從意圖中獲取它時,它消失了。幫幫我。

+0

我也嘗試添加 「resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);」,但沒有工作。 – antonio081014

+1

有沒有原因你不使用'resultPendingIntent.getExtras()'? – PearsonArtPhoto

+0

我剛剛做了這些行來檢查我的pendingIntent是否有額外功能,並驗證了它具有我放在那裏的內容。 – antonio081014

回答

8

我剛剛得到了答案, 添加一行:resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

注意:如果你將它添加爲resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 它不會工作。

我也嘗試過其他的標誌,如 「FLAG_ACTIVITY_NEW_TASK」 和 「FLAG_ACTIVITY_RESET_TASK_IF_NEEDED」。在這裏都不行。

+1

我正在嘗試同樣的事情。 文檔中爲何需要此標誌的任何線索? 它只是說「如果設置,如果活動已經在歷史堆棧頂部運行,則活動將不會啓動。」 – mparaz

21

對於我來說,除了設置Intent.FLAG_ACTIVITY_SINGLE_TOP,我有一個獨特的操作添加到意圖:

Intent resultIntent = new Intent(caller, NotificationActivity.class); 

    String xId = getNextUniqueId(); 
    resultIntent.putExtra("x_id", xId); 
    resultIntent.setAction(xId); 
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 

..沒有的setAction(..),額外爲空收到的意向通過我的NotificationActivity。

這篇文章可以幫助解釋:https://stackoverflow.com/a/3128271/2162226

13

中的PendingIntent使用此標誌PendingIntent.FLAG_UPDATE_CURRENT這是爲我工作

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
0

我是在一個本地通知插件的團結工作,當我遇到同樣的問題 - >我啓動了應用程序,計劃了本地通知,將應用程序發送到了後臺,出現了本地通知,當我點擊它時,應用程序已恢復,getIntent().getExtras()null

在我的情況下,問題是我試圖在錯誤的意圖得到了臨時演員。嘗試使用簡單的toString打印意圖,在創建時和獲取它時確保收到的是您所期望的。

也看看到onNewIntent方法。