cancelAll() and cancel() don’t dismiss my notification Android

My notification just stays there and never disappears when I call cancelAll() / cancel() from the notification manager, also I have autoCancel set to true, can you help me ? thanks

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";
String idCourse;
private String origine;
private String destination;

private double originLatitude;
private double originLongitude;

private double destLatitude;
private double destLongitude;
private String prix;
private String type;
private String distance;
private String numTelephoneClient;
private String motif_annulation;
private String numTelephoneConducteur;
private String dateCourse;
public static final String CHANNEL_ID = "NOTIFICATION_CHANNEL";

NotificationChannel channel;


@Override
public void onCreate() {
    super.onCreate();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // channel for notifications

        NotificationManager notificationManager = getSystemService(NotificationManager.class);

        String name = CHANNEL_ID;
        int importance = NotificationManager.IMPORTANCE_HIGH;

        channel = new NotificationChannel(CHANNEL_ID, name, importance);

        assert notificationManager != null;
        notificationManager.createNotificationChannel(channel);
    }

}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
   
    
    System.out.println("From: " + remoteMessage.getFrom());

    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        System.out.println("From:2 " + remoteMessage.getFrom());
        idCourse = remoteMessage.getData().get("idCourse");
        origine = remoteMessage.getData().get("origine");
        destination = remoteMessage.getData().get("destination");
        prix = remoteMessage.getData().get("prix");
        originLatitude = Double.parseDouble(remoteMessage.getData().get("OriginLatitude"));
        originLongitude = Double.parseDouble(remoteMessage.getData().get("OriginLongitude"));
        destLatitude = Double.parseDouble(remoteMessage.getData().get("destLatitude"));
        destLongitude = Double.parseDouble(remoteMessage.getData().get("destLongitude"));
        type = remoteMessage.getData().get("type");
        numTelephoneClient = remoteMessage.getData().get("numTelephoneClient");
        motif_annulation = remoteMessage.getData().get("Motif_annulation");
        numTelephoneConducteur = remoteMessage.getData().get("numTelephoneConducteur");
        dateCourse = remoteMessage.getData().get("dateCourse");
        distance = remoteMessage.getData().get("distance");
        System.out.println("remoteMessage.getData()type" +type);
        System.out.println("remoteMessage.getData().get("OriginLatitude")  "+remoteMessage.getData().get("OriginLatitude"));
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        System.out.println("Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

FCM
// message, here is where that should be initiated. See sendNotification method below.
System.out.println(“From:3 ” + remoteMessage.getFrom());
// sendNotification(remoteMessage.getFrom(), remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}

@Override
public void onNewToken(@NonNull String token) {
    Log.d(TAG, "Refreshed token: " + token);

    // If you want to send messages to this application instance or
    sendRegistrationToServer(token);
}

private void sendRegistrationToServer(String token) {
    // TODO: Implement this method to send token to your app server.
}



private void sendNotification(String messageBody) {
     int DELAY = 15000;
    System.out.println("From:4 ");
    PendingIntent pendingIntent = null;
        if (Objects.nonNull(type)&&type.equals("N")) {
            System.out.println("From:5 ");
            Intent intent = new Intent(this, AffectationCourseActivity.class);
            if(Objects.nonNull(idCourse)){
                intent.putExtra("id_course", Integer.parseInt(idCourse));
            }
            intent.putExtra("origine", origine);
            intent.putExtra("destination", destination);
            intent.putExtra("prix", prix);
            intent.putExtra("OriginLatitude", originLatitude);
            intent.putExtra("OriginLongitude", originLongitude);
            intent.putExtra("destLatitude", destLatitude);
            intent.putExtra("destLongitude", destLongitude);
            intent.putExtra("type", "N");
            intent.putExtra("numTelephoneClient", numTelephoneClient);
            intent.putExtra("distance",distance);
            System.out.println("distance  "+distance);

     
            pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);
        } else {
            Intent intent = new Intent(this, AffectationCourseActivity.class);
            System.out.println("From:6 ");
            if(Objects.nonNull(idCourse)){
                intent.putExtra("id_course", Integer.parseInt(idCourse));
            }
            intent.putExtra("origine", origine);
            intent.putExtra("destination", destination);
            intent.putExtra("OriginLatitude", originLatitude);
            intent.putExtra("OriginLongitude", originLongitude);
            intent.putExtra("type", "O");
            intent.putExtra("numTelephoneClient", numTelephoneClient);
      
            pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);
        }

    System.out.println("From:7 ");

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

  
    Uri sound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.sound);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL" )
            .setSmallIcon(R.drawable.saferide_logo)
            .setContentTitle( "Creation course" )
            .setSound(sound)
            .setAutoCancel(true)
            .setTimeoutAfter(10000)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setCategory(NotificationCompat.CATEGORY_CALL)
            .setContentIntent(pendingIntent)
            .setContentText( messageBody ) ;

    System.out.println("From:77 ");
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context. NOTIFICATION_SERVICE );
   if (android.os.Build.VERSION. SDK_INT >= android.os.Build.VERSION_CODES. O ) {
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes. CONTENT_TYPE_SONIFICATION )
                .setUsage(AudioAttributes. USAGE_ALARM )
                .build() ;
        System.out.println("From:778 ");
        channel.enableLights( true ) ;
        channel.setLightColor(Color. RED ) ;
        channel.enableVibration( true ) ;
        channel.setVibrationPattern( new long []{ 100 , 200 , 300 , 400 , 500 , 400 , 300 , 200 , 400 }) ;
        channel.setSound(sound , audioAttributes) ;
        mBuilder.setChannelId("NOTIFICATION_CHANNEL") ;
        assert mNotificationManager != null;
        mNotificationManager.createNotificationChannel(channel) ;

        Notification notification = mBuilder.build();
        notification.flags = Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL;
        System.out.println("From:7788 ");
        assert mNotificationManager != null;
        mNotificationManager.notify( 2 ,
                notification) ;

       new Handler(Looper.getMainLooper()).postDelayed (() -> {  
           notificationManager.cancelAll();
       
       }, 15000);

       new Handler().postDelayed(new Runnable() {
           @Override
           public void run() {
               notificationManager.cancelAll();
             
           }
       }, 15000);
    }
    else {

        Uri defaultSoundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.sound);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, CHANNEL_ID)
                        .setSmallIcon(R.drawable.saferide_logo)
                        .setContentTitle("Creation course")
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setChannelId(CHANNEL_ID)
                        .setSound(defaultSoundUri)
                        .setTimeoutAfter(10000)
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                        .setCategory(NotificationCompat.CATEGORY_CALL)
                        .setFullScreenIntent(pendingIntent,true)
                        .setContentIntent(pendingIntent);

        Notification notification = notificationBuilder.build();
        notification.flags = Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0 /* ID of notification */, notification);

       new Handler().postDelayed(new Runnable() {
           @Override
           public void run() {
               notificationManager.cancel(0);
          
               notificationManager.cancelAll();
           
           }
       }, 15000);
    }

}

}

New contributor

Jack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật