For someone who will be looking for a similar question. Starting with Android Q, it is limited to trigger any action when the application is in the background. To get started, you need to display a notification using PRIORITY_HIGH and set the full-screen intent . So 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 ); NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); nManager.notify(10, notifyBuilder.build())
This will display a notification and start your activity immediately. And in your onStart() activity, you can reject the notification, as shown below:
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); nManager.cancel(10);
NOTE : Remember to initiate the channel. Starting with Android O, this is a must
source share