Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 876
puzzled Avatar asked Aug 17 '20 04:08

puzzled


People also ask

How do I get rid of notifications on flutter?

Using the plugin The clearAllAppNotifications method can be invoked to clear all notifications received by your Flutter app. The clearAppNotificationsByTag method can then be used to clear all notifications with that notificationTag.

How do I change the push notification icon on flutter?

You can do this easily using Roman's Notification Icon Generator - Click on "Notification Icon Generator" On the left panel, click "Image" to upload your own image or use ClipArt or text as provided.

How do I show local notifications on flutter?

After your creating your flutter project it is necessary to add the “flutter_local_notifications” package into your pubspec. yaml file under dependencies. It will help you to effectively deal with the Push Notification tasks. Then import the package into the necessary place of coding.


1 Answers

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)
  }
}
like image 164
Gal Rom Avatar answered Sep 29 '22 18:09

Gal Rom