通知未被驳回(Android)

通知未被驳回(Android),android,notifications,dismiss,Android,Notifications,Dismiss,如果单击操作,则通知设置自动取消(true)不起作用 我有一个通知,里面有一个动作。当我点击通知时,它将从列表中删除。但是,当我单击该操作时,它会成功地完成该操作(即发出一个调用),但当我返回到通知列表时,它仍在那里。 报警接收器的相关代码: public class AlarmReceiver extends BroadcastReceiver { Meeting meeting; /** * Handle received notifications about meetings tha

如果单击操作,则通知设置自动取消(true)不起作用

我有一个通知,里面有一个动作。当我点击通知时,它将从列表中删除。但是,当我单击该操作时,它会成功地完成该操作(即发出一个调用),但当我返回到通知列表时,它仍在那里。 报警接收器的相关代码:

public class AlarmReceiver extends BroadcastReceiver {
Meeting meeting;

/**
 * Handle received notifications about meetings that are going to start
 */
@Override
public void onReceive(Context context, Intent intent) {

    // Get extras from the notification intent
    Bundle extras = intent.getExtras();
    this.meeting = extras.getParcelable("MeetingParcel");

    // build notification pending intent to go to the details page when click on the body of the notification
    Intent notificationIntent = new Intent(context, MeetingDetails.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    notificationIntent.putExtra("MeetingParcel", meeting);      // send meeting that we received to the MeetingDetails class
    notificationIntent.putExtra("notificationIntent", true);    // flag to know where the details screen is opening from

    PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    // build intents for the call now button
    Intent phoneCall = Call._callIntent(meeting);
    if (phoneCall != null) {

        PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);

        int flags = Notification.FLAG_AUTO_CANCEL;

        // build notification object
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        Notification notification = builder.setContentTitle("Call In")
                .setContentText(intent.getStringExtra("contextText"))
                .setTicker("Call In Notification")
                .setColor(ContextCompat.getColor(context, R.color.colorBluePrimary))
                .setAutoCancel(true)                    // will remove notification from the status bar once is clicked
                .setDefaults(Notification.DEFAULT_ALL)  // Default vibration, default sound, default LED: requires VIBRATE permission
                .setSmallIcon(R.drawable.icon_notifications)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(meeting.description))
                .addAction(R.drawable.icon_device, "Call Now", phoneCallIntent)
                .setCategory(Notification.CATEGORY_EVENT)   // handle notification as a calendar event
                .setPriority(Notification.PRIORITY_HIGH)    // this will show the notification floating. Priority is high because it is a time sensitive notification
                .setContentIntent(pIntent).build();

        notification.flags = flags;

        // tell the notification manager to notify the user with our custom notification
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notification);
    }
  }
}

您已经在both和change标志中创建了两个挂起的意图使用

  PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0);

  PendingIntent phoneCallIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), phoneCall, PendingIntent.FLAG_UPDATE_CURRENT);
//换成这一行

   PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

   PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);
使用此标志:

Notification.FLAG\u AUTO\u CANCEL
其中:

int flags = Notification.FLAG_AUTO_CANCEL;
Notification notification = builder.build();
        notification.flags = flags;

好的,这是一个已知的问题,需要额外的代码(通过id保持对通知的引用)。我不知道API为什么不提供这个,因为这样做似乎很合乎逻辑。但无论如何

查看此

在notification manager上调用notify时,您为其提供了一个id,这是您以后可以用来访问它的唯一id(这是从notification manager:

notify(int id, Notification notification)
要取消,请致电:

cancel(int id)

使用相同的id。因此,基本上,您需要跟踪该id,或者可能将该id放入一个捆绑包中,您将其添加到PendingEvent中的意图中?

我今天遇到了这个问题,并发现FLAG_AUTO_CANCEL和setAutoCanel(true)在单击通知时都可以工作, 但不适用于行动

简单地说,在目标服务或操作活动中,取消通知

NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancelAll();
或者如果有更多的通知

manager.cancel(notificationId);

很抱歉,没有帮助仍然没有取消通知:(我按照您的建议更新了代码,但仍然没有成功..请查看问题中我编辑的代码(正确吗?)@异步-,是的。必须尝试使用一个非常简单的通知来测试吗?好的,事实证明这是一个已知的问题,我将发布链接到其他关于stackoverflow的问题this@Async-,您是否尝试重新启动设备?异步-您是否找到了解决此问题的方法。目前,我面临着相同的问题?