Andrew
Andrew

Reputation: 23

Open activity from foreground notification when firebase data has changed

I feel like I am doing something stupid here. I have set up a service which listens to changes in a collection in my firebase database, and when there has been a change, the app is meant to open, except the activity doesn't open. Both the log message and the toast appear when the data is changed in the collection, but the activity doesn't open. The code from the onStartCommand is below.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    String input = intent.getStringExtra("inputExtra");
    context = getApplicationContext();
    Intent notificationIntent = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Example Service")
            .setContentText(input)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentIntent(pendingIntent)
            .build();
    startForeground(1, notification);


    reference.addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
            for (DocumentChange documentChange : value.getDocumentChanges()) {
                if (documentChange.getType() == DocumentChange.Type.MODIFIED) {
                    Log.d(TAG, "onComplete: reference modified");
                    Toast.makeText(context, "message received", Toast.LENGTH_SHORT).show();
                    Intent intent1 = new Intent(context, Open.class);
                    intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent1);

                }
            }
        }
    });

    return START_STICKY;
}

Any help would be greatly appreciated.

Upvotes: 0

Views: 143

Answers (1)

Umer Farooq
Umer Farooq

Reputation: 593

Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.

For the purposes of starting activities, an app running a foreground service is still considered to be "in the background"

Alternatives to display activity

Apps that are in the background should display time-sensitive notifications to provide urgent information to the user instead of directly starting an activity.

Exceptions to the restrictions: There are some exceptions in which app can display activity directly, some of those are:

  • The app has a visible window, such as an activity in the foreground.
  • The app has an activity in the back stack of the foreground task.
  • The app has an activity in the back stack of an existing task on the Recents screen.

For more detailed articles, read this

Upvotes: 1

Related Questions