Swift, BGTaskScheduler Local Notification

57 Views Asked by At

I created My class for iOS 13 and later with BGTaskScheduler. Thanks to this, for easy operation

import Foundation
import BackgroundTasks

class MyBGTaskService {
    let refreshBGIdentifier = "com.refresh.usyssoft.lifemotivation"
    func BgRegister() {
        let success = BGTaskScheduler.shared.register(forTaskWithIdentifier: refreshBGIdentifier, using: nil) { task in
            self.handleAppRefreshTask(task: task as! BGAppRefreshTask)
        }
        let message = "Registered background fetch task " + (success ? "successfully" : "unsuccessfully")
        guard success else { preconditionFailure(message) }
        print(message)
    }
    
    func handleAppRefreshTask(task: BGAppRefreshTask) {
        task.expirationHandler = {
            task.setTaskCompleted(success: false)
        }
        LocalNotification().createNotification()
        scheduleBackground()
        task.setTaskCompleted(success: true)
    }
    func scheduleBackground() {
        BgRegister()
        let fetchtask = BGAppRefreshTaskRequest(identifier: refreshBGIdentifier)
        fetchtask.earliestBeginDate = Date(timeIntervalSinceNow: 10)
        do {
          try BGTaskScheduler.shared.submit(fetchtask)
        } catch {
          print("Unable to submit task: \(error.localizedDescription)")
        }
    }
}

I registered with SceneDelegate willConnectTo bg.BgRegister(). I also ran my scheduleBackground function with sceneDidEnterBackground bg.scheduleBackground(). I ran the simulator by editing the scheme and activating the Launch Background. but I can't receive the notification. I tried the same operations in the AppDelegate didFinishLaunchingWithOptions and applicationDidEnterBackground functions, but I could not receive the notification. Where could I be making a mistake?

var window: UIWindow?
let bg = MyBGTaskService()
//let backgroundTaskService = BackgroundTaskService.shared

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    let auth = Auth.auth()
    if UserDefaults.standard.object(forKey: "firstInstall") is Int {
        let currentUser = auth.currentUser
        if currentUser != nil {
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let dc = sb.instantiateViewController(identifier: "HomeTabbarVC")
            window?.rootViewController = dc
        }
    }else {
        try? auth.signOut()
        UserDefaults.standard.set(1, forKey: "firstInstall")
    }
    
    //BackgroundTaskService.shared.delegate = self
    bg.BgRegister()
    guard let _ = (scene as? UIWindowScene) else { return }
    
    
}
func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
        // backgroundTaskService.scheduleAppRefresh()
        bg.scheduleBackground()
    }

I also activated info.plist and background mode.

Background Mode

info.plist description

My LocalNotification class is as follows:

class LocalNotification {
    struct notifyStruct {
        var title:String
        var body :String
        var timeInterval : Double
    }
    var notifyList = [notifyStruct]()
    
    func createNotification() {
        let notify1 = notifyStruct(title: "Hello", body: "Hello Notification Body", timeInterval: 10)
        let notify2 = notifyStruct(title: "Hi?", body: "Nice", timeInterval: 10)
        let notify3 = notifyStruct(title: "Bye", body: "Bye body", timeInterval: 10)
        self.notifyList.append(notify1)
        self.notifyList.append(notify2)
        self.notifyList.append(notify3)
        let random = Int.random(in: 0..<self.notifyList.count)
        let notify = self.notifyList[random]
        
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
            if granted {
                let content = UNMutableNotificationContent()
                content.title = notify.title
                content.body = notify.body
                content.sound = UNNotificationSound.default
                let trigger = UNTimeIntervalNotificationTrigger(timeInterval: notify.timeInterval, repeats: false)
                let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
                center.add(request)
            }
        }
    }
}
0

There are 0 best solutions below