Quick overview over the problem:
Clicking run the app selecting the notification extension target, files within the notification are debugged normally, function just as expected.
However when I run the app selecting the main app target, the same files are not debugged and my own functions are not being called, except that the didReceive()
function does its work by modifying the notification content as expect in all cases.
bellow you will find a function called apiCall()
, the function is called and an api call is performed normally when running the app from the notification extension target, however it is not called when running from the main target.
Solutions not to think about:
mutable-content: 1
is available otherwise content won't be modified.NotificationExtension
is available inside theTarget dependencies
underBuild phases
section of themain target
,- All used libraries are under
Link Binary With Libraries
underBuild phases
section of thenotification extension target
. Background Modes
:Background fetch
,Remote notifications
are already checked incapabilities
- don't look for a code problem since in works when building from the
extension target
.
My notification extension code:
class NotificationService: UNNotificationServiceExtension, UNUserNotificationCenterDelegate {
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 {
...
bestAttemptContent.title = "[modified]"
bestAttemptContent.body = "[modified]"
DispatchQueue.main.asyncAfter(deadline: .now() + 30){
apiCall()
}
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}