ios - can you run a background task in a notification service? -
i have push notification service (unnotificationserviceextension) intercepts push notifications show rich push notifications. tried contact server think service terminated before runs code. figured out [modified] bit @ end not being there code , there without code.
i've looked silent notifications found out that doesn't run when user kills app that's no good.
now thinking along lines of:
let queue = dispatchqueue.global(qos: .background) queue.async { self.contactserver() }
it runs server never contacted. not sure if code works not? do? how pass task system run in background because think service fast wait server contacted. sorry, new swift , ios in general.
is there way can along lines of:
await contactserver()
note: know swift doesn't have await trying here.
here 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 notification content here... bestattemptcontent.title = "\(bestattemptcontent.title) [modified]" dobgtask() contenthandler(bestattemptcontent) } } func dobgtask() { let queue = dispatchqueue.global(qos: .background) queue.async { serverengine.touchbase() } }
Comments
Post a Comment