如何知道应用程序何时被杀死并清除所有待处理的通知?
How to know when app got killed and clear all the pending notifications in flutter?
我的应用程序在后台播放预定通知(我正在使用 flutter 本地通知),但是当 phone 关闭并稍后再次打开时,所有一堆预定通知开始相互重叠播放(这很烦人用户,因为它播放自定义声音)所以我不希望在用户滑动并终止应用程序或关闭并再次打开时显示任何通知。所以我想在应用程序被杀死时清除所有待处理的通知。
如果有任何可用的解决方案,请告诉我。
也许您可以清除在 main 函数中的 runApp 函数之前安排的所有通知。有
await flutterLocalNotificationsPlugin.cancelAll();
替换你的
android/app/src/main/kotlin/com/example/appname>/MainActivity.kt
与以下。
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)
}
}
我的应用程序在后台播放预定通知(我正在使用 flutter 本地通知),但是当 phone 关闭并稍后再次打开时,所有一堆预定通知开始相互重叠播放(这很烦人用户,因为它播放自定义声音)所以我不希望在用户滑动并终止应用程序或关闭并再次打开时显示任何通知。所以我想在应用程序被杀死时清除所有待处理的通知。
如果有任何可用的解决方案,请告诉我。
也许您可以清除在 main 函数中的 runApp 函数之前安排的所有通知。有
await flutterLocalNotificationsPlugin.cancelAll();
替换你的
android/app/src/main/kotlin/com/example/appname>/MainActivity.kt
与以下。
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)
}
}