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.
When an activity is launched(when it is first started ) or relaunched(when it's brought to the top of the stack) the
getIntent().getExtra()
won't givenull
. But when the activity is present on the top of the stack then on starting that activity with the use ofPendingIntent
would not relaunch the activity(onCreate()
won't get called) insteadonResume()
would be called. AndgetIntent().getExtra()
would return the value which is associated with the intent that started the activity(which isnull
).In order to update the intent do the following steps:
1). Set
FLAG_ACTIVITY_SINGLE_TOP
andFLAG_ACTIVITY_CLEAR_TOP
flags in your intent.2). Override
onNewIntent()
in the activity wheregetIntent().getExtra()
is called. This method is called byFLAG_ACTIVITY_SINGLE_TOP
For more info: See this doc
I was working in a local notifications plugin for Unity when I encountered the same problem -> I launched the app, scheduled the local notification, sent the app to background, local notification appeared and, when I clicked on it, app was resumed and
getIntent().getExtras()
wasnull
.In my case, the problem was I was trying to get the extras at the wrong intent. Try to print the intents with a simple toString, at the creation moment and when you get it, to ensure that the received is what you expect.
Also take a look to the onNewIntent method.
I just got the answer, add line:
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
NOTICE: if you add it as
resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
It won't work.I also tried other flags like, "FLAG_ACTIVITY_NEW_TASK" and "FLAG_ACTIVITY_RESET_TASK_IF_NEEDED". neither works here.
in PendingIntent use this flag
PendingIntent.FLAG_UPDATE_CURRENT
it's work for meFor me, in addition to setting Intent.FLAG_ACTIVITY_SINGLE_TOP , I had to add a unique action to the intent:
.. without the setAction(..), Extras is null on the Intent received by my NotificationActivity.
This post helps explain it: https://stackoverflow.com/a/3128271/2162226