【问题标题】:Android - prompt users for device pin when performing push notification actions on lock screenAndroid - 在锁定屏幕上执行推送通知操作时提示用户输入设备密码
【发布时间】:2018-05-08 09:00:50
【问题描述】:

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

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

我对 stackoverflow 的研究让我遇到了许多相反的问题 - 许多人询问如何在没有设备 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;
}

【问题讨论】:

  • 在屏幕锁定时按下通知中的操作按钮会发生什么?
  • @JeffreyBlattman 直接执行操作,而不提示输入设备引脚。在我的例子中,服务器获得用户响应,通知从设备中消失,仅此而已。

标签: android push-notification google-cloud-messaging


【解决方案1】:

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

它的行为符合预期。通知操作一旦被用户点击就会立即执行。

目前没有直接的方法来推迟此操作,直到用户解锁手机。

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

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

但是,用户始终可以最终控制他们的通知是否在锁定屏幕上可见,甚至可以根据您应用的通知渠道进行控制。

【讨论】:

  • 如果我启动通知活动,那么即使设备解锁,也无法直接执行该操作,这会降低用户体验。有什么方法可以让应用在锁定屏幕上要求输入密码,但在解锁时直接执行操作而不启动活动?
  • If I start the notification activity, then even when the device is unlocked, the action can no longer be performed directly 您的通知活动将立即被带入前台,onResume() 将被调用。您可以自动执行该操作。您可以更多地启动一个透明活动(将背景颜色更改为透明)并在操作完成后关闭它。
猜你喜欢
  • 1970-01-01
  • 2014-08-23
  • 2016-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多