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 1

    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