1

Note: below code mention in MyFirebaseMessageingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {
.......
 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
..............
ValidateCall(validateRingerRequestModel, new ApiCallback() {
                @Override
                public void onResponse(boolean success, Object response) {
                    if (response != null) {
                        ValidateRingerResponseModel responseModel = (ValidateRingerResponseModel) response;
                        if (responseModel.status) {

                            Intent intent = new Intent(getApplicationContext(), Notification.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra("data", model);
                            startActivity(intent);
                        }
                    }
                }
            });
}

"Notification" is a simple activity which shows some buttons with a click event actions. When application is running on foreground and notification comes in, then only activity starts but when application is closed or minimized or device is in sleep mode (battery saver mode or not used for long time), then above code gets executed but Notification.java activity is not opened.

That issue is faced by the devices with newer android versions like android 9, for older versions like android 6, it works.

Is the above code missing something or is there any other way that can work on all android versions?

Please suggest. Thanks.

  • 1
    Please post your Notification builder code also – Rahul Gaur Feb 08 '20 at 10:58
  • Does this answer your question? [How to handle the Firebase notification when app is in foreground](https://stackoverflow.com/questions/38451235/how-to-handle-the-firebase-notification-when-app-is-in-foreground) – Abhinav Suman Feb 10 '20 at 06:53

1 Answers1

1

You can't start activity from background from API 29. As per the docs

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.

https://developer.android.com/guide/components/activities/background-starts#exceptions

Sunny
  • 14,522
  • 15
  • 84
  • 129