Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Return to App on Notification Click

I am showing a notification in Android. This is working fine. Now I want to focus MyApp if the User clicks on the Notification. I know I can pass an Intent to the Notification, so I could start any Activity I want. But I do not wont to start a specific activity, I just want to go back into the app. And if the App is not running then I want to start the app normally and also not a specific Activity.

So those three scenarios:

A: MyApp is running in background, but does not have focus -> Focus MyApp

B: MyApp is not running -> Start MyApp normally

C: MyApp is running and also has focus -> Do nothing

My Code for the Notification:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
            .setContentTitle(title)
            .setContentIntent(intent) // -> here I could parse an Intent, I know... 
                                      //    But that is not exactly what I want

I found nothing on Google for that. Is that possible?

like image 410
julienduchow Avatar asked Dec 21 '25 19:12

julienduchow


1 Answers

Try this as pending intent:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

This will :

  1. If app not running, start the app.
  2. If in foreground, will do nothing, focus again to the app.
  3. If in background, (most probably, as far as I can recall) will focus the app and will not create activity if already created.
like image 156
touhid udoy Avatar answered Dec 23 '25 07:12

touhid udoy