In iOS 12, can a UNNotificationServiceExtension use UNUserNotificationCenter.current().add(notificationRequest, withCompletionHandler:) ?

Hi,


Question


In iOS 12, will a UNNotificationServiceExtension ever be allowed to add local notifications using UNUserNotificationCenter? If it requires an entitlement to be added, which entitlement is needed?


Background


We have implemented an App extension that extends UNNotificationServiceExtension.


On iOS 10 and 11 we have successfully used it to implement a feature in our App that lets the user choose to get reminders of remote notifications if they have not responded to them. The UNNotificationServiceExtension examines the content of each remote notification and, if they meet the criteria the user has configured for reminders, we construct a UNNotificationRequest and use UNUserNotificationCenter.current().add() to schedule a local notification.


This works on iOS 10 and 11, but not on iOS 12 beta 12 (16A5366a). Instead the extension gets an error "Notifications are not allowed for this application" and the device logs contain the entry


SpringBoard 2018-09-11 11:53:42.581817 -0700 Not allowing service extension 'com.myapp.MyService' to request user notifications at this time


I have tried changing the extension to request authorization before adding the local notification:


UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, grantError) in

if let err = grantError {

Logger.log("grantError: \(err.localizedDescription)")

} else if granted {

Logger.log("requestAuthorization was granted")

scheduleLocalNotifications(request: request, accountSettings: accountSettings)

} else {

Logger.logWarn("requestAuthorization was not granted")

}

}



but this results in the same "Notifications are not allowed for this application" error and now the iOS device logs contain:


SpringBoard 2018-09-11 10:53:47.496252 -0700 [com.myapp] Requesting authorization with options 6

SpringBoard 2018-09-11 10:53:47.498404 -0700 Not allowing service extension 'com.myapp.MyService' to request user notifications at this time

SpringBoard 2018-09-11 10:53:47.498801 -0700 [com.myapp] Requesting authorization failed because of missing entitlement

MyService 2018-09-11 10:53:47.499657 -0700 [com.myapp] Requested authorization [ didGrant: 0 hasError: 1 hasCompletionHandler: 1 ]


Hence my question: is what I am trying to do just not allowed in iOS 12, or is there an entitlement I need to add to my extension?

Accepted Reply

The solution for me was:


1. don't need to call UNUserNotificationCenter.current().requestAuthorization()


2. only call the Notification Extension's contentHandler once the UNUserNotificationCenter.current().add() method had fully completed.


UNUserNotificationCenter.current().add(notificationRequest, withCompletionHandler: { (_ error: Error?) -> Void in

contentHandler(bestAttemptContent)

})

Replies

I have the same (Disallowed notification center access to service extension") issue with ios12.

I was removing some of the existing (delivered) notifications based on the content of the new one.


Strangely enough e.g. WhatsApp is still doing this - removing notification after it has been delivered and they are doing it a)from the service or b)some private api as I killed it off (thus silent push wouldn't work) but notifcation still got removed if I read it e.g. in the browser.


console reports:


[***.push] Disallowed notification center access to service extension

The solution for me was:


1. don't need to call UNUserNotificationCenter.current().requestAuthorization()


2. only call the Notification Extension's contentHandler once the UNUserNotificationCenter.current().add() method had fully completed.


UNUserNotificationCenter.current().add(notificationRequest, withCompletionHandler: { (_ error: Error?) -> Void in

contentHandler(bestAttemptContent)

})