10

I have a notification and when I select, it sends a Broadcast to a BroadcastReceiver using a PendingIntent. In the onReceive I start a new Activity.

However, if I remove my app from recent apps opened (or the notification sit in the draw for a long time) this scenario occurs:

When I have multiple notifications in the drawer the first one opens great. After tapping on the second one my onCreate() nor my onResume() are being called and its as if the startActivity() is not working at all. If I add the flag Intent.FLAG_ACTIVITY_SINGLE_TOP then onNewIntent is being called.

notificationIntent = new Intent();
notificationIntent.setAction(AppConstants.ACTION_ACTIVITY);
notificationIntent.putExtra("key", value);
int requestID = (int) System.currentTimeMillis();

mBuilder.setContentIntent(PendingIntent
                    .getBroadcast(context, requestID, notificationIntent, 
                                  PendingIntent.FLAG_UPDATE_CURRENT));

onReceive

  Intent intent = new Intent(context, Activity.class);
  intent.putExtra("key", value);
  //IF I ADD FLAG_ACTIVITY_SINGLE_TOP IT WORKS
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startActivity(intent);
Cœur
  • 37,241
  • 25
  • 195
  • 267
user1163234
  • 2,407
  • 6
  • 35
  • 63

4 Answers4

14

Try using flags as follows for the intent.

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

And for Pending intent use PendingIntent.FLAG_UPDATE_CURRENT

Roadblock
  • 2,041
  • 2
  • 24
  • 38
4

For someone, who will look for a similar question. Beginning from Android Q it's restricted to launch any activity, when app in background. To launch your activity, you need to displaying a notification with PRIORITY_HIGH and set the full-screen intent. So, totally you should have something like this:

//Create your intent as usual
Intent intent = new Intent(context, Activity.class);
intent.putExtra("key", value);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

// Build a pending intent 
PendingIntent pIntent = PendingIntent.getActivity(context, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

//Notification builder
NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(title)
                .setPriority(PRIORITY_HIGH)
                .setFullScreenIntent(pIntent, true /*isHighPriority*/);

NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(10, notifyBuilder.build())

This will display notification and will launch your activity immediately. And in your activity onStart() you can dismiss a notification like below:

NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.cancel(10);

NOTE: Don't forget to init channel. Beginning from Android O it's a mandatory thing

Dmitriy Mitiai
  • 1,112
  • 12
  • 15
  • Using the above core on wear os, I am not able to open the app. But I am able to generate the notification. Any suggestion? – SANAT Nov 25 '22 at 06:04
3

in order to start activity from BroadcastReciever you should add flag of FLAG_ACTIVITY_NEW_TASK

 Intent imap =new Intent(context,MainActivity.class);
                  imap.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.startActivity(imap);
Hussein Alrubaye
  • 915
  • 8
  • 14
1

An activity can be started from Broadcast Receiver even if it is not a launcher. That shouldn't be the problem.

Try passing a unique requestid,

notificationIntent = new Intent();
notificationIntent.setAction(AppConstants.ACTION_ACTIVITY);
notificationIntent.putExtra("key", value);

mBuilder.setContentIntent(PendingIntent.getBroadcast(context, value, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT));

See this question.

Community
  • 1
  • 1
Sajith Sageer
  • 163
  • 3
  • 16