HomeiOS DevelopmentIOS swift native notification shouldn't be working

IOS swift native notification shouldn’t be working


Beneath is my code. Mainly, I’ve a climate app. I needed to Write a swift code, the place I’ll use BGTaskScheduler to schedule a background process each minute.
The background process will set an area notification utilizing UserNotifications.
The duty will first name a perform named getWeather which can return the climate situation.
Set the climate situation as a notification description. And the notification title would be the timestamp. The registerBackgroundTask() perform is known as from AppDelegate after granting notification from didFinishLaunchWithOptions. Even in the event you can’t present resolution, telling me methods to debug if the background process is ready correctly And methods to debug if the notification is ready correctly will assist too.

    func registerBackgroundTask() {
        let taskIdentifier = "internet.tazwar.abohaoa"
        BGTaskScheduler.shared.register(forTaskWithIdentifier: taskIdentifier, utilizing: nil) { process in
            self.handleBackgroundTask(process: process as! BGProcessingTask)
        }

        // Schedule the background process to run each minute
        let request = BGProcessingTaskRequest(identifier: taskIdentifier)
        request.requiresNetworkConnectivity = false
        request.requiresExternalPower = false

        do {
            strive BGTaskScheduler.shared.submit(request)
        } catch {
            print("Unable to schedule background process: (error.localizedDescription)")
        }
    }
    
    func handleBackgroundTask(process: BGProcessingTask) {
        // Fetch climate situation
        let weatherCondition = getWeather()

        // Create a notification content material
        let content material = UNMutableNotificationContent()
        content material.title = DateFormatter.localizedString(from: Date(), dateStyle: .medium, timeStyle: .brief)
        content material.physique = weatherCondition

        let set off = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
        let request = UNNotificationRequest(identifier: "weatherNotification", content material: content material, set off: set off)

        // Add the notification request to the notification middle
        UNUserNotificationCenter.present().add(request) { error in
            if let error = error {
                print("Failed so as to add notification request: (error.localizedDescription)")
            }
        }

        process.setTaskCompleted(success: true)
        scheduleNextBackgroundTask()
        process.expirationHandler = {}
    }

    func scheduleNextBackgroundTask() {
        let taskIdentifier = "internet.tazwar.abohaoa"
        let request = BGProcessingTaskRequest(identifier: taskIdentifier)
        request.requiresNetworkConnectivity = false
        request.requiresExternalPower = false

        // Schedule the background process to run each minute
        let nextFireDate = Date(timeIntervalSinceNow: 60)
        request.earliestBeginDate = nextFireDate

        do {
            strive BGTaskScheduler.shared.submit(request)
        } catch {
            print("Unable to reschedule background process: (error.localizedDescription)")
        }
    }

    func getWeather() -> String {
        return "Sunny"
    }

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments