Android 锁定屏幕上的单个 click/tap 通知无效

Android Notification not working for single click/tap on lock screen

我将我的逻辑放在 android 后台服务中,它将在我的粘性 notification.Everything 的 onClick 操作上启动,工作正常但问题是:-

  1. 当我锁定 phone 并尝试 click/tap 通知时,它总是需要双 click/tap。
  2. 我的逻辑在后台服务中,但在点击通知后后台服务不会启动,直到我的手机解锁。(后台服务是粘性的)

以下代码用于生成粘性通知。

private void Notify() {
    Context objContext = this.cordova.getActivity();
    Intent objIntent = new Intent(objContext, ApiCallServeice.class);
    PendingIntent pi = PendingIntent.getService(objContext, intNotificationId, objIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    Notification.Builder builder = new Notification.Builder(objContext);
    builder.setContentTitle("Click to get help.");
    builder.setAutoCancel(false);
    builder.setSmallIcon(objContext.getApplicationInfo().icon);
    builder.setOngoing(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setVisibility(Notification.VISIBILITY_PUBLIC);
    }
    builder.setContentIntent(pi);
    builder.build();

    myNotication = builder.getNotification();
    manager.notify(intNotificationId, myNotication);
}

请给我建议解决方案或需要在我的代码中设置任何标志。

为了获得点击通知 UI everwhere.We 需要使用远程视图,您可以在其中将按钮覆盖在整个布局上并在该按钮上编写点击监听器

下面是我使用的更新码:-

   private void Notify() {
    Context objContext=this.cordova.getActivity();
    Intent objIntent = new Intent(objContext, ApiCallServeice.class);
    PendingIntent pi = PendingIntent.getService(objContext, intNotificationId, objIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    RemoteViews objRemoteViews = new RemoteViews(objContext.getApplicationContext().getPackageName(), R.layout.your_notification_layout);
    objRemoteViews.setOnClickPendingIntent(R.id.your_notification_clickable_button, pi);

    Notification.Builder builder = new Notification.Builder(objContext);
    builder.setAutoCancel(false);
    builder.setSmallIcon(objContext.getApplicationInfo().icon);
    objRemoteViews.setImageViewResource(R.id.img_icon, objContext.getApplicationInfo().icon);
    builder.setOngoing(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setVisibility(Notification.VISIBILITY_PUBLIC);
    }
    builder.setContent(objRemoteViews);
    builder.build();

    myNotication = builder.getNotification();
    manager.notify(intNotificationId, myNotication);
}