iOS 10 don't call Notification Service Extension - ios

I tried to implement the new Notification Service Extension, but I have a problem.
In my NotificationService.swift file I have this code:
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 {
// Modify the notification content here...
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
print(bestAttemptContent.body)
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)
}
}
}
When I got a push notification the didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: #escaping (UNNotificationContent) -> Void) method never called.
Maybe I misunderstood how this extension is working?

Check your deployment target on Service Extension.
I had deployment target set to 10.2 when testing on device with 10.1 and extension wasn't called until I changed it.
Another issue might be debugging.. In order to make it work You need to attach Service Extension Process. In Xcode menu Debug > Attach To Process > Name of your extension

Run service extension as the Target instead of the app. Then it will ask for which app you have run service extension, then select your app and it will send the notification.
Make sure the deployment target of the service extension is less than your physical device's OS version.
Ensure payload contains 'mutable-content: 1'
{"aps" : {
"alert" : {
"title" : "Introduction To Notification",
"subtitle" : "Session 707",
"body" : "New Notification Look Amazing"
},
"sound" : "default",
"category" : "message",
"badge" : 1,
"mutable-content": 1
},
"attachment-url": "https://api.buienradar.nl/Image/1.0/RadarMapNL"
}
Don't add the content-available flag in aps or if you've added then make sure it's set to 0.
---------------------------------------- if nothing works, Restart your device --------------------------------------

I was getting crazy. Finally I realized that I had the deployment target of Notification Service Extension was 10.3 (my phone too). I changed to 10.2 and it works perfectly

If all else fails, restart your device. I just spent 2 hours on this and simply restarting my phone fixed it.

Your push notification payload should contain the "mutable-content" : 1 key value pair.
The remote notification’s aps dictionary includes the mutable-content key with the value set to 1.
Ex of push notification payload JSON:
{
"aps":{
"alert": {
"body": "My Push Notification",
"title" : "Notification title"},
"mutable-content" : 1,
"badge":0},
}
This works perfectly fine and i get the Push notification as follows:
Also Note That :
You cannot modify silent notifications or those that only play a sound or badge the app’s icon.
You can try Pusher or Houston for testing the Push Notifications.

I have the same issue but my problem was notification extension is "app". It should be appex

From docs on UNNotificationServiceExtension class:
The remote notification is configured to display an alert.
The remote notification’s aps dictionary includes the mutable-content key with the value set to 1.
You cannot modify silent notifications or those that only play a sound or badge the app’s icon.
Basically
Must include:
mutable-content: 1
an alert dictionary.
Must NOT include:
content-available: 1
To summarize Apple is doing its best to not allow apps to mutate silent notifications. It wants to allow that only on user notifications (user facing notifications)

If you trying to send push from Firebase console it's not possible to send "mutable_content": true.
So you should send it from server or postman or any type of terminal.
And your payload should be like this: For firebase
{
"to" : "your device token/ firebase topic",
"notification" : {
"body" : "This is an FCM notification that displays an image.!",
"title" : "FCM Notification"
},
"mutable_content": true
}
If not from FCM then your payload should be:
{"aps" : {
"alert" : {
"title" : "Your title",
"subtitle" : "Your subtitle",
"body" : "Your body"
},
"sound" : "default",
"category" : "message",
"badge" : 1,
"mutable-content": 1
},
"attachment-url": "https://yourattachment-url.com"
}
Note: And make sure the deployment target of the service extension is less that your physical device's OS version. Like if your development target is 10.3, your service extension version should be 10.2.

you need to add "mutable-content": 1 to your payload

This is a sample if you implement with Swift and Firebase cloud functions (Node.js) to send notifications has attachments with Notification Service Extension.
Below i have mentioned some important points only.
Payload sample
const payload = {
notification: {
title: name,
body: messageText,
badge: "1",
mutable_content: "true"
},
data: {
type: "MESSAGE",
fromUserId: name,
attachmentUrl: imageUrl
}};
mutable_content: "true" this part is the important i spend so much of time to find the exact naming most of the documentations are invalid now.
The next main thing is the way we have to run the App
Select the Notification Service Extension with your device
After when you run you will get a popup to Select your main project
This way it will call the NotificationService didReceive function (Which is inside the Notification Service Extension) when you receive a notification.

Related

Notification delegate not being called when app is in background when notification is received

