从FCM启动活动

时间:2018-05-18 13:28:58

标签: android firebase firebase-cloud-messaging android-doze

在我的应用程序中,我需要在收到高优先级FCM消息时启动活动并打开屏幕。它是一种警报活动,对用户来说非常非常重要。 在大多数Android设备上,代码运行正常。但是,在某些华为或LG设备上,当设备处于打盹模式或口袋(接近传感器)时,活动不会启动。行为应该类似于闹钟,呼叫等。 这是我的代码:

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);
}

警报活动:

@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);
    .
    .
}

我想在发布活动之前使用SCREEN_BRIGHT_WAKE_LOCK,但已弃用。

1 个答案:

答案 0 :(得分:0)

我在我的活动中使用此代码:

@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
                    );
                }
    }
    .....
}

为了防止我在我的活动布局中使用的不同设备上的不同行为:

<android.support.design.widget.CoordinatorLayout
    ....
    android:keepScreenOn="true"
    >
相关问题