Android - 在锁定屏幕上执行推送通知操作时提示用户输入设备密码

标签 android push-notification google-cloud-messaging

我的通知有操作按钮。当通知到达锁定屏幕并且用户点击操作按钮时,我需要显示设备 pin 屏幕,输入 pin 后,操作(在我的例子中,操作是对服务器的 API 调用)应该是执行而不会引发通知 Activity 。现在,在锁定屏幕上,直接执行操作而不提示用户输入设备密码。我想解决这个问题。

当设备解锁时通知到达时,用户应该能够直接点击操作按钮而不会看到通知 Activity 。

我对 stackoverflow 的研究让我遇到了许多相反的问题 - 许多人询问如何在没有设备 pin 的情况下在锁定屏幕上执行操作。然而,就我而言,我从未收到设备 pin 提示。当用户在锁定屏幕上执行通知操作时,代码中的什么设置会显示设备 PIN?

我下面的代码会导致在锁定屏幕上执行通知操作而不提示输入 pin:

private void displayChallengeNotification(Context context, ChallengeInformation extras) {
    /* build the notification */
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setVisibility(NotificationCompat.VISIBILITY_SECRET)
                    .setSmallIcon(R.drawable.status_bar_icon)
                    .setContentTitle(context.getString(R.string.push_notification_title))
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(getChallengeContextString(extras)))
                    .setContentText(context.getString(R.string.push_notification_description))
                    .setAutoCancel(false) 
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setColor(context.getResources().getColor(R.color.notification))
                    .setLocalOnly(true) 
                    .setDefaults(DEFAULTS);

    /* set the target of the notification */
    PendingIntent challenge =
            getChallengePendingIntent(context, extras);
    mBuilder.setContentIntent(challenge);

    addNotificationActions(mBuilder, context, extras);

    challengeTracker.notifyChallenge(extras, context, mBuilder.build());
}

private PendingIntent getChallengePendingIntent(Context context, ChallengeInformation extras) {

    Intent challenge = getChallengeIntent(context, extras);

    /* set up the back stack so that navigation works as expected */
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addNextIntent(challenge);

    int notificationId = extras.getTransactionId().hashCode();
    PendingIntent challengePendingIntent = stackBuilder.getPendingIntent(notificationId, 0);
    return challengePendingIntent;
}

private static Intent getChallengeIntent(Context context, ChallengeInformation info) {
    /* set up the intent to launch the challenge screen */
    Intent challenge = new Intent(context, PushChallengeActivity.class);
    challenge.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    /* get the information for the challenge */
    challenge.putExtras(info.getBundle());
    if (info.isChallengeAccepted() != null) {
        challenge.putExtra(Constants.IS_CHALLENGE_ACCEPTED, info.isChallengeAccepted());
    }

    return challenge;
}

最佳答案

My code below would cause the notification actions to be performed on lock screen without prompting for pin

它的行为符合预期。通知操作在用户点击后立即执行。

目前没有直接的方法可以将此操作推迟到用户解锁手机后执行。

最好的解决方法是启动通知 Activity 和 Activity 的 onResume(),执行必要的操作。

您还可以查看通知 lock screen visibility options以避免在锁定屏幕上显示您的通知。但是,请注意以下几点:

However, the user always has final control over whether their notifications are visible on the lock screen and can even control that based on your app's notification channels.

关于Android - 在锁定屏幕上执行推送通知操作时提示用户输入设备密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50183937/

相关文章:

android - 从 FirebaseDatabse 收集所有数据并调用一次 notifyDataSetChanged()

Android - 假设 MotionEvent.ACTION_UP 将 100% 意味着用户已触摸 View 是否安全?

ios - iOS 中 didReceiveRemoteNotification 的两个委托(delegate)方法有什么区别?

android - Workmanager 在待机和休眠模式下不工作

android - 谷歌云消息,无法初始化接收器

ios - 在遗留代码中使用 Firebase 数据库与 Google Cloud Messaging

android - 无法从 onActivityReenter 中的 Intent 读取 Parcelable

java - 如何在登录 Activity 模板android studio中设置有效的用户名和密码

ios - 适用于 iOS 的 GCM 获取 com.google.gcm 错误 501

android - 为什么首选使用 GCM 进行推送通知?