I am using firebase cloud messaging to send push notifications to our iOS application. When the application is in the foreground everything works fine.
The problem is when i send the app to the background and then send a notification.
I expect the following delegate to be called, but it is not:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult)-> Void) {
I have ticked the "remote notifications" background mode in my apps signing & capabilities tab.
The payload i send to firebase looks like this:
{"to":"--TOKEN--",
"priority":"high",
"content_available":true,
"mutable_content": true,
"notification":
{"body":"Test Notification",
"title":"New Notification",
"sound":"default",
"click_action":"notification"
}
}
This payload is sent to firebase via https://fcm.googleapis.com/fcm/send
When i click an notification it is then processed, and i can see the apns version i receive looks like this:
[AnyHashable("google.c.sender.id"): 8xxxxxxxx, AnyHashable("gcm.message_id"): 1xxxxxxxxxx, AnyHashable("google.c.a.e"): 1, AnyHashable("google.c.fid"): fxxxxxxxxx, AnyHashable("aps"): {
alert = {
body = "Test Notification";
title = "New Notification";
};
category = feedback;
"content-available" = 1;
"mutable-content" = 1;
sound = default;
}]
I am not sure why content-available and mutable-content appear in quotes? I know firebase convers its payload to apns format, is there something wrong here?
I want the delegate to be called so that i can execute some code to maintain various data items, so it is important that i can run some code when my app is in the background and a notification is received.
I am not sure what config i am missing as everything i read seems to say this is all i need to do?

Can you set thread-id with FCM notifications for ios

Firebase FCM messages support tag for Android which causes a new notification to replace the previous one with the older one with the same tag. Is there any way to do the same for ios?
This answer suggest using thread-id in the data payload. But it didn't work for me.
There is no thread-id FCM payload key in Legacy FCM HTTP Server protocol. The workaround is to use iOS Notification Service Extension. Send the grouping identifier as "thread-id" key in FCM data payload and let the service extension read the "thread-id" key and set it to the notification content's threadIdentifier property.
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)
let userInfo: [AnyHashable : Any] = (bestAttemptContent?.userInfo)!
if let apsInfo = userInfo["aps"] as? [AnyHashable: Any], let bestAttemptContent = bestAttemptContent {
//add thread-id to group notification.
let patientId = userInfo["thread-id"] as! String
bestAttemptContent.threadIdentifier = patientId
contentHandler(bestAttemptContent)
}
}
}
As I mentioned in my answer that you linked:
However, there is currently no parameter counterpart for thread-id in FCM.
The workaround I mentioned is putting the thread-id in the data payload as a custom key value pair wasn't tested (I haven't dabbled with iOS that much so it was a long shot). But logically, if you're able to get the value of the data payload, you should be able to use the value as needed.
Alas, there still isn't any native way to set the thread-id parameter in the iOS payload. The only supported parameters are what is mentioned in the FCM docs.
Yes, you can set thread-id with FCM, but it serves to group notifications.
What you actually need to replace old notifications by new ones is apns-collapse-id
Use the same thread-id to visually group multiple notifications together.
Multiple notifications with the same thread-id are grouped and appear as a stack. Tapping on the stack expands all notifications with the most recent one being on top.
There's a collapseKey option which can be used to skip sending outdated notifications. If the device didn't receive a bunch of notifications and there's a newer one with the same key - only the latest will be pushed.
It seems this would also work to replace old notifications on Android, but not on iOS.
As per delivery options there's an apple specific key: apns-collapse-id
What's a collapsible message
A collapsible message is a message that may be replaced by a new message if it has yet to be delivered to the device.
Quoted from: Non-collapsible and collapsible messages
Setting apns-collapse-id
apns-collapse-id can be set using the lower level messaging APIs - send, sendAll and sendMulticast, where you can specify an apns key
messaging methods reference
low level payload reference
It's set in the apns.headers
const payload = {
tokens: [/*...*/],
data: {/*...*/},
notification: {
title: 'My Title',
body: 'My Content',
},
apns: {
headers: {
'apns-collapse-id': doc.id,
},
payload: {
aps: {
sound: 'default',
},
},
},
};
admin.messaging().sendMulticast(payload);
Setting thread-id
threadId can be set using the lower level messaging APIs - send, sendAll and sendMulticast, where you can specify an apns key
messaging methods reference
low level payload reference
It's set in the apns.payload.aps:
const payload = {
tokens: [/*...*/],
data: {/*...*/},
notification: {
title: 'My Title',
body: 'My Content',
},
apns: {
payload: {
aps: {
threadId: doc.id,
sound: 'default',
},
},
},
};
admin.messaging().sendMulticast(payload);
threadId behavior while offline
For me there are 2 cases:
Sending a few notifications results in the latest being received when the device regains connectivity
Be offline with existing (non dismissed) notification(s) for a given threadId. Regaining connectivity would add the latest notification to the top of the stack
Setting collapseKey
Higher level messaging APIs like sendToDeivce cannot tweak apns directly but can specify options
One of those options is options.collapseKey
const tokens = [/*...*/];
const payload = {
data: {/*...*/},
notification: {
title: 'My Title',
body: 'My Content',
sound: 'default',
},
};
const options = {
collapseKey: doc.id,
}
const response = await admin.messaging().sendToDevice(
tokens,
payload,
options,
);
FCM's HTTP v1 API should support thread-id. Check out the APNS field in https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages

iOS 10 FireBase when notification arrived, do not get call NotificationService.h Notification Service Extension

I use firebase and get successfully Notifications but when notification arrived, do not get call NotificationService.h Notification Service Extension.
I also set
NSExtensionPrincipalClass to NotificationService
NSExtensionPointIdentifier to com.apple.usernotifications.service
My notification is like
{
"registration_ids": ["devicetoken"],
"mutable_content": true,
"data":{
"post_details": {
"pcm_message": "asdf",
"pcm_img_url": "http://portalvhds34w6bf5z9b21h.blob.core.windows.net/images/1519365008_5a8fab90cf683.jpg",
}
},
"notification" : {
"title" : "demo push",
"body" : "this is push body" }
}
where is a problem or missing some information I have already set deployment target 10.0 in my whole project.

FCM Push Notification "content" entry

I'm using FCM to publish push notifications to my users, which is working great so far.
Currently I'm trying to implement a Notification Content Extension to deliver customized push notification and previews, which is working great with local notifications, following this post.
As far I know, I have to set the category entry and my notification category identifier in the push notification, in order to tell iOS, which notification UI it is supposed to use.
The problem is, when I send the following message to FCM, with the category entry set, FCM erases the entry or changes it to gcm.notification.category, depending where I place the category entry (aps / data, etc.)
This way iOS never shows my custom UI / extension. Unfortunately I was not able to find any help in the FCM documentation.
Send (POST: https://fcm.googleapis.com/fcm/send):
{
"notification": {
"title": "Good Morning",
"body": "Wake up Jack!",
"badge" : 1,
"sound" : "horn.aiff",
"category" : "Cheers" <-- Is going to be deleted / changed
},
"data" : {
"time" : "2018-01-19 23:00:00",
...
},
"mutable_content" : true,
"priority" : "high",
"registration_ids" : [
"abcdefg123456"
]
}
Received:
{
aps = {
alert = {
body = "Wake up Jack!";
title = "Good Morning";
};
badge = 1;
"mutable-content" = 1;
};
"gcm.message_id" = "0:1516392279506894%dc84760ddc84760d";
"gcm.notification.category" = "Cheers"; <-- not working
}
The category APNS parameter FCM counterpart is click_action.
When adding in a custom parameter (using the data message payload), it is handled differently for iOS and is often included outside of the aps payload (like in the sample you provided).

Push Notification Delivery and Dismiss Callback

Can we make a service call from the client side when a push notification is delivered or dismissed when the app is not active?
Also does APNS give any information on whether the notification is delivered at an individual level and is there any api we can use to query the delivery status report?
Yes you can detect delivery of remote notification if you send the content-available flag to 1 in the aps dictionary. If you set the key, application:didReceiveRemoteNotification:fetchCompletionHandler: method of the AppDelegate is called.
Also, from iOS 10 onwards, you can also detect dismissal of remote notifications. For that you need to implement UserNotifications Framework.
You need to perform the below steps for the same:
Use iOS 10 APIs to register the categories for remote notifications.
You need to check [[UNUserNotificaionCenter currentNotificationCenter] setNotificationCategories] method for the same. See https://developer.apple.com/documentation/usernotifications/unusernotificationcenter
iOS 10 introduces a new class called UNNotificationCategory for encapsulating a category instance. You need to set the CustomDismissAction on the category instance to be able to receive the dismiss callback.
See https://developer.apple.com/documentation/usernotifications/unnotificationcategory
Implement the UNUserNotificationCenterDelegate protocol with the implementation of userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: method. This method will receive the callback to the dismiss action provided you perform the above steps.
See https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate
Set your delegate to the UserNotificationCenter - [[UNUserNotificaionCenter currentNotificationCenter].delegate = your_delegate_implementation
Set the category with CustomDismissAction property in the payload.
{
"aps": {
"alert": "joetheman",
"sound": "default",
"category": "my-custom-dismiss-action-category",
"content-available":1
},
"message": "Some custom message for your app",
"id": 1234
}
Yes you can make a service call from the ios app when a notification is received when app is not active. Do this in:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
<#code#>
}
yes you can add additional key value pair in your push notification payload.
{
"aps": {
"alert": "joetheman",
"sound": "default"
},
"message": "Some custom message for your app",
"id": 1234
}

Resources