Android 安卓:收到新通知时如何打开屏幕?

Android 安卓:收到新通知时如何打开屏幕?,android,firebase,push-notification,firebase-cloud-messaging,android-notifications,Android,Firebase,Push Notification,Firebase Cloud Messaging,Android Notifications,我尝试了几乎所有的方法,但我不明白在后台模式下收到新通知时如何打开手机屏幕 我用的是FCM。当应用程序位于后台时,收到的通知由系统托盘管理。收到新内容时,如何要求系统托盘打开屏幕?谢谢大家! 试试这个 您必须做一件事,当您显示通知时,您必须广播此意图。在onMessageReceived(RemoteMessage RemoteMessage)FirebaseMessagingService的方法中生成通知后 Intent in=new Intent(Intent.ACTION_POWER_CO

我尝试了几乎所有的方法,但我不明白在后台模式下收到新通知时如何打开手机屏幕

我用的是FCM。当应用程序位于后台时,收到的通知由系统托盘管理。收到新内容时,如何要求系统托盘打开屏幕?谢谢大家!

试试这个

您必须做一件事,当您显示通知时,您必须广播此意图。在
onMessageReceived(RemoteMessage RemoteMessage)
FirebaseMessagingService的
方法中生成通知后

Intent in=new Intent(Intent.ACTION_POWER_CONNECTED);
        getActivity().sendBroadcast(in);

最后,答案就在这里

如果您使用Firebase API将通知从服务器发送到设备,请记住extended FirebaseMessagingService中的onMessageReceived()方法,如果您发送这样的通知,则仅在后台调用:

                        $msg = array
                        (
                            'body'  => $newsObj['description'],
                        );
                        $fields = array
                        (
                            'registration_ids'  => $registrationIds,
                            'data'          => $msg,
                            'priority'  => "high"
                        );
您可以看到,如果将“数据”属性更改为“通知”,则仅当应用程序位于前台时才会调用onMessageReceived()方法

要在收到新通知时打开屏幕,请将以下代码放入onMessageReceived()方法中:

        // Turn on the screen for notification
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        boolean result= Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT_WATCH&&powerManager.isInteractive()|| Build.VERSION.SDK_INT< Build.VERSION_CODES.KITKAT_WATCH&&powerManager.isScreenOn();

        if (!result){
            PowerManager.WakeLock wl = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MH24_SCREENLOCK");
            wl.acquire(10000);
            PowerManager.WakeLock wl_cpu = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MH24_SCREENLOCK");
            wl_cpu.acquire(10000);
        }
//打开屏幕进行通知
PowerManager PowerManager=(PowerManager)getSystemService(POWER\u服务);
布尔结果=Build.VERSION.SDK_INT>=Build.VERSION_code.KITKAT_WATCH&powerManager.isInteractive()| Build.VERSION.SDK_INT
在AndroidManifest.xml中:

    <uses-permission android:name="android.permission.WAKE_LOCK" />


就这些

我正在使用php从服务器发送通知。因此,通知不会在设备上本地处理。在这种情况下,我应该把这段代码放在哪里?在服务器上用php定义通知体的位置。我正在通过firebase api发送通知。从中调用此notificationManager.notify();声明?我不是在设备上打电话。。服务器运行脚本后,通知会自动显示在屏幕上。唯一的问题是屏幕没有打开,我不知道怎么做。
PowerManager.FULL\u WAKE\u LOCK
已被弃用,没有它怎么办?