As the title states, i am currently unable to schedule a task from within a UNNotificationServiceExtension. I would like to schedule the task when the following function is called and thus a push is received:

func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void)

Currently as soon as i call:

try BGTaskScheduler.shared.submit(request)

The catch clause is entered and the following error is returned: 'The operation couldn’t be completed. (BGTaskSchedulerErrorDomain error 1.)'

The push messages are send with Firebase cloud messaging via the following url and are received perfectly in my app: https://fcm.googleapis.com/v1/{path to project}. I call this url from within POSTMAN. I also specify the access token and the json body.

The apns header in the push message is set to "apns-push-type": "background". And the payload.aps has the following values: "apns-priority": "5","content-available": 1, "mutable-content": 1. I think this all works correctly, as i verified this.

The strange thing is that i did get the background task to work from just within the actual app, so without firing it from within the UNNotificationServiceExtension. I tried this to check if the code actually works at all.

I have done my research and so far i have tried the following already:
1. I made sure 'Permitted background task scheduler identifiers' in the info.plist is filled with the correct task identifier. I have triple checked that this string gets used correctly in the code as well.
2. Enabled the following capabilities in the app: 'Background fetch', 'Remote notifications', 'Background processing'.
3. In the info.plist the following UIBackgroundModes are supported: 'audio', 'fetch', 'processing', 'remote-notification' and 'voip'. These same UIBackgroundModes are entered in the UNNotificationServiceExtension info.plist as well (i did that just to be sure).
4. I made sure the task gets registered in the AppDelegate in 'didFinishLaunchingWithOptions'
5. I call the 'expirationHandler' and the 'setTaskCompleted' every time the task is actually fired

Any help or guidance is deeply appreciated!
Thanks in advance ;-)

1

There are 1 best solutions below

2
On

It seems that background tasks can not be scheduled on a simulator, I got the same error as you when using the simulator. When running on a real device everything is fine. Hope this could help you.