简体   繁体   English

Android 在 Firebase 通知上将应用程序带到前台

[英]Android bring app to foreground on Firebase notification

I have the following code which brings my Android app to foreground once I receive a Firebase Push notification or FCM.一旦我收到 Firebase 推送通知或 FCM,我有以下代码将我的 Android 应用程序带到前台。

@ReactMethod
public void backToForeground() {

    Context context = getAppContext();
    String packageName = context.getApplicationContext().getPackageName();
    Intent focusIntent = context.getPackageManager().getLaunchIntentForPackage(packageName).cloneFilter();
    Activity activity = getCurrentActivity();
    boolean isOpened = activity != null;

    if (isOpened) {
        focusIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        activity.startActivity(focusIntent);
    } else {

        // Custom flag to check whether app was started from this method
        focusIntent.putExtra("FLAG_ON_CALL_BRING_TO_FRONT", "true");

        focusIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK +
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED +
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD +
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

        getReactApplicationContext().startActivity(focusIntent);

    }

}

Now in MainActivity I have used the bundle as following:现在在 MainActivity 中,我使用了如下捆绑包:

@Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen.show(this);
super.onCreate(savedInstanceState);
  Bundle extras = getIntent().getExtras();

        if (extras != null) {

            // Custom flag defined in AppStateManagerModule
            String extraString = extras.getString("FLAG_ON_CALL_BRING_TO_FRONT");

            if (extraString != null) {
                getWindow()
                        .addFlags(
                                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                                        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                                        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                        );
            }
        }
}

My code is working correctly in the following cases:我的代码在以下情况下正常工作:

When FCM is received on device:当设备接收到 FCM 时:

  1. If app is open, action is performed如果应用程序已打开,则执行操作
  2. If app is in background ie minimized, app opens and action is performed如果应用程序在后台,即最小化,应用程序打开并执行操作
  3. If app is killed, app opens and action is performed如果应用程序被杀死,应用程序打开并执行操作
  4. If app is killed and phone is locked, app opens on top of lock screen and then action is performed如果应用程序被杀死并且手机被锁定,应用程序会在锁定屏幕顶部打开,然后执行操作

Now here's the case which doesn't work properly:现在这是无法正常工作的情况:

  1. If I start my app and its in foreground or if its minimized and I lock my phone, my app is running fine and my code brings it to front, it performs the required operation BUT it doesn't show on top of lock screen.如果我在前台启动我的应用程序,或者如果它最小化并且我锁定了我的手机,我的应用程序运行良好并且我的代码将它带到前面,它会执行所需的操作,但它不会显示在锁定屏幕的顶部。

Action is performed fine in point 5 but it doesnt show on top of lock screen.动作在第 5 点执行得很好,但它没有显示在锁定屏幕的顶部。

If app is active or in background, after phone is locked it will still be considered active.如果应用程序处于活动状态或在后台,则在手机锁定后它仍将被视为活动状态。 So since an activity is active, it can't be thrown on top with intent since its already used to activate the activity.因此,由于活动处于活动状态,因此不能有意将其抛到顶部,因为它已经用于激活活动。

We need to add window flags on an active activity to bring it to front like this:我们需要在活动活动上添加 window 标志,以将其置于最前面,如下所示:

 if (isOpened) {

    focusIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

    activity.startActivity(focusIntent);

    // Adding Window Flags to bring app forward on lock screen

    activity.runOnUiThread(new Runnable() {

        @Override
        public void run() {

            activity.getWindow()
                    .addFlags(
                            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                                    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    );
                    
        }
    });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将FCM通知中的Android应用的前一个实例置于前台 - Bring Android app's previous instance to foreground from FCM notification 将正在运行的Android应用程序带到前台 - Bring A Running Android App to Foreground Android将应用重新带到前台 - Android bring App back to foreground 在Android中-当应用程序处于前台时如何防止Firebase推送通知? - In android - How to prevent Firebase push notification when app is in foreground? 在收到应用程序启动器之类的通知推送后,将Android应用程序置于前台 - Bring the Android app to foreground after recieving a notification push like the application launcher 设备休眠时将Android应用置于前台 - Bring Android app to foreground when device is sleeping 如何将Android应用程序在后台运行到前台 - Ways to bring an Android app in background to foreground IONIC/Capacitor:触发时将 Android 应用带到前台 - IONIC/Capacitor: Bring Android app to foreground on trigger 如何在Calabash-android中将应用程序带到Foreground? - How to bring app to Foreground in calabash-android? 接收GCM推送通知(Cordova)时将应用程序带到前台 - Bring app to foreground when receiving GCM Push notification (Cordova)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM