Notification service put extra on notification click, how?
Solution 1
I'm guessing that your problem is here:
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),
0, intent, 0);
You are passing intent
to getActivity()
and expecting that you will get back a PendingIntent
that matches your Intent
and includes your extras. Unfortunately, if there is already a PendingIntent
floating around in the system that matches your Intent
(without taking into consideration your Intent
extras) then getActivity()
will return you that PendingIntent
instead.
To see if this is the problem, try this:
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
This says that if there is already a PendingIntent
that matches your Intent
somewhere in the system that it should replace the extras with the ones in your intent
parameter.
Solution 2
I had the same problem. You used getIntent().getExtras()
but you should have used getIntent().getStringExtra(id)
.
It's mentioned here too: Android: intent.putExtra in a Service Class to get notifications
Also it's considered not to be a good practice to use static variables as you've mentioned in your comment, I'd suggest to avoid it.
Rotary Heart
Updated on June 08, 2022Comments
-
Rotary Heart almost 2 years
Well I have a service that runs with a notification, I want to put an extra with the new intent opened when the user clicks the notification, so when
MainActivity
opens I can get the extra. I have tried with different ways, but I just don't find how to get it to work.Here's what I have tried:
This is MyService
Intent intent = new Intent(MyService.this, MainActivity.class); intent.putExtra("EXTRA", StringTEST); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent contentIntent = PendingIntent .getActivity(getApplicationContext(), 0, intent, 0);
this opens the activity fine, but I can't get the extra?? This is how I have tried to get it:
MainActivity
@Override public void onNewIntent(Intent intent){ Bundle extras = intent.getExtras(); if(extras != null){ String name = extras.getString("EXTRA"); System.out.println(name); } } @Override protected void onResume() { Bundle extras = getIntent().getExtras(); System.out.println(extras+""); if (extras != null) { try { onNewIntent(getIntent()); Intent i = getIntent(); String name = i.getStringExtra("EXTRA"); System.out.println(name); currentDir = new File(name); changeFiles(); } catch (Exception e) { e.printStackTrace(); } } super.onResume(); }
Those are the two ways I have tried, but I still get null... What am I doing wrong?
Thanks
-
Rotary Heart over 11 yearsTried this and I didn't get it either, but I managed to got it to work making it a
static String
I know is not the best way, but... -
David Wasser over 11 yearsGlad you got it working. I still don't understand what the problem is. Maybe you'll figure it out. If you do, please answer your own question so that others can benefit from it.
-
DaMachk about 9 yearsThis helped in my case. Thanks! :)
-
phil almost 9 yearsThank you very much! Now it is possible to get different extras into multiple notification
-
David Wasser almost 9 years@flipperweid There are many questions on stackoverflow about this. Just look. Basically you need to ensure your
PendingIntent
is unique. You can do that by using a uniquerequestCode
in each call toPendingIntent.getActivity()
or you can set a unique ACTION on eachPendingIntent
. -
phil almost 9 yearsHi, yes I use that. My comment was meant only as a "thank you!:-)" , cause before I use the 'FLAG_UPDATE_CURRENT every clicked Notification had the same extras altough I had set different extras in different notifications. Now it work's:-))
-
Siddharth Sachdeva over 6 yearsTo my surprise, it actually worked, however, I was casting it to string in the other case.