0
votes

I'm developing an app which I'm using Firebase Cloud Messaging. But I have a situation where the app is closed, killed state and the user does not click the notification the bar after the 10 seconds how to close this notification automatically. And My question is: how to close this notification automatically when the app is in killed state and notification is in the system tray.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String ChannelID = "Qwykr";
    public static final int NOTIFICATION_ID = 1041;
    NotificationManager notificationManager;
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        String RiderLOcation = remoteMessage.getData().get("RiderLocation");
        String RiderID = remoteMessage.getData().get("RiderID");
        String Dlat = remoteMessage.getData().get("Dlat");
        String DLong = remoteMessage.getData().get("DLong");
        String Plat = remoteMessage.getData().get("Plat");
        String Plong = remoteMessage.getData().get("Plong");
        String RiderName = remoteMessage.getData().get("RiderName");
        String MainBody="RiderID:"+RiderID+";RiderName:"+RiderName+";RiderLocation:"
                +RiderLOcation+";PLat:"+Plat+";PLong:"+Plong+";DLat:"+Dlat+";DLong:"+DLong;
        System.out.println("notificationIntentIs  On Recevied"+MainBody);
        sendNotification(MainBody);
        PreferenceHandler.writeString(this, PreferenceHandler.RIDE_DETAIL,MainBody);

    }

    private void sendNotification(String messageBody) {

        Intent intent = new Intent(this, MainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("bookingId", messageBody);
        intent.putExtras(bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
        startActivity(intent);
        Intent YesButton = new Intent(this, MainActivity.class);
        Bundle bundle1 = new Bundle();
        bundle1.putString("bookingId", messageBody);
        YesButton.putExtras(bundle1);
        YesButton.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK|
                Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent YesPendingIntent = PendingIntent.getActivity(this, 0,
                YesButton,
                PendingIntent.FLAG_ONE_SHOT);
        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.logo)
                        .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
                        .setLights(Color.BLUE, 100, 3000)
                        .setPriority(Notification.DEFAULT_ALL)
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setWhen(System.currentTimeMillis())
                        .setContentTitle(getResources().getString(R.string.app_name))
                        .setContentText(getString(R.string.notificationData))
                        .setSound(defaultSoundUri)
                        .setAutoCancel(true)
                        .setContentIntent(pendingIntent);

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


        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(NOTIFICATION_ID /* ID of notification */,
                notificationBuilder.build());
        clearNotification(NOTIFICATION_ID);

    }

    public  void clearNotification(int ID) {
       Handler handler=new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message message) {
            }
        };
        Runnable doDisplayError = new Runnable() {
            public void run() {
                notificationManager.cancel(ID);
            }
        };




handler.postDelayed(doDisplayError,3000);

}
}
1
your fcm message type is user or data notification?Nouman Ch
@Nouman please check an update related to question.Mohit Lakhanpal

1 Answers

0
votes

A workaround that you can do to achieve this functionality:

Use Alarm Manager and set its time according to your requirement and attach Broadcast receiver with it. When the time reaches your app receives the callback[onReceive(Context context, Intent intent) ] from where you can cancel the notification using Notification ID.

Use NotificationManager to get notification from NotificationId.

For Notification Id you can store it preferences or in Db it's upto your requirements how much id's do you have.

Hope this will help you.