I'm working on a SwiftUI app.
The Local Notifications are working if the app was opened by Xcode, are running even after pulling out the cable if the app wasn't restated.
After I close the app and open it again, the notification are not working anymore.
Notification Handler:
import Foundation
import UserNotifications
final class NotificationHandler {
public static func scheduleLocalNotification(title: String, body: String, timeInterval: Double = 0.1) {
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
}
How I use it:
private func scheduleNotificationsIfNecessary(notifications: [Notification]) {
let newNotificationsIDs = notificationAppStorage.checkIfNotificationExists(ids: notifications.map { $0.id })
newNotificationsIDs.forEach { notificationID in
notificationAppStorage.addNotification(notificationID)
}
newNotificationsIDs.forEach { notificationID in
if let newNotification = findNotificationByID(notificationID) {
NotificationHandler.scheduleLocalNotification(
title: newNotification.symbol,
body: getNotificationBody(notification: newNotification.notification, profit: newNotification.profit, volume: newNotification.volume) )
}
}
}