简体   繁体   English

在 Android Q 的锁定屏幕上收到 fcm 通知后开始活动

[英]Starting activity after receive fcm notification on lock screen in Android Q

For my application, I need to realize behaviour like WhatsApp, show my application above the lock screen when a device is locked, and I did it successfully in android versions below Android Q. To do that I grant Settings.ACTION_MANAGE_OVERLAY_PERMISSION .对于我的应用程序,我需要实现类似 WhatsApp 的行为,在设备被锁定时在锁定屏幕上方显示我的应用程序,并且我在 Android Q 以下的 android 版本中成功地做到了。为此,我授予Settings.ACTION_MANAGE_OVERLAY_PERMISSION Does anybody know how to do it without SYSTEM_ALERT_WINDOW permission ?有没有人知道如何在没有SYSTEM_ALERT_WINDOW permission的情况下做到这一点? For push notification I use fcm.对于推送通知,我使用 fcm。

My code:我的代码:

private void tryWakeUp() {
        try {

            String ns = getApplicationContext().getPackageName();
            String cls = ns + ".MainActivity";
            Intent intent = new Intent(getApplicationContext(), Class.forName(cls));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.putExtra("foreground", true);
            intent.putExtra("incoming_call", true);

            PowerManager pm = (PowerManager) getApplicationContext()
                    .getSystemService(Context.POWER_SERVICE);

            PowerManager.WakeLock wl1 = pm.newWakeLock(
                    PowerManager.ACQUIRE_CAUSES_WAKEUP |
                            PowerManager.ON_AFTER_RELEASE |
                            PowerManager.FULL_WAKE_LOCK,
                    "wl1"
            );
            wl1.acquire(10000);

            Log.d(TAG, "try wake up");

            startActivity(intent);
        } catch (Exception e) {
            Log.w(TAG, "Failed to open application on message receive", e);
        }
    }

This code executes after I receive data push notification.此代码在我收到数据推送通知后执行。

Use setVisiblity() from NotificationBuilder added in API level 21 Android 5.0):使用在 API 级别 21 Android 5.0 中添加的NotificationBuilder中的setVisiblity() ):

notificationBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);

public NotificationCompat.Builder setVisibility (int visibility) Sets Notification.visibility. public NotificationCompat.Builder setVisibility (int visibility) 设置 Notification.visibility。

Parameters参数
visibility int : One of Notification.VISIBILITY_PRIVATE (the default), Notification.VISIBILITY_PUBLIC , or Notification.VISIBILITY_SECRET .可见性 intNotification.VISIBILITY_PRIVATE (默认)、 Notification.VISIBILITY_PUBLICNotification.VISIBILITY_SECRET之一。

WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED has deprecated in Android Q. Instead you can use Activity.setShowWhenLocked(true) and Activity.setTurnScreenOn(true) in onCreate of Activity to show activity in lock screen. WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 在 Android Q 中已弃用。相反,您可以在 Activity 的 onCreate 中使用 Activity.setShowWhenLocked(true) 和 Activity.setTurnScreenOn(true) 以在锁定屏幕中显示活动。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM