Problem when sending 2-layer-json-payload from FCM to iOS devices - ios

I had a problem with sending push notification from FCM server. Previously, we use APNS for this purpose, my server and clients create payload like this. And it worked very well.
{
"data": {
"image": "https://premierleague-static-files.s3.amazonaws.com/premierleague/photo/2018/09/24/0e228e97-1644-4fcf-bc18-d7223d8f398f/DreamTeamGW6.png",
"link":"https://stackoverflow.com/"
},
"aps": {
"alert": "This is me",
"sound": "default",
"mutable-content": 1
},
"contentId": "123456"
}
Now, we turn to use FCM as replacement of APNS, as you've known, FCM server will receive the message, convert it to APNS format then send it to APNS server, the APNS server will send the converted message to clients. But first, I have to follow its payload's format like this.
{
"notification": {
"body": "This is me",
"badge": 1,
"sound": "default",
"mutable-content": 1
},
"delay_while_idle": false,
"data": {
"data": {
"image": "https://premierleague-static-files.s3.amazonaws.com/premierleague/photo/2018/09/24/0e228e97-1644-4fcf-bc18-d7223d8f398f/DreamTeamGW6.png",
"link":"https://stackoverflow.com/"
},
"contentId": "123456"
},
"time_to_live": 10
}
And the converted message from FCM to APNS was not I desired.
{
"data": "{\"image\":\"https:\\\/\\\/premierleague-static-files.s3.amazonaws.com\\\/premierleague\\\/photo\\\/2018\\\/09\\\/24\\\/0e228e97-1644-4fcf-bc18-d7223d8f398f\\\/DreamTeamGW6.png\",\"link\":\"https:\\\/\\\/stackoverflow.com/\\\/\"}",
"aps": {
"alert": "This is me",
"sound": "default",
"mutable-content": 1
},
"contentId": "123456"
}
As you see, the value of "data" key was not JSONObject like before, it became a string. My question is: How can I make the FCM server understand that the sub "data" object inside main "data" is a JSONObject, not a string when it convert my message to APNS's payload?
Thank you!

In your FCM payload (reference for the FCM payload parameters here):
badge should be String
mutable_content should be outside notification
delay_while_idle is deprecated
data messages can only hold key-value pairs. You're passing a (data) JSON object which is not expected to work.
The fastest way I could think of in order to make this work is have the contents of your data JSON object outside (i.e. same level as the contentId and then format your FCM payload like so:
{
"mutable-content": 1
"notification": {
"body": "This is me",
"badge": 1,
"sound": "default"
},
"data": {
"image": "https://premierleague-static-files.s3.amazonaws.com/premierleague/photo/2018/09/24/0e228e97-1644-4fcf-bc18-d7223d8f398f/DreamTeamGW6.png",
"link":"https://stackoverflow.com/",
"contentId": "123456"
},
"time_to_live": 10
}
But depending on your client side code, this couldn't work, but I hope you get the gist. Cheers!

Related

iOS push notification not working in ionic application. using cordova plugin add cordova-plugin-firebase-messaging

We are using the firebase-messaging plugin for push notification. In android this works fine, but in iOS, the response JSON shows is not registered, despite that we are using the token-id which was generated by the plugin. Please, someone, find any solution to this problem.
Request JSON
{
"notification": {
"sound": "alert",
"title": "2022-07-25",
"body": "Your One Time Password is 5360"
},
"to": "mobile token id"
}
Response JSON
{
"multicast_id": 8005322019976108918,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "NotRegistered"
}
]
}

Azure Logic App: Read telemetry data as dynamic content from IoT hub message

