Missing topic name when "content_available" is set - ios

we're trying to integrate GCM messaging on our iOS app. We use topic messaging to send messages to a group of devices subscribed to the same topic.
During the developemnt we noticed that enabling the "content_available" flag to let the app receive notifications while in background the message we receive is missing the topic name, so if the app is subscribed to multiple topics we're not able to determine which topic was the sender of the message.
Notification sent using HTTP api:
{
"to":"/topics/user_5",
"data":{"action":"sync","what":"user"},
"collapse_key":"866c9b6f36461511697a5089fe4b0d8e",
"delay_while_idle":true
}
Notification received on iOS:
{
action = sync;
"collapse_key" = 866c9b6f36461511697a5089fe4b0d8e;
from = "/topics/user_5";
what = user;
}
Notification sent using HTTP api, with "content_available" flag set to true:
{
"to":"/topics/user_5",
"data":{"action":"sync","what":"user"},
"collapse_key":"866c9b6f36461511697a5089fe4b0d8e",
"content_available":true,
"delay_while_idle":true
}
Notification received on iOS:
{
action = sync;
aps = {
"content-available" = 1;
};
"gcm.message_id" = "0:1436954611368845%1f9e30f91f9e30f9";
what = user;
}
Is it the expected behaviour or is it a GCM bug? Is there someone else struggling with the same thing?

Related

how to send silent notification / background notification using Firebase Cloud Messaging?

