I have a push notification service (UNNotificationServiceExtension) that intercepts my push notifications to show rich push notifications. I tried to contact my server but I think the service is terminated before it runs all the code. I figured this out by the [modified] bit at the end not being there with my code and there without my code.
I've looked into silent notifications but I found out that that doesn't run when the user kills the app so that's no good.
Now I am thinking to do something along the lines of:
let queue = DispatchQueue.global(qos: .background)
queue.async {
self.contactServer()
}
It runs but the server is never contacted. So now I am not sure if the code works not? What do I do? How do I pass a task to the system to run it in the background because I think this service is too fast to actually wait for the server to be contacted. Sorry, I am new to swift and iOS in general.
Is there a way I can do something along the lines of:
await contactServer()
NOTE: I know Swift doesn't have await but you get what I am trying to say here.
Here is my full code:
import UserNotifications
import ServerEngine
import UIKit
class NotificationService: UNNotificationServiceExtension {
var backgroundTask: UIBackgroundTaskIdentifier!
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
doBgTask()
contentHandler(bestAttemptContent)
}
}
func doBgTask() {
let queue = DispatchQueue.global(qos: .background)
queue.async {
serverEngine.touchBase()
}
}