private void addNotification1(String ss) { String ssBigText = ss; Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0); NotificationManager nm = (NotificationManager)getApplicationContext() .getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(getApplicationContext()) .setTicker("") .setContentTitle("") .setContentText(ss) .setSmallIcon(R.drawable.a) .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.logoapp)) .setOngoing(true) .addAction(0, "", intent).setAutoCancel(true); Notification notification = builder.getNotification(); long[] vibrate = new long[] { 0, 0, 200, 30, 10 }; notification.vibrate = vibrate; notification.priority =Notification.PRIORITY_MAX; notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; nm.notify(NOTIFY_ID, notification); } 

I put the flag ongoing and the auto cancel flag but when I click it all the same does not close, what am I doing wrong?

    1 answer 1

      private void addNotification1(String ss) { String ssBigText = ss; Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0); Notification.Builder builder = new Notification.Builder(getApplicationContext()) .setTicker("") .setContentTitle("") .setContentText(ss) .setSmallIcon(R.mipmap.ic_launcher_round) .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher_round)) .setOngoing(true) // .addAction(0, "", intent) // вот это заменил .setContentIntent(intent) // на это .setAutoCancel(true); Notification notification = builder.getNotification(); long[] vibrate = new long[] { 0, 0, 200, 30, 10 }; notification.vibrate = vibrate; notification.priority =Notification.PRIORITY_MAX; notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; //спустил NotificationManager отдельно вниз NotificationManager nm = (NotificationManager)getApplicationContext() .getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(1, notification); } 

    Checked your code with your changes, it works now.

    Look at my 3 comments:

     // .addAction(0, "", intent) // вот это заменил .setContentIntent(intent) // на это //спустил NotificationManager отдельно вниз NotificationManager nm = (NotificationManager)getApplicationContext() .getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(1, notification); 

    Most likely you have .addAction(0, "", intent) not configured. But if it doesn't matter, use what I have written.