Я написал некоторый код для реализации пользовательских уведомлений в моем приложении, но, похоже, он не работает, когда приложение находится в фоновом режиме. Вот код ниже:
let content = UNMutableNotificationContent()
content.title = "test notifaction"
content.body = "test notification after 5 second"
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: true)
let request = UNNotificationRequest(identifier: "testidentifire", content: content, trigger: trigger)
В приложении делегат
//user notification method
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.sound])
}
//response to user notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.notification.request.identifier == "testidentifire"
{
print("test")
}
completionHandler()
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge]) { (granted, error) in
print("granted\(granted)")
}
return true
}
теперь, когда я ищу, я находил один и тот же код везде, где не могу понять, что не так с моим кодом