簡體   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