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 ?
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.
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.
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.
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With