gpt4 book ai didi

android - 通知未被取消 (Android)

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:11:30 26 4
gpt4 key购买 nike

如果单击操作,通知 setAutoCancel(true) 不起作用

我有一个包含操作的通知。当我点击通知时,它会从列表中删除。但是,当我单击操作时,它成功完成了操作(即调用电话),但是当我返回通知列表时,它仍然存在。AlarmReceiver的相关代码:

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

enter image description here

最佳答案

使用这个标志:

Notification.FLAG_AUTO_CANCEL在里面:

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

Documentation

关于android - 通知未被取消 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37595594/

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