Using UNNotificationServiceExtension with FCM not getting called

I don't know what could be wrong but my UNNotificationServiceExtension is never getting called by any push from FCM. I call the FCM from a Python3 script and so far it works (I get a push notification but always with the default text and not the modified one).
Code Block
firebaseHeaders = {
"Content-Type":"application/json",
"Authorization":"key=MY_KEY"
}
firebaseBody = {
"to":device_token,
"priority":"high",
"mutable-content":True,
"apns-priority":5,
"notification": { "title_loc_key":"push.goalReachedTitle", "body_loc_key":"push.goalReachedContentRemote", "body_loc_args":[str(entry['value']), entry['region']], "sound":"default", "badge":1 },
"data": { "values":data, "region":entry['region'], "value":entry['value'] }
}
firebaseResult = requests.post("https://fcm.googleapis.com/fcm/send", data=None, json=firebaseBody, headers=firebaseHeaders)

My Extension is also pretty basic (for testing) and I already tried to remove it and add it again to my main app project without success.
Code Block
class NotificationService: UNNotificationServiceExtension {
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 = bestAttemptContent.title + " [TEST 123]"
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)
}
}
}

Anyone has a idea what I still can check, test or miss?

Replies

What is FCM ? Firebase Cloud Messaging ? Using so many acronyms …

How do you know it is not called ?
Did you include a print statement to check for it ?

The error is probably elsewhere:

Code Block
if let bestAttemptContent = bestAttemptContent {


you create a NEW local bestAttemptContent, change its title. But the original one is unchanged, most likely.

Try:
Code Block
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
print("bestAttemptContent", bestAttemptContent)
if bestAttemptContent != nil {
bestAttemptContent!.title = bestAttemptContent!.title + " [TEST 123]"
contentHandler(bestAttemptContent!)
}
}


Thanks for the reply.

The line you mentioned is from the template that Xcode generates if you create such an extension. So if this is the problem, Apple included a not working template which would be very bad.

The debugger is also never hit if I place breakpoints in it, even if they are on line one of the function.

And yes, FCM is the Firebase Cloud Messaging which seems to work (I get push notifications at least but I don't know if something prevents it from including the mutable content flag; tried "mutable-content" as from Apples documentations as well as "mutable_content" from the FCM documentation).
Do you get the message from
Code Block
print("bestAttemptContent", bestAttemptContent)

printed ?
What do you get there ?

The line you mentioned is from the template that Xcode generates if you create such an extension.

Do you speak of line 10 ?
Code Block
if let bestAttemptContent = bestAttemptContent {

Note that there is no problem with line 19.
Code Block
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {

Can you show exactly what is created automatically ?

It's a Google framework, so I would be surprised it is Apple code…

Whoever wrote this line, did you try the change I proposed for line 10?
Code Block
if bestAttemptContent != nil {


The line with " if let bestAttemptContent = bestAttemptContent {" is in the template of Xcode and does not come from Google at all. That is how Xcode creates this service extension if added.

And the print yields nothing because it is never getting called. I receive the push on my device but this extension won't get called for some reason..
Have a look here. Deployment target could be the problem.

https://stackoverflow.com/questions/39663315/ios-10-dont-call-notification-service-extension


Deployment target is set to 14.0 for all parts of my app (Main app, Widget extension, Watch extension and this one as well). So I doubt it could cause this issue.

@Sebastian1989101 Did you ever find the solution for this issue?