16

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 ?

puzzled
  • 509
  • 1
  • 5
  • 18

5 Answers5

17

Shri Hari solution did the trick. Kotlin:

import android.app.NotificationManager
import android.content.Context
import io.flutter.embedding.android.FlutterActivity


class MainActivity: FlutterActivity() {

    override fun onResume() {
        super.onResume()
        closeAllNotifications();
    }

    private fun closeAllNotifications() {
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.cancelAll()
    }

}

And For IOS I use UNUserNotificationCenter:

import UIKit
import Flutter
import UserNotifications

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {

    GeneratedPluginRegistrant.register(with: self)
    if #available(iOS 10.0, *) {
        application.applicationIconBadgeNumber = 0 // For Clear Badge Counts
        let center = UNUserNotificationCenter.current()
        center.removeAllDeliveredNotifications() // To remove all delivered notifications
        center.removeAllPendingNotificationRequests()
    }
     
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}
Gal Rom
  • 6,221
  • 3
  • 41
  • 33
6

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.

Shri Hari L
  • 4,551
  • 2
  • 6
  • 18
  • Hi, I would like to ask, how to do it for iOS ? when notification shows up on status bar, I would like to remove the badge "after" user clear notification from status bar – wahyu Jul 25 '22 at 01:38
3

In case you don't want to modify your swift or java code you can use flutter_local_notifications which offers a method cancelAll().

class SomeWidget extends State<SomeState> with WidgetsBindingObserver {

 @override
  void didChangeAppLifecycleState(AppLifecycleState state) async {
    if (state == AppLifecycleState.resumed) {
      await flutterLocalNotificationsPlugin
          .cancelAll();
    }
  }
}
MarcS82
  • 2,065
  • 7
  • 29
  • 46
1

Clear/Cancel Notifications on App Load and on Resume

iOS AppDelegate

Replace your ios/Runner/AppDelegate.swift with the following.

Just make sure any code you need for your app is also there (such as the import for Firebase and FirebaseApp.configure()).

import UIKit
import Flutter
import UserNotifications

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    if #available(iOS 10.0, *) {
      clearAllNotifications(application)
    }
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

  override func applicationDidBecomeActive(_ application: UIApplication) {
    super.applicationDidBecomeActive(application)
    clearAllNotifications(application)
  }

  func clearAllNotifications(_ application: UIApplication) {
    application.applicationIconBadgeNumber = 0 // Clear Badge Counts
    let center: UNUserNotificationCenter = UNUserNotificationCenter.current()
    center.removeAllDeliveredNotifications()
    center.removeAllPendingNotificationRequests()
  }
}

Android MainActivity

Note: remove all < and > in examples and use your values instead.

Replace your android/app/src/main/kotlin/com/<your_domain>/<your_app_name>/MainActivity.kt with the following.

package com.<your_domain>.<your_app_name>

import android.app.NotificationManager
import android.content.Context
import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
    override fun onResume() {
        super.onResume()
        closeAllNotifications();
    }

    private fun closeAllNotifications() {
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.cancelAll()
    }

}
SupposedlySam
  • 675
  • 5
  • 14
1

Try this package: clear_all_notifications

Add package:

 $ flutter pub add clear_all_notifications

Use:

import 'package:clear_all_notifications/clear_all_notifications.dart';

await ClearAllNotifications.clear();
educoutinho
  • 869
  • 9
  • 16