android - 在自定义通知点击时提示解锁锁屏

标签 android notifications lockscreen

我在锁定屏幕上显示了一条自定义通知。单击通知后,我正在使用广播待定 Intent 向我的应用程序发送消息。稍后在广播接收器中启动一个 Activity 。

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

我的要求是通知的点击事件一发送广播就要求用户解锁屏幕。我该怎么做?

我可以找到 this看起来像我的问题的问题。但遗憾的是没有答案。

这里有一些代码解释了我所做的通知创建。

/**
 * 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;
}

最佳答案

在未决 Intent 中使用 Activity Intent 而不是服务或广播

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 复制相同的内容。

关于android - 在自定义通知点击时提示解锁锁屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40787196/

相关文章:

android - 如何使用 Android 连接到无线接入点?

java - 加密和解密应用程序 key

ios - 在 iOS 5 上锁定屏幕时不会发生 NSTimer 事件

应用程序中的 android:screenOrientation 标签不起作用

android - 智能 watch 示例安装

objective-c - UIKeyboardWillShowNotification 不调用,只有 UIKeyboardWillHideNotification 在 iOS 9 中调用

ios 火灾前本地通知检查

android - Oreo 模拟器上未显示通知

支持指纹识别的 Android 锁屏

android - 如何在猴子测试期间避免锁屏事件