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);
}
}
}
Jack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.