I'm routing telemetry messages via IoT Events and event Grid to Logic Apps using a webhook. The logic app lets you input a sample JSON message and then use dynamic content to add information to an email alert I'm sending(O365: Send an Email V2)
I can include System Properties like "iothub-connection-device-id" But when I try to pick temeletry data I get the following error:
InvalidTemplate. Unable to process template language expressions in action 'Send_an_email_(V2)' inputs at line '1' and column '1680': 'The template language expression 'items('For_each')?['data']?['body']?['windingTemp1']' cannot be evaluated because property 'windingTemp1' cannot be selected. Property selection is not supported on values of type 'String'. Please see https://aka.ms/logicexpressions for usage details.'.
When I look at the raw output of the webhook connector it shows the following message but the telemetry points are cleary not there. I'd expect to see them in the "body" property but instead there is just the string: "eyJ3aW5kaW5nVGVtcDEiOjg2LjYzOTYxNzk4MjYxODMzLCJ3aW5kaW5nVGVtcDIiOjc4LjQ1MDc4NTgwMjQyMTUyLCJ3aW5kaW5nVGVtcDMiOjg1LjUzMDYxMDY5OTQ1MzY1LCJMb2FkQSI6MjAyOS44NDgyMTg4ODYxMTEsIkxvYWRCIjoyMDQwLjgxMDk4OTg0MDMzMzgsIkxvYWRWIjoyMDA0LjYxMTkzMjMyNTQ2MTgsIk9pbFRlbXAiOjk5LjA2MjMyNjU2MTY4ODU4fQ=="
Looking for help to determine what could be causing this and how to get the telemetry data passed through correctly so that I can inculde it dynamically in the email alert.
Thanks!
{
"headers": {
"Connection": "Keep-Alive",
"Accept-Encoding": "gzip,deflate",
"Host": "prod-24.northeurope.logic.azure.com",
"aeg-subscription-name": "TEMPALERT",
"aeg-delivery-count": "1",
"aeg-data-version": "",
"aeg-metadata-version": "1",
"aeg-event-type": "Notification",
"Content-Length": "1017",
"Content-Type": "application/json; charset=utf-8"
},
"body": [
{
"id": "c767fb91-3806-324c-ec3c-XXXXXXXXXX",
"topic": "/SUBSCRIPTIONS/XXXXXXXXXXXX",
"subject": "devices/Device-001",
"eventType": "Microsoft.Devices.DeviceTelemetry",
"data": {
"properties": {
"TempAlarm": "true"
},
"systemProperties": {
"iothub-connection-device-id": "Device-001",
"iothub-connection-auth-method": "{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
"iothub-connection-auth-generation-id": "637264713410XXXX",
"iothub-enqueuedtime": "2020-06-01T23:05:58.3130000Z",
"iothub-message-source": "Telemetry"
},
"body": "eyJ3aW5kaW5nVGVtcDEiOjg2LjYzOTYxNzk4MjYxODMzLCJ3aW5kaW5nVGVtcDIiOjc4LjQ1MDc4NTgwMjQyMTUyLCJ3aW5kaW5nVGVtcDMiOjg1LjUzMDYxMDY5OTQ1MzY1LCJMb2FkQSI6MjAyOS44NDgyMTg4ODYxMTEsIkxvYWRCIjoyMDQwLjgxMDk4OTg0MDMzMzgsIkxvYWRWIjoyMDA0LjYxMTkzMjMyNTQ2MTgsIk9pbFRlbXAiOjk5LjA2MjMyNjU2MTY4ODU4fQ=="
},
"dataVersion": "",
"metadataVersion": "1",
"eventTime": "2020-06-01T23:05:58.313Z"
}
]
}
Here is the sample input I am using with the trigger:
[{
"id": "9af86784-8d40-fe2g-8b2a-bab65e106785",
"topic": "/SUBSCRIPTIONS/<subscription ID>/RESOURCEGROUPS/<resource group name>/PROVIDERS/MICROSOFT.DEVICES/IOTHUBS/<hub name>",
"subject": "devices/LogicAppTestDevice",
"eventType": "Microsoft.Devices.DeviceTelemetry",
"eventTime": "2019-01-07T20:58:30.48Z",
"data": {
"body": {
"windingTemp1": 95.62818310718433
},
"properties": {
"Status": "Active"
},
"systemProperties": {
"iothub-content-type": "application/json",
"iothub-content-encoding": "utf-8",
"iothub-connection-device-id": "d1",
"iothub-connection-auth-method": "{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
"iothub-connection-auth-generation-id": "123455432199234570",
"iothub-enqueuedtime": "2019-01-07T20:58:30.48Z",
"iothub-message-source": "Telemetry"
}
},
"dataVersion": "",
"metadataVersion": "1"
}]
Summary comment to answer to help others who have same problem.
The body you provided is Base64 encoded, you can decode it with Convert.FromBase64String(String) Method.
byte[] newBytes = Convert.FromBase64String(body);
For more details, you could refer to this issue.
Update:
Add the following code in my application will solve the problem.
message.ContentEncoding = "utf-8";
message.ContentType = "application/json";

Displaying Message Body in Push Notification

