21

通知から呼び出されたアクティビティに情報を送信しようとしていますが、自分のアクティビティからnullが返されました。Android通知PendingIntent Extras 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があることを確認しました。 インテントからフェッチしようとしたときに、それがなくなりました。助けて。 resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

注意:あなたがresultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); として追加した場合それは動作しません

+0

また、 "resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);"を追加しようとしましたが、作業はしませんでした。 – antonio081014

+1

'resultPendingIntent.getExtras()'を使用していない理由はありますか? – PearsonArtPhoto

+0

私はpendingIntentがExtrasを持っているかどうかを確認するためにこれらの行を作成しました。 – antonio081014

答えて

8

私は答え、 追加ラインを得ました。

「FLAG_ACTIVITY_NEW_TASK」や「FLAG_ACTIVITY_RESET_TASK_IF_NEEDED」などの他のフラグも試しました。どちらもここでは働かない。私にとって

+1

私は同じことを試みていました。 ドキュメントにこのフラグが必要な理由は何か? 「これが設定されていると、履歴スタックの一番上で既に実行されている場合、アクティビティは起動されません」と表示されます。 – mparaz

21

は、Intent.FLAG_ACTIVITY_SINGLE_TOPの設定に加えて、私は意思にユニークなアクションを追加する必要がありました:SetActionは(..)せずに

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); 

..エクストラは、受け取った意図にnullです私のNotificationActivityによって。

この投稿は、それを説明するのに役立ちます:PendingIntentでhttps://stackoverflow.com/a/3128271/2162226

13

は、このフラグPendingIntent.FLAG_UPDATE_CURRENTを使用し、それは私が同じ問題に遭遇したとき、私はUnityのためのローカル通知プラグインで働いていた私

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

ための作業です - >私はアプリを起動し、ローカル通知をスケジュールし、バックグラウンドにアプリを送った、ローカル通知が表示され、それをクリックすると、アプリが再開され、nullだった。

私の場合、問題は私が間違った意図でエクストラを手に入れようとしていたことでした。あなたが受け取ったものがあなたが期待しているものであることを確実にするために、創造の瞬間とそれを得るときに、簡単なtoStringでインテントを印刷してみてください。

また、onNewIntentメソッドを見てください。

関連する問題