Search code examples
flutterfirebase-cloud-messagingandroid-notifications

In flutter how to clear notification in bar?


I am learning Google cloud messaging and firebase messaging is working fine, but when a user do not click at the notification to open the app and instead goes directly to app by manually opening it and bringing to foreground, the notification stays. I want those notification message related to my app go away when the app comes to foreground. How can I achieve this ?


Solution

  • It thinks there is no way to do this in documentation.

    But you can try this. You can just go to your MainActivity.java in your Android folder, and do something like this.

    import android.app.NotificationManager;
    import android.content.Context;
    

    Import these packages.

     @Override
        protected void onResume() {
            super.onResume();
    
            // Removing All Notifications
            closeAllNotifications();
        }
    
        private void closeAllNotifications() {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancelAll();
        }
    

    Add an onResume function below onCreate on MainActivity.java.
    Hope this solves your issue.