0

In my application, i need to launch an activity and turn screen on, when high-priority FCM message is received. It's an alarm activity, which is very, very important for users. On most Android devices, the code is working fine. However, on some Huawei or LG devices, the activity is not launched when the device is in Doze mode, or in the pocket (proximity sensor). The behaviour should be similar like Alarm clocks, calls etc. Here is my code:

FirebaseMessagingService:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
     Intent dialogIntent = new Intent(getBaseContext(), AlarmActivity.class);
     dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
     dialogIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
     dialogIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
     getApplication().startActivity(dialogIntent);
}

Alarm Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // set flags so an activity fire on the screen
    getWindow().addFlags(
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
    );

    setContentView(R.layout.activity_alarm);
    .
    .
}

I wanted to use SCREEN_BRIGHT_WAKE_LOCK before I launch an activity, but it's deprecated.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
Kelib
  • 648
  • 2
  • 7
  • 15
  • See if the system is logging any messages in LogCat related to your failed activity launch. Or, advise users to add your app to the battery optimization whitelist. – CommonsWare May 18 '18 at 13:36
  • Battery optimization whitelisting doesn't help. I will try to get the device, which is not working properly. This issue is reported by users, on my debug devices it is working well. – Kelib May 18 '18 at 13:55

1 Answers1

1

I am using in my activity this code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    setShowWhenLocked(true);
                    setTurnScreenOn(true);
                    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
                    if (keyguardManager != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        keyguardManager.requestDismissKeyguard(this, null);
                    }
                } else {
                    //noinspection deprecation
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    );
                }
    }
    .....
}

To prevent different behavior on different devices I use in my activity layout this:

<android.support.design.widget.CoordinatorLayout
    ....
    android:keepScreenOn="true"
    >
MeLean
  • 3,092
  • 6
  • 29
  • 43
  • Is your code working reliable? These issues was reported on Android N, so the only difference is in use of keepScreenOn attribute in layout resource. – Kelib May 18 '18 at 13:53
  • did you notice setShowWhenLocked(true); setTurnScreenOn(true); methods. I had test it on several SDKs and it works – MeLean May 18 '18 at 13:57
  • These are methods available from Android O. On Android N, where the problem occurs, you are using same flags. However, thank you for your reply, I will add it to my code to better target Oreo. – Kelib May 18 '18 at 14:11