我的應用程式在后臺播放預定通知(我正在使用顫振本地通知),但是當手機關閉并稍后再次打開時,所有的預定通知開始相互重疊播放(它讓用戶煩惱,因為它使用自定義聲音播放)所以如果用戶滑動并殺死應用程式或關閉并再次打開,我不希望顯示任何通知。所以我想在應用程式被殺死時清除所有待處理的通知。
請讓我知道是否有任何可用的解決方案。
uj5u.com熱心網友回復:
更換你的
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)
}
}
uj5u.com熱心網友回復:
也許您可以清除 main 函式中 runApp 函式之前安排的所有通知。和
await flutterLocalNotificationsPlugin.cancelAll();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/467979.html
