繁体   English   中英

如何知道应用程序何时被杀死并清除 flutter 中所有未决通知?

[英]How to know when app got killed and clear all the pending notifications in flutter?

我的应用程序在后台播放预定通知(我正在使用 flutter 本地通知),但是当手机关闭并稍后再次打开时,所有这些预定通知开始相互重叠播放(这让用户很烦,因为它播放自定义声音)所以如果用户滑动并终止应用程序或关闭并再次打开,我不希望显示任何通知。 所以我想在应用程序被杀死时清除所有待处理的通知。

如果有任何可用的解决方案,请告诉我。

也许您可以清除主 function 中 runApp function 之前安排的所有通知。随着

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM