I create notifications and when I click on them, I open the activity. How can I find out from this activity the id of the notification that caused its opening?
1 answer
In this case, you can simply add Extra
's.
Here is the launch code for the notification:
int id = ΡΠ²ΠΎΠΉ id ΡΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΡ; Intent myIntent = new Intent(...); myIntent.putExtra("myNotificationId", id); <<< ΠΊΠ»Π°Π΄ΡΠΌ ΡΠ²ΠΎΠΉ id myNotificationManager.notify(id, new android.app.Notification.Builder(context) ... .setContentIntent(PendingIntent.getActivity(context, requestCode, myIntent, flags)) <<< ΠΏΠΈΡ
Π°Π΅ΠΌ Π² ΡΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΠ΅ ...
Here is the code for getting your notification ID from the running Activity
(this is in the onCreate()
method):
getIntent().getIntExtra("myNotificationId", 0)
- Exactly, thanks! But since, in my case, MainActivity is used, which at this moment can already be created, it is necessary to call onNewIntent (getIntent ()) in onCreate (); , and override this method public void onNewIntent (Intent intent) {...} - Vlad Alekseev
|