Android Notification无法单击/点击锁定屏幕

时间:2016-06-27 06:57:44

标签: android notifications lockscreen lockscreenwidget

我在Android后台服务中推出了我的逻辑,这将在我的粘贴通知的onClick操作开始。一切正常但问题是: -

  1. 当我锁定手机并尝试点击/点按通知时,需要双击/点击始终。
  2. 我的逻辑是后台服务但是在点击通知后,后台服务直到我的手机解锁才开始。(后台服务很粘)
  3. 以下代码用于生成粘性通知。

    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);
    }
    

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

1 个答案:

答案 0 :(得分:1)

用于点击通知用户界面。我们需要使用远程视图,您可以在其中将按钮覆盖放在整个布局上,并在该按钮上编写点击监听器

以下是我使用的更新代码: -

   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);
}