繁体   English   中英

在自定义通知单击时提示解锁锁定屏幕

[英]Prompt to unlock lock screen on custom notification click

我在锁定屏幕中显示了自定义通知。 单击通知后,我正在使用广播待定意图向我的应用发送消息。 后来在广播接收器中开始活动。

问题是,一旦我点击通知,它就会从锁定屏幕上消失,并且活动会在锁定屏幕后面启动。 它不会要求用户解锁屏幕。

我的要求是在通知的点击事件发送广播后立即要求用户解锁屏幕。 我怎么做?

我可以找到这个看起来像我的问题的问题。 但不幸的是没有答案。

以下是一些解释我所做通知创建的代码。

/**
 * Method to render Notification
 */
private void showNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    /* set mandatory stuff on builder */

    Notification notification = builder.build();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        notification.bigContentView = createCustomView(context);
    }

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(getNotificationId(), notification);
}

/**
 * Method to return notification click's PendingIntent
 */
private PendingIntent getNotificationClickIntent() {
    Intent bcIntent = new Intent(OPEN_APP);
    bcIntent.putExtra(EXTRA_DEEP_LINK_URL, getDeepLinkUrl());

    return PendingIntent.getBroadcast(
        context, getReqCode(), bcIntent, PendingIntent.FLAG_ONE_SHOT);
}

/**
 * Method to create custom view for notification
 */
private RemoteViews createCustomView(Context context) {
   RemoteViews customView = new RemoteViews(context.getPackageName(), R.layout.custom_layout);

   if (customView != null) {
        // set dat on views

        customView.setOnClickPendingIntent(R.id.my_view, getNotificationClickIntent());
   }

   return customView;
}

在待定意图中使用活动意图而不是服务或广播

PendingIntent.getActivity(context, 0, intent,
                                 PendingIntent.FLAG_UPDATE_CURRENT);

更新:

在我的情况下,我使用服务。

private fun activityPendingIntent(context: Context, action: String? = null): PendingIntent {
    Timber.d("activityPendingIntent")
    val intent = Intent(context, DummyBackgroundActivity::class.java)
    action?.let { intent.action = action }
    return PendingIntent.getActivity(context, ACTIVITY_NOTIFICATION_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT)
}

DummyBackgroundActivity

class DummyBackgroundActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val service = Intent(this, BackgroundService::class.java)
        service.action = intent.action
        startService(service)
    }

    override fun onResume() {
        super.onResume()
        finish()
    }
}

服务:

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    Timber.d("onStartCommand intent = %s", intent?.action)
    when (intent?.action) {
       //Handle it 
    }

    return Service.START_NOT_STICKY
}

我希望你使用Broadcast复制相同的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM