【问题标题】:Starting activity after receive fcm notification on lock screen in Android Q在 Android Q 的锁定屏幕上收到 fcm 通知后开始活动
【发布时间】:2019-11-22 06:29:03
【问题描述】:

对于我的应用程序,我需要实现类似 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);
        }
    }

此代码在我收到数据推送通知后执行。

【问题讨论】:

  • 尝试使用我的旧答案:stackoverflow.com/questions/58723440/…
  • 不幸的是,它对我不起作用
  • 嗨@Denis,我发现Skype,whatapp,messenger做得很完美,但我不知道怎么做,和你的问题一样,你解决了吗?谢谢

标签: java android push-notification


【解决方案1】:

使用 API 级别 21 Android 5.0 中添加的 NotificationBuilder 中的 setVisiblity()):

notificationBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);

public NotificationCompat.Builder setVisibility (int 可见性) 设置 Notification.visibility。

参数
visibility intNotification.VISIBILITY_PRIVATE(默认)、Notification.VISIBILITY_PUBLICNotification之一.VISIBILITY_SECRET.

【讨论】:

    【解决方案2】:

    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 在 Android Q 中已弃用。您可以在 Activity 的 onCreate 中使用 Activity.setShowWhenLocked(true) 和 Activity.setTurnScreenOn(true) 在锁定屏幕中显示活动。

    【讨论】:

    • 如果在我锁定屏幕之前屏幕上有活动,则此标志有效。但是,如果活动不可见并且我锁定屏幕,然后发送推送通知,那么什么也不会发生。 startActivity 不起作用
    猜你喜欢
    • 2021-07-21
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多