apns-collapse-id not merging multiple notifications iOS 10 - ios

I am trying to collapse/merge multiple notifications belonging to same event in my app. iOS 10 introduced a key name apns-collapse-id in payload. Document says multiple remote notifications with same apns-collapse-id will merged and shown as single notification.
So we have the payload with apns-collapse-id and thread-id in our notification payload. Unfortunately as there is no much content online explaining how these keys actually works, for safe play we have used the same unique value for both apns-collapse-id and thread-id.
We are expecting the multiple remote notifications with same apns-collapse-id to merge. It is not happenning.
Few people suggested APNS collapse does not come for free and I have to implement UNNotificationContentExtension and handle the notifications manually. I know I can add and remove local notification already posted but not really sure how to update the already posted remote notification.
Here is how our payload looks like
payload {
"aps": {
"alert": {
"title": "Some title : ",
"body": "Some body text"
},
"sound": "default",
"payload": {
"target": {
"some key" : "Some value"
},
"thread_id": "Some_string_155863",
},
"apns-collapse-id": "Some_string_155863",
"mutable-content": 1,
"thread-id": "Some_string_155863",
"badge": 33
},
"thread-id": "Some_string_155863",
"apns-collapse-id": "Some_string_155863",
"mutable-content": 1
}
as you can see apns-collapse-id and thread-id are repeated multiple times in JSON because we aren't sure where exactly to have them
We are not using HTTPS/2 yet, but I believe iOS 10 notfication still supports legacy HTTPS. I am not really a network guy so I don't understand these things completely. Ignore my arrogance towards Https and Http2
Has anybody managed to get apns-collapse-id to work ? Please help.

apns-collapse-id is a request header, so you need to use the HTTP service and not the legacy binary interface.

Related

Getting silent notification values when app starts

I am trying to implement cache meachanism for FCM on IOS. I am using Xamarin Forms with CrossGeeks FirebasePushNotificationPlugin
Push notification service works fine. I am using payload pattern as below
{
"data": {
"message" : "my_custom_value",
"other_key" : true,
"body":"test"
},
"notification": {
"body" : "hello",
"title": "firebase",
"sound": "default",
"content_available" : true
},
"priority": "high",
"condition": "'general' in topics"
}
Above pattern appears delivered notification as alert on IOS main screen.
I have written a Dependency Service for getting undeleted or unopened accumulated notifications when app starts. Dependency Service include below codes
await UNUserNotificationCenter.Current.GetDeliveredNotificationsAsync();
When I use above payload, I can get accumulated notifications. But when I use payload pattern as below, I can not.
{
"data": {
"message" : "my_custom_value",
"other_key" : true,
"body":"test"
},
"notification": {
"content_available" : true
},
"priority": "high",
"condition": "'general' in topics"
}
Actually I would like to send 2 payload types for every notification for my cache mechanism because of prevent the user's delete action. After that, I would like to show cached notifications without any lost in my app's notification page.
Shortly my question about "I am not getting silent payload values from UNUserNotificationCenter this approach is possible or not?"
Or can I prevent the user's notification delete action from IOS main screen when my app is terminated.
Note: My minimum OS version is 10.0 in info.plist
Thank you in advance.

Multi line title in push notification for iOS

I'm integrating FCM for an app which has both iOS and Android version.
Currently, for iOS, I want to send title in multiple lines. How do I do this? I'm trying out using Firebase console to test. But nothing seems to work for me.
I tried with these
\n
<br/>
\\n
\r\n
as suggested in this post. But nothing really worked.
I basically want the first 2 lines of the notification to be in bold. Any help much appreciated.
The 1st line in bold you see is a notification Title, the second the Subtitle, and the Content underneath (regular font).
These are properties of a notification, since iOS 10 set on UN(Mutable)NotificationContent (subtitle line is not available before).
They properties can be configured in the payload sent via APNS:
{
"aps": {
"alert": {
"title": "Custom title",
"subtitle": "Custom subtitle",
"body": "Custom message"
}
}
}
Also, you can create a UserNotificationServiceExtension to update the above properties of a notification that has been received.

Notification from FCM is sometimes displayed in iOS notification tray

I am working on a project where we are using Firebase Cloud Messaging for push notifications. The following JSON is currently being produced by the backend API and sent to FCM:
{
"priority": "normal",
"delay_while_idle": true,
"dry_run": false,
"time_to_live": 3600,
"notification": {
"body_loc_key": "MyCustomNotificationId"
},
"data": {
// contains notification data
},
"registration_ids": [
]
}
This notification should be silent, meaning it should be only received when the iOS application is in the foreground, however sometimes on some devices this notification also finds its way to the iOS notification tray with the app being in the background as if it had parameters to be displayed there.
I have found out the body_loc_key attribute must be present to iOS devices, otherwise the notification won't ever hit the device, whether the application is in the foreground or background.
The problem occured on the following devices:
Apple iPhone 5,
Apple iPhone 6,
with the possibility of others being affected as well.
Is there a different configuration for the JSON sent to FCM you have used with a success, where the notifications are only being sent to the device when the application is in the foreground?
After fiddling with the FCM payload for sime time we have found out the problem was actually the body_loc_key attribute all along.
In order for a notification to be silent and still sent to an apple device, the payload must meet the following criteria:
priority must be set to normal,
content_available must be set to true,
notification attribute must contain data, but should it contain the body_loc_key attribute it must be set to an empty string - "".
Working payload examples:
// Example one
{
"priority": "normal",
"delay_while_idle": true,
"dry_run": false,
"time_to_live": 3600,
"content_available": true,
"notification": {
"body_loc_key": ""
},
"data": {
// contains notification data
},
"registration_ids": [
]
}
// Example two
// (note that body_loc_key has been replaced with badge)
{
"priority": "normal",
"delay_while_idle": true,
"dry_run": false,
"time_to_live": 3600,
"content_available": true,
"notification": {
"badge": 10
},
"data": {
// contains notification data
},
"registration_ids": [
]
}
Changing the body_loc_key to an empty string pretty much fixed the issue. On top of that we have also found out the following about other attributes of the notification attribute:
badge may be present and is processed, notification remains silent,
title_loc_key has no effect, notification remains silent,
body_loc_args has no effect, notification remains silent.
All three additions apply to scenario where the precedent criteria have been met (empty body_loc_key if/when present, etc.).
The reason why your notification is being sent as display-message and not as silent-message is because you are using the notification payload.
In particular you are using the body_loc_key.
You wrote that you read that body_loc_key is required to send silent-messages.
This is not true.
Can you please link the page where you found this?

