gpt4 book ai didi

java - 关闭当前点击操作的通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:04:58 24 4
gpt4 key购买 nike

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

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

public class NotificationActivity {

public int notificationId;

public void createNotification(Context context, String message, String studentId, String notificationId){
this.notificationId = Integer.parseInt(notificationId);

// manager
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

// notification
Notification.Builder mBuilder = new Notification.Builder(context);
mBuilder.setContentTitle("My Title");
mBuilder.setContentText(message);
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
mBuilder.setAutoCancel(true);
mBuilder.setStyle(new Notification.BigTextStyle()
.bigText(message));

// cancel intent
Intent cancelIntent = new Intent(context, CancelNotification.class);
Bundle extras = new Bundle();
extras.putInt("notification_id", this.notificationId);
cancelIntent.putExtras(extras);
PendingIntent pendingCancelIntent =
PendingIntent.getBroadcast(context, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT) ;
mBuilder.addAction(R.drawable.notification_close, "Fechar", pendingCancelIntent);

// notify
Notification notification = mBuilder.build();
notificationManager.notify(Integer.parseInt(notificationId), notification);
}

public static class CancelNotification extends BroadcastReceiver {

private int id;

@Override
public void onReceive(Context context, Intent intent) {
id = intent.getIntExtra("notification_id", 1);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(id);
}
}
}

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

我知道我需要通知的 ID 才能取消它,但是我执行代码的方式是,当我单击“关闭”按钮并创建扩展 BroadCastReceiver 的类 CancelNotification 时,我得到了通知 ID最后一个通知,因此,即使我点击了我创建的第一个通知,也会关闭最后一个通知。

我可能做错了什么?

最佳答案

找到了

你pendingIntent总是发送请求码== 0;

由于您有多个通知,每个通知都应该使用不同的请求代码。

所以,尝试改变:

来自:

PendingIntent pendingCancelIntent = 
PendingIntent.getBroadcast(context, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT) ;

收件人:

PendingIntent pendingCancelIntent = 
PendingIntent.getBroadcast(context, this.notificationId, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT) ;

我在这里测试了您的代码,在我进行更改后它可以正常工作。

关于java - 关闭当前点击操作的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35236494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com