如何在点击后关闭通知

时间:2018-04-20 16:46:31

标签: android notifications

单击完成后,通知不会消失(通知中的按钮)我正在使用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());


        }

片段:我的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);
    }

1 个答案:

答案 0 :(得分:0)

在片段类中尝试此代码:

            Bundle extras = getIntent().getExtras();
            if (extras != null)
            {
                NotificationManager manager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
                manager.cancel(NOTIFICATION_ID);
            }

在接收器类中:

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

如果您在单击使用相同的NOTIFICATION_ID取消后取消系统托盘中的通知警报。