java - 在 Android Q 的锁定屏幕上收到 fcm 通知后开始 Activity

标签 java android push-notification

对于我的应用程序,我需要实现类似 WhatsApp 的行为,在设备锁定时在锁定屏幕上方显示我的应用程序,我在 Android Q 以下的 android 版本中成功地做到了这一点。为此,我授予 设置。 ACTION_MANAGE_OVERLAY_PERMISSION。有人知道如何在没有 SYSTEM_ALERT_WINDOW 权限 的情况下执行此操作吗?对于推送通知,我使用 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) Sets Notification.visibility.

Parameters
visibility int: One of Notification.VISIBILITY_PRIVATE (the default), Notification.VISIBILITY_PUBLIC, or Notification.VISIBILITY_SECRET.

关于java - 在 Android Q 的锁定屏幕上收到 fcm 通知后开始 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58988662/

相关文章:

java - 安卓(常量)

Java Spring 启动 : How to find all rows with multiple Ids like findAllByAge(ArrayList<Long> ageList)?

java - Collections.sort、Enum、Comparable接口(interface)问题

android - 按下设备的主页键后,如何始终在图标按下时打开当前 Activity

android - 无法设置开机重复闹钟

android - 如何在textview android上显示来自firebase云消息的推送通知消息

wcf - 将 HTTP-Push 技术 "Reliable Secure Profile"迁移到 Azure

java - SQL 服务器异常 : "The column name xxx is not valid" when using JDBC

google-chrome - 我们可以在不使用 GCM/FCM 的情况下在 chrome 中进行网络推送通知吗?

java - 亚洲语言中带年份的日期格式