iOS Development

IOS swift native notification just isn’t working

Spread the love


Beneath is my code. Mainly, I’ve a climate app. I wished to Write a swift code, the place I’ll use BGTaskScheduler to schedule a background activity each minute.
The background activity will set an area notification utilizing UserNotifications.
The duty will first name a perform named getWeather which is able to 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 case you can’t present resolution, telling me how one can debug if the background activity is ready correctly And how one can debug if the notification is ready correctly will assist too.

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

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

        do {
            attempt BGTaskScheduler.shared.submit(request)
        } catch {
            print("Unable to schedule background activity: (error.localizedDescription)")
        }
    }
    
    func handleBackgroundTask(activity: 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)")
            }
        }

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

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

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

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *