Android Notification PendingIntent Extras null

antonio081014 picture antonio081014 · Nov 28, 2012 · Viewed 20.4k times · Source

I am trying to send information from notification to invoked activity, while from my activity I got null.

The code for notification is:

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

Code for get information from intent in main activity;

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

I have verified before the notification get notified, bundle is not null, and it has id in extras. While, when I tried to fetch it from intent, it's gone. Help.

Answer

Oubaida AlQuraan picture Oubaida AlQuraan · Mar 28, 2016

in PendingIntent use this flag PendingIntent.FLAG_UPDATE_CURRENT it's work for me

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