I need to make silent notification/background notification in my iOS app using Firebase Cloud Messaging, so I can make an update in the app even though the user doesn't tap the push notification.
the documentation from Apple about background notification is in here , it is said that
To send a background notification, create a remote notification with
an aps dictionary that includes only the content-available key in the payload
Sample payload for a background notification from that documentation is like this:
{
"aps" : {
"content-available" : 1
},
"acme1" : "bar",
"acme2" : 42
}
so I create my own payload when sending FCM using cloud function node JS. my code is like this
const userToken = device.token
const payload = {
data: {
notificationID : notificationID,
creatorID : moderatorID,
creatorName: creatorName,
title : title,
body : body,
createdAt : now,
imagePath : imagePath,
type: type
},
apps : {
"content-available" : 1 // to force background data update in iOS
}
}
await admin.messaging().sendToDevice(userToken,payload)
I try to send and I have error:
'Messaging payload contains an invalid "apps" property. Valid
properties are "data" and "notification".
so adding "apps" property is not allowed, but iOS documentation said that I need to add "content-available" in the payload.
I read other answer in here, it is said that the payload should be written like this
{
"to" : "[token]",
"content_available": true,
"priority": "high",
"data" : {
"key1" : "abc",
"key2" : abc
}
but I am confused how to write the payload FCM that can trigger background notification in iOS
Based on the error message that you are receiving, you should remove the apps property since data and notification properties are considered to be valid, as per the documentation.
Now, in regards to the the payload that you found elsewhere, this refers to the HTTP syntax used to pass messages from your app server to client apps via Firebase Cloud Messaging using the FCM legacy HTTP API. You can refer to the the documentation to learn more about the new HTTP v1 API.
To answer your question, when you are using a Cloud Function with Node.js runtime to send notifications using the sendToDevice(token, payload, options) method, you will need to pass the contentAvailable in the function's options parameter.
The contentAvailable option does the following: When a notification or message is sent and this is set to true, an inactive client app is awoken, and the message is sent through APNs as a silent notification and not through the FCM connection server.
Therefore, your Cloud Function might look something like this:
const userToken = [YOUR_TOKEN];
const payload = {
"data": {
"story_id": "story_12345"
},
"notification": {
"title": "Breaking News"
}
};
const options = {
"contentAvailable": true
};
admin.messaging().sendToDevice(userToken, payload, options).then(function(response) {
console.log('Successfully sent message:', response);
}).catch(function(error) {
console.log('Error sending message:', error);
});

How to prepare APN for production

Im trying to deploy my app with notifications but it's giving me the biggest headache in the world. All other questions ive seen with regards to this seem outdated.
I set up APNs to be sent from a nodeJS script that I have running. When running in my sandbox everything was working well. As soon as I sent my app to TestFlight, notifications stopped sending. My script is still Successfully sending to the Notification Id registered with my phone but im assuming its not the correct production Id. If anyone canhelp get me sending production notifications it would be greatly appreciated! Thank you
APN Server code
var options = {
token: {
key: "AuthKey_6V27D43P5R.p8",
keyId: "3Z6SEF7GE5",
teamId: "ASQJ3L7765"
},
production: true
};
var apnProvider = new apn.Provider(options);
function SendIOSNotification(token, message, sound, payload, badge){
var deviceToken = token; //phone notification id
var notification = new apn.Notification(); //prepare notif
notification.topic = 'com.GL.Greek-Life'; // Specify your iOS app's Bundle ID (accessible within the project editor)
notification.expiry = Math.floor(Date.now() / 1000) + 3600; // Set expiration to 1 hour from now (in case device is offline)
notification.badge = badge; //selected badge
notification.sound = sound; //sound is configurable
notification.alert = message; //supports emoticon codes
notification.payload = {id: payload}; // Send any extra payload data with the notification which will be accessible to your app in didReceiveRemoteNotification
apnProvider.send(notification, deviceToken).then(function(result) { //send actual notifcation
// Check the result for any failed devices
var subToken = token.substring(0, 6);
console.log("Succesfully sent message to ", subToken);
}).catch( function (error) {
console.log("Faled to send message to ", subToken);
})
}

Sending Custom Payload using One Signal

THe client had asked to integrate OneSignal with their Android and iOS App. Earlier, they were using OpenBack for push notification. The payload which is being received in iOS and Android app in in the following format:
{ "aps": { "alert": { "loc-key": "WATCH_VIDEO", "loc-args": ["Mo Adham", "Simpson's theme on Two guitars"]}, "guid": "1GSIP6J" } }
When I look up in OneSignal dashboard, they have no option to create custom payload. I want the payload in above format from OneSignal.
Currently from dashboard, the OneSignal sends payload in this format:
{
aps = {
alert = {
body = hello;
subtitle = test;
title = test;
};
sound = default;
};
custom = {
i = "db7e56d9-df72-4ec3-adbe-1cd8e1c5d327";
};
}
The keys don't match in the both payloads.
What I want to ask is does the client needs to integrate OneSignal in their backend so that they can send the payload in the specified format?
Check out their documentation on rich notifications, action buttons and customizing action buttons. Links mentioned below. If it still doesn't help, chat with their customer support.
https://onesignal.com/blog/sending-rich-notifications-in-ios10-with-onesignal/
https://documentation.onesignal.com/docs/action-buttons
https://documentation.onesignal.com/docs/customize-action-buttons
Try using Create notification
I tested in postman
POST - https://onesignal.com/api/v1/notifications
Add this in header
Authorization: Basic FETVGER2dsaftOWQ3MS00NmQ2LTlmYmYtODkz34dasgYzkx
(Authorization: Basic Your REST API Key)
{
"app_id": "d7c310b5-f2b6-4e84-8f91-c3e4b77c2904",
"included_segments": ["Subscribed Users"], //To send all user
"include_player_ids": ["6392d91a-b206-4b7b-a620-cd68e32c3a76","76ece62b-bcfe-468c-8a78-839aeaa8c5fa","8e0f21fa-9a5a-4ae7-a9a6-ca1f24294b86"], //To specific user using player id
"content_available": true
}
REST API Key - find here

Send push notification to iOS device via Firebase (FCM) using HTTP Request after app is killed [duplicate]

This question already has answers here:
Can't send push notifications using the server API
(4 answers)
Closed 6 years ago.
I am having trouble sending push notifications via Firebase through HTTP Request to my iOS device after the app is killed. When the app is in the foreground or active in the background everything work as expected. But if I kill the app it won't work. I am able to send notifications to my app through the Firebase console if the app is killed, so I believe something must be wrong with the code I am using.
This is my code for sending the push notification:
private void SendPushNotification(string devicetoken, string header, string content, string pushdescription)
{
var textNotification = new
{
to = devicetoken,
notification = new
{
title = header,
text = content,
content_available = true,
sound = "enabled",
priority = "high",
id = pushdescription,
},
project_id = "rrp-mobile",
};
var senderId = "212579566459";
var notificationJson = Newtonsoft.Json.JsonConvert.SerializeObject(textNotification);
using (var client = new WebClient())
{
client.Encoding = Encoding.UTF8;
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers[HttpRequestHeader.Authorization] = "key=AIfrSyAtgsWCMH4s_bOyj-Us4CrdsifHv-GqElg";
client.Headers["Sender"] = $"id={senderId}";
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.UploadString("https://fcm.googleapis.com/fcm/send", "POST", notificationJson);
}
}
Am I forgetting something here? This works for sending push notifications to Android devices both in foreground, background and when app is killed, and like I said also to iOS devices in foreground and background.
The only issue is sending push notifications to iOS devices when the app has been killed. Does anyone have any idea as to how I would solve this issue?
I just realized my mistake, and it was very simple. I am posting this here because I believe this may be an easy thing to miss.
var textNotification = new
{
to = devicetoken,
notification = new
{
title = header,
text = content,
content_available = true,
sound = "enabled",
**priority = "high",**
id = pushdescription,
},
project_id = "rrp-mobile",
};
You need to make sure that the priority property is defined outside the "notification" scope, like this:
var textNotification = new
{
to = devicetoken,
**priority = "high",**
notification = new
{
title = header,
text = content,
content_available = true,
sound = "enabled",
id = pushdescription,
},
project_id = "rrp-mobile",
};
This will make your push notifications deliver even if the app is killed.

Firebase FCM IOS don´t send Apple apns payload

I have 3 apps, 2 are working well and are receiving pushs from FCM, but one of them does not receive pushs.
I don´t know why, but my other apps receive a Push Notification like this:
{
aps = {
alert = {
body = "body here";
title = "title here;
};
badge = 3;
"content-available" = 1;
};
"gcm.message_id" = "0:1468862214187339%9c127a139c127a13";
}
But in my other app, I receive messages like this (without aps body):
{
"collapse_key" = "...";
from = 1003897668292;
notification = {
body = "body here";
e = 1;
title = "title here";
};
}
Well, so I have some questions?
I did the same process in both applications.
I use the default swizzling
I created the p12 certificate to send Push Messages and I did upload it to the Firebase Console.
Does anyone know any reason for Firebase send messages with a different format? On the server I'm sending messages in the same way.
I would like to receive messages with the "aps/alert/body" format.
kind regards

Resources