flutter - 在颤振中如何清除栏中的通知?
问题描述
我正在学习 Google 云消息传递和Firebase 消息传递工作正常,但是当用户不单击通知打开应用程序而是通过手动打开应用程序并将其置于前台直接进入应用程序时,通知仍然存在。当应用程序进入前台时,我希望那些与我的应用程序相关的通知消息消失。我怎样才能做到这一点?
解决方案
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)
}
}
推荐阅读
- c# - 很多 if 和 else 条件重复代码
- android - 意图过滤器 android.nfc.action.NDEF_DISCOVERED 不起作用
- java - Apache PDFBox - 无法读取 PDF 中的所有字段
- go - Golang 之旅:切片就像对数组的引用
- ubuntu - 如何解决 Jenkins 作业中的 PLINK“无法打开串口”问题
- dji-sdk - Onboard SDK 的俯仰角似乎随着速度而减小
- node.js - 如何根据通配符子域返回动态 TXT 记录?
- javascript - 将事件动态添加到 div
- java - 如何在 Java 的标准类中设置通用对象?
- html - Django 2.0:后退按钮根据您来自哪里重定向到不同页面