импорт Awesome_notifications
импорт Shared_preferences_ios
импорт UserNotifications
@UIApplicationMain
class AppDelegate: FlutterAppDelegate {
var alertController: UIAlertController?
Код: Выделить всё
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GeneratedPluginRegistrant.register(with: self)
SwiftAwesomeNotificationsPlugin.setPluginRegistrantCallback { registry in
SwiftAwesomeNotificationsPlugin.register(
with: registry.registrar(forPlugin: "io.flutter.plugins.awesomenotifications.AwesomeNotificationsPlugin")!)
FLTSharedPreferencesPlugin.register(
with: registry.registrar(forPlugin: "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin")!)
}
requestNotificationAuthorization(application)
NotificationCenter.default.addObserver(self, selector: #selector(appDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
createRepeatableNotificationWithCustomSound()
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
@objc func appDidBecomeActive() {
print("App became active")
}
func requestNotificationAuthorization(_ application: UIApplication) {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in
DispatchQueue.main.async {
if let error = error {
print("Notification authorization error: \(error)")
return
}
if granted {
print("Notification authorization granted")
application.registerForRemoteNotifications()
} else {
print("Notification authorization denied")
}
}
}
}
func createRepeatableNotificationWithCustomSound() {
print("createRepeatableNotificationWithCustomSound called")
let content = UNMutableNotificationContent()
content.title = "Custom Sound Notification"
content.body = "This is a notification with a custom sound."
if let soundURL = Bundle.main.url(forResource: "new", withExtension: "aiff") {
print("Sound file found at path: \(soundURL.path)")
content.sound = UNNotificationSound(named: UNNotificationSoundName("new.aiff"))
} else {
print("Sound file not found: new.aiff")
}
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let request = UNNotificationRequest(
identifier: "RepeatableCustomSoundNotification",
content: content,
trigger: trigger
)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print("Failed to add notification: \(error)")
} else {
print("Repeatable notification scheduled with custom sound.")
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... he-problem
Мобильная версия