Reputation: 2497
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);
Upvotes: 12
Views: 9056
Reputation: 1200
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
Upvotes: 7
Reputation: 925
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);
Upvotes: 3
Reputation: 2071
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
Upvotes: 16
Reputation: 173
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.
Upvotes: 1