通过点击通知在锁定屏幕上打开活动

问题描述 投票:0回答:1

问题:无法通过通知在锁定屏幕上打开活动。

想要存档:当用户点击通知时,即使应用程序被杀死/不在堆栈/后台,也必须在设备的锁定屏幕上打开一个活动

Flow:我已经实现了 VOIP SDK,它在 FCMNotification 类的

onMessageReceived
中提供来电信息,即使应用程序被杀死/在堆栈中/在后台。收到数据后,会显示带有“接受”和“拒绝”选项的通知。当用户点击“接受”时,它必须像 whatsapp、skype 应用程序那样在锁定屏幕上方打开一个活动。

我尝试过的:

  1. 在创建活动中应用以下代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            setShowWhenLocked(true)
            setTurnScreenOn(true)
        } else {
            window.addFlags(
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            )
        }

在 Android Manifest 中的 Activity 标签中

            android:showOnLockScreen="true"
            android:launchMode="singleTop"

还有活动也低于意图过滤器

<intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
  1. 实现广播接收器并在通知到达时启动活动
  2. 实施意图服务以在通知到达时启动活动
android kotlin lockscreen
1个回答
0
投票

要通过点击通知在锁定屏幕上打开活动,您可以使用一个名为 FLAG_SHOW_WHEN_LOCKED 的特殊标志结合 Intent 来启动活动。

以下是您可以遵循的步骤:

1.在您的通知代码中,创建一个 Intent 以在用户点击通知时启动您要打开的 Activity。例如:

Intent intent = new Intent(context, MyActivity.class);

2.将 FLAG_ACTIVITY_NEW_TASKFLAG_SHOW_WHEN_LOCKED 标记添加到意图中。这将允许活动作为新任务启动并出现在锁定屏幕上:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_SHOW_WHEN_LOCKED);

3.创建一个 PendingIntent 将在用户点击通知时启动活动:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

4.在您的通知生成器上设置pendingIntent

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setContentTitle("My notification title")
        .setContentText("My notification message")
        .setSmallIcon(R.drawable.notification_icon)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true);

5.最后调用NotificationManager.notify()显示通知:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, builder.build());

当用户点击通知时,如果设备已锁定,MyActivity 活动将启动并显示在锁定屏幕上。

© www.soinside.com 2019 - 2024. All rights reserved.