This article mainly records the format of APNs, FCM message push and some problems encountered when the back-end service integrates message push. The environment used is Node.js + AWS SNS.
AWS SNS is an AWS message management service. In this article, we mainly use it to create multi-platform message push. We can send messages to multiple platforms via SNS, such as APNs (iOS), FCM (Android), etc. The message push in the article can be achieved simply by the following Node.js code (Typescript).
import {
SNS } from 'aws-sdk';
const sns = new SNS({
accessKeyId: '***',
secretAccessKey: '***',
region: '***',
});
const params = {
// 发送消息提醒到topic则需要填写此项,本文中主要是发送到topic
TopicArn: '',
// 发送消息提醒到指定终端则填写此项而非TopicArn
// TargetArn: '',
// 消息推送内容的类型,值为json
MessageStructure: 'json'
// 消息提醒的内容
Message: JSON.stringify({
// 若发送至topic,必须包含default,其代表发送至未被指定的平台的内容
default: '默认消息',
// 为指定平台发送数据时,必须为JSON键值对字符串
APNS: JSON.stringify({
aps: {
alert: {
title: '你好,iOS',
body: '这是提醒内容',
},
},
data: {
customId: '123',
}
}),
GCM: JSON.stringify({
notification: {
title: '你好,Android',
body: '这是提醒内容',
},
data: {
customId: '123',
}
}),
}),
}
sns.publish(params);
After the message is sent, the iOS terminal in the production environment that has subscribed to the topic will receive '你好,iOS'
a message reminder titled , the Android terminal will receive '你好,Android'
the message reminder, and other terminals (such as the iOS development environment or browser) will receive it '默认消息'
.
APNs data format (iOS)
APNs (Apple Push Notification service) is a service that pushes notifications for Apple terminal devices. First, let's look at an example of the content pushed by APNs:
{
"aps": {
"alert": {
"title": "",
"body": "",
}
},
// other custom data...
"data": {
"customId": ""
}
}
Among them, it aps
means some push settings defined by Apple. For the value, please refer aps
to the description of some key-value pairs .
In addition aps
, we can also add some custom content, as examples customId
.
aps
Field description
name | Types of | Description |
---|---|---|
alert |
Dictionary or string | The content displayed by the push banner, when it is a string, directly display the value of the string, and when it is a dictionary, please refer to the following table |
badge |
Numerical value | The number displayed by the app tag (badge), if it is 0, the tag will not be displayed |
sound |
String or dictionary | Sound reminder when accepting push, the default is default , for more push sounds, please refer to UNNotificationSound |
thread-id |
String | Used for grouping different types of message reminders |
category |
String | For the type of message reminder, please refer to Declaring Your Actionable Notification Types |
content-available |
Numerical value | Its value is 1 and does not include alert , badge and sound promptly indicates that the message reminder is a background reminder |
mutable-content |
Numerical value | When its value is 1, the message push needs to be processed by the app first, please refer to Modifying Content in Newly Delivered Notifications |
target-content-id |
String | Identifier of the open window |
For more details, aps
please refer to Payload Key Reference Table 1 .
alert
Description of some commonly used fields
name | Types of | Description |
---|---|---|
title |
String | The main title of the message reminder |
subtitle |
String | The subtitle of the message reminder, used to indicate additional information explaining the purpose of the push |
body |
String | The main content of the message reminder |
launch-image |
String | Indicates the image displayed when the user opens the app by clicking the message reminder |
In addition, alert
some fields are defined loc-key
, loc-args
etc. related to localization. If you need to use it, please refer to the document. For more details, alert
please refer to Payload Key Reference Table 2 .
FCM data format
FCM (Firebase Cloud Messaging) is a cross-platform messaging solution. Since 2018, Google has replaced GCM (Google Cloud Messaging) with FCM. Similarly, let's take a look at an example of FCM's push content:
{
"notification": {
"title": "",
"body": "",
},
"data": {
"customId": ""
//
}
}
Among them, notification
some are predefined fields used by the system, and the included fields can refer to notification
some common fields ; data
some are user-defined fields, as in the example {"customId": ""}
.
notification
Some common fields
name | Types of | Description |
---|---|---|
title |
String | Message reminder title |
body |
String | The main content of the message reminder |
icon |
String | The icon of the message reminder, the default is the app icon defined in the manifest |
sound |
String | The sound played by the message reminder, the default isdefault |
notification_count |
Numerical value | Set the label number of the message reminder |
visibility |
Boolean value | Whether to display a message reminder |
image |
String | Picture displayed in the message reminder |
For more FCM fields, please refer to AndroidNotification-Firebase .
Unable to obtain data
Correctly distinguish between development environment and production environment
Some platforms need to differentiate between different environments for push pushes, such as iOS push APNs, which need to be differentiated APNS
as well APNS_SANDBOX
. When using AWS SNS to create a Topic message, if the wrong environment is used, the data received by the terminal may be default
medium information. For example, in our test code above to send a message to the development environment, the data received by the terminal may be:
{
"aps": {
"alert": "默认消息"
}
}
Use SNS to convert the payload to a string
When using SNS to create a message push, each payload data must be converted into a string form first, otherwise the correct push content may not be received, as in the sample code APNS: JSON.stringify({})
.