首页 > 解决方案 > 在颤振中如何清除栏中的通知?

问题描述

我正在学习 Google 云消息传递和Firebase 消息传递工作正常,但是当用户不单击通知打开应用程序而是通过手动打开应用程序并将其置于前台直接进入应用程序时,通知仍然存在。当应用程序进入前台时,我希望那些与我的应用程序相关的通知消息消失。我怎样才能做到这一点?

标签: flutterfirebase-cloud-messagingandroid-notifications

解决方案


Shri Hari 解决方案成功了。科特林:

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

}

对于 IOS,我使用 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)
  }
}

推荐阅读