Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击操作时取消当前通知_Java_Android_Push Notification - Fatal编程技术网

Java 单击操作时取消当前通知

Java 单击操作时取消当前通知,java,android,push-notification,Java,Android,Push Notification,我有一个带有操作按钮的自定义通知: public class NotificationReceiver extends com.parse.ParsePushBroadcastReceiver { @Override public void onPushReceive(Context context, Intent intent) { ... NotificationActivity notification = new NotificationActivit

我有一个带有操作按钮的自定义通知:

public class NotificationReceiver extends com.parse.ParsePushBroadcastReceiver {
    @Override
    public void onPushReceive(Context context, Intent intent) {

    ...

    NotificationActivity notification = new NotificationActivity();
    notification.createNotification(context, message, notificationId);

    Log.i("tag", "Notification Received!");
}

我想取消我单击操作按钮“关闭”的通知

我知道我需要通知的id来取消它,但按照我编写代码的方式,当我单击“关闭”按钮并创建扩展BroadCastReceiver的类CancelNotification时,我会得到上一个通知的通知id,等等,正在关闭最后一个通知,即使我单击了创建的第一个通知


我可能做错了什么?

最好使用通知生成器。 举个例子:

    NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(this);
    mBuilder.setContentTitle("Your title");
    mBuilder.setOnlyAlertOnce(true);
    mBuilder.setAutoCancel(true);
    mBuilder.setContentText("main content")
    mBuilder.setSubText("subtext")
接下来,您将创建一个意图,您希望在单击通知时打开该意图

    intent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
然后创建通知管理器

    notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = mBuilder.build();
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(notificationID, notification);
notificationID可以是任何整数值。使用这种类型可以让您始终遵循android规范进行通知。

我发现了它

您PendingEvent始终发送请求代码==0

因为您有多个通知,所以每个通知都应该使用不同的requestCode

因此,尝试改变:

发件人:

致:


我在这里测试了您的代码,在我所做的更改之后,它仍在工作。

在您的意图被激发后,您需要运行以下代码来删除通知

NotificationManagerCompat.from(this).cancel(null, notificationId);

您是否尝试添加通知。标志\u自动\u取消?此标志是否具有您期望的行为?我使用notification.builder中的所有内容更新了问题。现在,当我点击“关闭”按钮时,它什么也不做。当你点击这些按钮时,意图会被广播到系统中。也许,你需要处理好意图。。试试这个asnwer:@guilhermp我编辑了我的问题,如果可以的话,请再看一看。我也知道哈哈,在我的测试过程中,我不小心插入了ID而不是“0”,并且它工作正常。在检查原因时,我注意到了这一差异,瞧!!最好的礼物但是。。。如果使用“id=intent.getIntExtra(“notification_id”,1);”检索通知id,为什么您建议的更改(这是一个参数更改)会使其生效?此问题中的问题在创建意图期间发生。始终使用ID=0创建意图。因此,getIntExtra(“通知_id”,1)总是返回0,然后,只有收到的最后一个通知被取消(甚至单击第一个通知)…很酷,但是如何在BroadcastReceiver中检索通知id以确保取消了正确的通知?
PendingIntent pendingCancelIntent = 
         PendingIntent.getBroadcast(context, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT) ;
PendingIntent pendingCancelIntent = 
         PendingIntent.getBroadcast(context, this.notificationId, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT) ;
NotificationManagerCompat.from(this).cancel(null, notificationId);