Can't send push notifications using the server API

Im using the new Firebase platform. Am trying to get a push notification sent by my app server and delivered to my iPhone.
I have the setup working where I manually send the message with the Firebase notifications area on the website, but when I try and send the message with a POST to https://fcm.googleapis.com/fcm/send I get no message being delivered to the device.
I'm sending the following (with auth headers)
{ "notification": {
"title": "Portugal vs. Denmark",
"text": "5 to 1"
},
"to" : "<registration token>"
}
I'm getting a 200 response from the POST with the following body:
{
"multicast_id": 5511974093763495964,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1463685441784359%3ad254b53ad254b5"
}
]
}
If I try and send to this device directly through the Firebase website it works, but the above form post doesn't. No idea where to go from here!
On iOS the priority field seems mandatory.
{
"to": "cHPpZ_s14EA:APA91bG56znW...",
"priority": "high",
"notification" : {
"body" : "hello!",
"title": "afruz",
"sound": "default"
}
}
If the API returns you a message_id it means that your message has been correctly accepted and it will eventually be delivered to the device.
On Android, messages are delivered as soon as possible (provided that the device is connected of course).
On Apple devices, IF the application is closed or in background, the notification is sent through Apple infrastructure and can be delayed accordingly with Apple documentation.
To reduce the delay of the prioritymessages sent to Apple device you can use the priority parameter.
More details: https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message
I found the message field to be required, as well as the priority field to deliver a message with a POST.
message is not required (and labeled as optional) in the Firebase Console.
I resolved adding the notification tag in request body without priority.
Here is a recap:

Is the following structure with body and loc-args in the same notification payload valid?

Would the following payload notification cause any problems if you have body and loc-args and loc-key in the same payload?
{"aps":{"alert":{"body" : "Bob wants to play poker", "loc-key":"general","loc-args":["Test DEFAULT."]},"badge":1,"sound":"default"}}
The reason I have it set up like this. Is that I am not sure if the notification to just have a body in would be handle during the app, so it would use the localized loc-args in with the following key-valus in the localizable string:
"general"="%#";
I've made several tests.
For example sending
{
"aps": {
"alert": {
"body": "hello world!",
"loc-args": ["andy", "my text"],
"loc-key": "comment"
}
},
"sound": "default",
"extras": {}
}
On my device I have a string in the loc file that looks like:
"comment" = "%# has comment %#";
So the notification that I'm receiving is:
Andy has comment my text
Now I've sent a new notification like this:
{
"aps": {
"alert": {
"body": "hello world!",
"loc-args": ["andy", "my text"],
"loc-key": "does_not_exist"
}
},
"sound": "default",
"extras": {}
}
with a loc-key that does not exist, and the notification that I'm receiving is
does_not_exist
so as kadam said in his answer:
Ultimately, one can infer from the docs that body and loc-key should
be used in an exclusive or relationship. Either one or the other, but
not both.
Blockquote
According to this post, if the key is missing the key is used. That's why 'body' is always ignored and is EITHER ONE or the OTHER.
There are two options to localize an alert message embedded in a push notification. One is to localize on the server and send the localized message as a string value of the alert key. Eg:
{"alert": "A localized sentence."}
Optionally, if you want to also set the launch image and/or have the view button of the alert read something other than 'View', you would replace the string value of alert with a dictionary. In this case the string value should be the value of body in the dictionary. Eg:
{'alert':
{'body: "A localized sentence.", action-loc-key: "...", launch-image: "..."'
}}
The docs warn that if you only have the body key in the dictionary, don't use a dictionary. Instead set the value of body as the string value of alert.
The other option is to use the loc-key, loc-args pair and have the localization take place on the device.
Although I have not verified which one iOS defaults to if both body and loc-key are present in the alert dictionary, I guess it will pick one and ignore the other. Someone with access to a working push notification dev setup could verify which one.
Ultimately, one can infer from the docs that body and loc-key should be used in an exclusive or relationship. Either one or the other, but not both.
I'm not 50 yet so I can't comment but to follow-up on #kadam answer, just for future reference to those who will visit this question.
Although I have not verified which one iOS defaults to if both body and loc-key are present in the alert dictionary, I guess it will pick one and ignore the other. Someone with access to a working push notification dev setup could verify which one.
I have tried it and it defaults to the value of loc-key if it has a value on the app itself. If none then it uses the value of body.

Resources