I'm getting follow message when I receive Push Notification and the same JSON is displayed to user too. I want only the Data to be displayed in the body when user receives the push notification rather than displaying the whole JSON.
under aps["alert"] I get this
{
\"Type\":\"TestType\",
\"NotificationId\":\"40\",
\"ImageUrl\":\"\",
\"UserId\":1,
\"Data\":\"Testing Push Notification\"
}
How to display Data in the message body of Push Notification.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
if(application.applicationState == UIApplicationState.Active) {
if let alertInfo = userInfo["aps"]?["alert"] as? Dictionary<String,String>{
if let title = alertInfo["Data"]{
}
}
}
}
please try to send push notification flowing format and if application staying in background then automatic showing body message :
[aps: {
alert = {
body = "Testing Push Notification";
title = "New Message";
NotificationId= "536"
};
sound = default;
}]
Update correct format For example
{
"aps": {
"alert": "Hello World",
"sound": "default"
},
"Person": {
"Address": "this is a test address",
"Name": "First Name",
"Number": "023232323233"
}
}
iOS Push Notification custom format
The keys in aps are pre-defined by apple. But you can send your custom
keys outside aps as shown below
{
"aps": {
"alert": {
"title": "Hey!🙂 Checkout my custom notification",
"subtitle": "Custom notification subtitle",
"body": "Description of custom notification"
},
"sound": "default",
"category": "CustomPush",
"badge": 1,
"mutable-content": 1
},
"Employee": {
"Name": "John Doe",
"Designation": "Manager"
}
}
Where Employee is custom key where you can set your own data
as required

FCM Notification in iOS doesn't play sound when received

I am using Firebase push notifications in my iOS Application. Although I am able to send the notification by sending below payload, it doesn't play a sound when received.
{
"to": "myToken",
"notification": {
"body": "test",
"title": "test"
},
"priority": "high"
"sound": "default"
}
If I send the test message from console, it works well and plays notification sound. Note:
My Authorization code is correct
I am sending http request to https://fcm.googleapis.com/fcm/send
I have tested it on IPhone 4 , IPhone 6 and IPhone 6S, All recieve
notifications without sound
your JSON "sound" : "default" should be inside the "notification" key not in the root of the JSON. This JSON should work.
{
"to": "myToken",
"notification": {
"body": "test",
"title": "test",
"sound": "default"
},
"priority": "high"
}
When using the FCM admin SDK, you have to specify sounds separately for Android and Apple devices:
let message = {
notification: {
'body': 'This is the message the user sees',
},
data: {
'param1': 'specify some extra data here',
},
// Apple specific settings
apns: {
headers: {
'apns-priority': '10',
},
payload: {
aps: {
sound: 'default',
}
},
},
android: {
priority: 'high',
notification: {
sound: 'default',
}
},
token: 'target FCM token goes here',
};
(Note: I've only tested the Apple settings thus far)
payload = {
notification:{
title: 'SOLO has been changed by an administrator',
body: 'Administrator changed your SOLO schedule',
},
android: {
},
apns: {
headers:{
"apns-collapse-id": "solo_changed_administrator",
"content-available": "1",
"apns-priority": "10",
},
payload:{
aps:{
sound: 'default',
badge: 12213123223
}
}
},
data:{
type: 'type'
}
}
https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?authuser=0#ApnsConfig
I had the same problem. When notifications come through FCM to iOS not working sounds or vibrations. Here I followed this link: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW1
And finally went successful. I created my apple notification as an alert in my payload and it works for me. Here is my JSON below to get some idea about my solution.
"apns": {
"payload": {
"aps" : {
"alert" : {
"body": "Notification body",
"title": "Notification title"
},
"badge" : 2,
"sound" : "default"
}
}
}
Note: Please put this "apns" key in the relevant place of your message request. I was using REST call for requesting message.
Please refer the following link to get a good idea of how to send a notification message with platform-specific delivery options.
Link: https://firebase.google.com/docs/cloud-messaging/concept-options#example-notification-message-with-platform-specific-delivery-options

Apple's push notification server version 2 dropping a message that seems valid

Apple's push notification server version 2 drops this message:
{
"aps": {
"badge": 1,
"alert": {
"loc-key": "New message from %#",
"loc-args": ["joe"]
},
"sound": "default"
},
"arg": "value"
}
But it delivers this message:
{
"aps": {
"badge": 1,
"alert": {
"loc-key": "New message from",
"loc-args": ["joe"]
},
"sound": "default"
},
"arg": "value"
}
Not only is the message not delivered but no http2 response is received and the connection is not disconnected.
I'm testing with apple's development service.
The app doesn't have a Localizable.strings file for the language (english).
How do I fix this?
It seems that as soon as a 'loc-key' contains a '%#' it can't be localized.
Moving the %# from the key into a value in Localization.strings works.

Resources