puzzled
puzzled

Reputation: 631

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 ?

Upvotes: 17

Views: 13685

Answers (5)

educoutinho
educoutinho

Reputation: 929

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();

Upvotes: 1

SupposedlySam
SupposedlySam

Reputation: 685

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()
    }

}

Upvotes: 2

MarcS82
MarcS82

Reputation: 2517

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();
    }
  }
}

Upvotes: 5

Gal Rom
Gal Rom

Reputation: 6461

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)
  }
}

Upvotes: 18

Shri Hari L
Shri Hari L

Reputation: 4894

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.

Upvotes: 6

Related Questions