[英]How to close notification after clicking
The Notification does not disappear after clicking Done (button inside notification) I am using NotificationCompat.Builder and I call receiver from the fragment 单击“完成”(通知内的按钮)后,通知不会消失。我正在使用NotificationCompat.Builder,并从片段中调用接收方
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
UUID id = (UUID) intent.getSerializableExtra(TaskFragment.SERVICE_EXTRA);
mTask = TaskLab.get(context).getTask(id);
// Action click
Intent doneIntent = new Intent(context, TaskActivity.class);
doneIntent.setAction("ACTION_DONE");
PendingIntent doneAlarmPending = PendingIntent.getActivity(context, DayListFragment.RSQ, doneIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// Notification Builder
mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle(mDay.getTask());
mBuilder.setSmallIcon(R.mipmap.motiv);
mBuilder.setContentText("Do exercise");
mBuilder.addAction(0, "Done", doneAlarmPending);
mBuilder.setAutoCancel(true);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
nManager = NotificationManagerCompat.from(context);
nManager.notify(NOTIFICATION_ID, mBuilder.build());
}
Fragment: My TaskFragment 片段: 我的任务片段
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void setAlarm(Calendar targetCalendar){
Intent intent = new Intent(getContext(), AlarmReceiver.class);
Bundle bundle = new Bundle();
bundle.putSerializable(SERVICE_EXTRA, mDay.getId());
intent.putExtras(bundle);
PendingIntent pendingintent = PendingIntent.getBroadcast(getContext(), RSQ, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, targetCalendar.getTimeInMillis(), pendingintent);
}
try this code in your fragment class : 在您的片段类中尝试以下代码:
Bundle extras = getIntent().getExtras();
if (extras != null)
{
NotificationManager manager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
manager.cancel(NOTIFICATION_ID);
}
in receiver class: 在接收器类中:
Intent notificationIntent = new Intent(view, CallActivity.class);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(view)
.setSmallIcon(R.drawable.nexge_logo,3)
.setContentTitle(callerName)
.setOngoing(true)
.setLargeIcon(bitmap)
.setContentText(status)
;
PendingIntent contentIntent = PendingIntent.getActivity(view, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, builder.build());
if you cancel your notification alert in your system tray after click use same NOTIFICATION_ID to cancel. 如果您单击“取消”后取消了系统任务栏中的通知警报,请使用相同的NOTIFICATION_ID进行取消。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.