问题
Is there a way to send a silent APNS using google's firebase? It seems that if the app is in the background it will always show a notification to the user.
Thanks?
回答1:
You can send silent APNS messages using the FCM server API https://firebase.google.com/docs/cloud-messaging/http-server-ref
In particular you need to use:
- The data field:
This parameter specifies the custom key-value pairs of the message's payload.
For example, with data:{"score":"3x1"}:
On iOS, if the message is sent via APNS, it represents the custom data fields. If it is sent via FCM connection server, it would be represented as key value dictionary in AppDelegate application:didReceiveRemoteNotification:.
The key should not be a reserved word ("from" or any word starting with "google" or "gcm"). Do not use any of the words defined in this table (such as collapse_key).
Values in string types are recommended. You have to convert values in objects or other non-string data types (e.g., integers or booleans) to string
- The content-available field:
On iOS, use this field to represent content-available in the APNS payload. When a notification or message is sent and this is set to true, an inactive client app is awoken. On Android, data messages wake the app by default. On Chrome, currently not supported.
Full documentation: https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json
回答2:
For a truly silent notification (both foreground and background) using FCM server use these fields:
"to" : "[token]",
"content_available": true,
"priority": "high",
"data" : {
"key1" : "abc",
"key2" : 123
}
NOTE: Make sure you are using "content_available" NOT "content-available" with FCM. It's converted for APNS and won't be received properly otherwise. The difference had tripped me up for some time.
回答3:
I explain this topic more detail on my blog. http://blog.boxstory.com/2017/01/how-to-send-silent-push-notification-in.html
** keypoint is : "content_available:true"
this is sample JSON
{
"to" : "<device>",
"priority": "normal",
"content_available": true, <-- this key is converted to 'content-available:1'
"notification" : {
"body" : "noti body",
"title" : "noti title",
"link": "noti link "
}
}
Note: If the above sample JSON is sent, then the notification will be visible to the user. Use below If you don't want the user to see the push notification.
{
"to": "<device>",
"priority": "normal",
"content_available": true <-- this key is converted to 'content-available:1'
}
回答4:
For guys who do not use the Legacy HTTP
as is shown in other answers and use the latest v1 HTTP protocol
, I've finally figured out the right way to send silent notifications.
NodeJS example using firebase-admin
:
const message = {
apns: {
payload: {
aps: {
"content-available": 1,
alert: ""
}
}
}
};
admin
.messaging()
.send(message)
.then(response => {
// Response is a message ID string.
console.log("Successfully sent message:", response);
})
.catch(error => {
console.log("Error sending message:", error);
});
Explanation:
- It seems the payload in
apns
does not get converted by Firebase inv1 HTTP protocol
so you need the original"content-available": 1
for that. alert: ""
is also necessary. If you ever try to send silent notifications using something likePusher
, you will find onlycontent-available
cannot trigger it. Instead, adding additional field such assound
oralert
can make it work. See Silent Push Notification in iOS 7 does not work. Since Firebase forbids empty sound, we can use empty alert for this.
来源:https://stackoverflow.com/questions/37570200/firebase-silent-apns-notification