簡體   English   中英

在 Android Q 的鎖定屏幕上收到 fcm 通知后開始活動

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

對於我的應用程序,我需要實現類似 WhatsApp 的行為,在設備被鎖定時在鎖定屏幕上方顯示我的應用程序,並且我在 Android Q 以下的 android 版本中成功地做到了。為此,我授予Settings.ACTION_MANAGE_OVERLAY_PERMISSION 有沒有人知道如何在沒有SYSTEM_ALERT_WINDOW permission的情況下做到這一點? 對於推送通知,我使用 fcm。

我的代碼:

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

此代碼在我收到數據推送通知后執行。

使用在 API 級別 21 Android 5.0 中添加的NotificationBuilder中的setVisiblity() ):

notificationBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);

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

參數
可見性 intNotification.VISIBILITY_PRIVATE (默認)、 Notification.VISIBILITY_PUBLICNotification.VISIBILITY_SECRET之一。

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