เมื่อใช้โปรโตคอลเซิร์ฟเวอร์ของแอป Firebase Admin SDK หรือ FCM คุณจะสร้างคำขอข้อความและส่งไปยังเป้าหมายประเภทเหล่านี้ได้
- ชื่อหัวข้อ
- เงื่อนไข
- โทเค็นการลงทะเบียนอุปกรณ์
- ชื่อกลุ่มอุปกรณ์ (โปรโตคอลเดิมและ Firebase Admin SDK สำหรับ Node.js เท่านั้น)
คุณสามารถส่งข้อความพร้อมเพย์โหลดการแจ้งเตือนซึ่งประกอบด้วยฟิลด์ที่กำหนดไว้ล่วงหน้าเพย์โหลดข้อมูลของฟิลด์ที่ผู้ใช้กำหนดเองหรือข้อความที่มีเพย์โหลดทั้งสองประเภท ดู ประเภทข้อความ สำหรับข้อมูลเพิ่มเติม
ตัวอย่างในหน้านี้แสดงวิธีส่งข้อความแจ้งเตือนโดยใช้ Firebase Admin SDK (ซึ่งรองรับ Node , Java , Python , C # และ Go ) และ โปรโตคอล HTTP v1 นอกจากนี้ยังมีคำแนะนำในการส่งข้อความผ่าน โปรโตคอล HTTP และ XMPP แบบ เดิม
ส่งข้อความไปยังอุปกรณ์เฉพาะ
ในการส่งไปยังอุปกรณ์เดียวโดยเฉพาะให้ส่งโทเค็นการลงทะเบียนของอุปกรณ์ดังที่แสดง ดูข้อมูลการตั้งค่าไคลเอนต์สำหรับแพลตฟอร์มของคุณเพื่อเรียนรู้เพิ่มเติมเกี่ยวกับโทเค็นการลงทะเบียน
โหนด js
// This registration token comes from the client FCM SDKs.
var registrationToken = 'YOUR_REGISTRATION_TOKEN';
var message = {
data: {
score: '850',
time: '2:45'
},
token: registrationToken
};
// Send a message to the device corresponding to the provided
// registration token.
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);
});
Java
// This registration token comes from the client FCM SDKs.
String registrationToken = "YOUR_REGISTRATION_TOKEN";
// See documentation on defining a message payload.
Message message = Message.builder()
.putData("score", "850")
.putData("time", "2:45")
.setToken(registrationToken)
.build();
// Send a message to the device corresponding to the provided
// registration token.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
Python
# This registration token comes from the client FCM SDKs.
registration_token = 'YOUR_REGISTRATION_TOKEN'
# See documentation on defining a message payload.
message = messaging.Message(
data={
'score': '850',
'time': '2:45',
},
token=registration_token,
)
# Send a message to the device corresponding to the provided
# registration token.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)
ไป
// Obtain a messaging.Client from the App.
ctx := context.Background()
client, err := app.Messaging(ctx)
if err != nil {
log.Fatalf("error getting Messaging client: %v\n", err)
}
// This registration token comes from the client FCM SDKs.
registrationToken := "YOUR_REGISTRATION_TOKEN"
// See documentation on defining a message payload.
message := &messaging.Message{
Data: map[string]string{
"score": "850",
"time": "2:45",
},
Token: registrationToken,
}
// Send a message to the device corresponding to the provided
// registration token.
response, err := client.Send(ctx, message)
if err != nil {
log.Fatalln(err)
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)
ค#
// This registration token comes from the client FCM SDKs.
var registrationToken = "YOUR_REGISTRATION_TOKEN";
// See documentation on defining a message payload.
var message = new Message()
{
Data = new Dictionary<string, string>()
{
{ "score", "850" },
{ "time", "2:45" },
},
Token = registrationToken,
};
// Send a message to the device corresponding to the provided
// registration token.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);
ส่วนที่เหลือ
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"body":"This is an FCM notification message!",
"title":"FCM Message"
}
}
}
คำสั่ง cURL:
curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
"message":{
"notification":{
"title":"FCM Message",
"body":"This is an FCM Message"
},
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send
เมื่อประสบความสำเร็จแต่ละวิธีการส่งจะส่งคืนรหัสข้อความ Firebase Admin SDK ส่งคืนสตริง ID ในรูปแบบ projects/{project_id}/messages/{message_id}
การตอบสนองโปรโตคอล HTTP เป็นคีย์ JSON เดียว:
{
"name":"projects/myproject-b5ae1/messages/0:1500415314455276%31bd1c9631bd1c96"
}
ส่งข้อความไปยังอุปกรณ์หลายเครื่อง
REST API และ Admin FCM API ช่วยให้คุณสามารถส่งข้อความแบบหลายผู้รับไปยังรายการโทเค็นการลงทะเบียนอุปกรณ์ คุณสามารถระบุโทเค็นการลงทะเบียนอุปกรณ์ได้ถึง 500 โทเค็นต่อการร้องขอ
โหนด js
// Create a list containing up to 500 registration tokens.
// These registration tokens come from the client FCM SDKs.
const registrationTokens = [
'YOUR_REGISTRATION_TOKEN_1',
// …
'YOUR_REGISTRATION_TOKEN_N',
];
const message = {
data: {score: '850', time: '2:45'},
tokens: registrationTokens,
}
admin.messaging().sendMulticast(message)
.then((response) => {
console.log(response.successCount + ' messages were sent successfully');
Java
// Create a list containing up to 500 registration tokens.
// These registration tokens come from the client FCM SDKs.
List<String> registrationTokens = Arrays.asList(
"YOUR_REGISTRATION_TOKEN_1",
// ...
"YOUR_REGISTRATION_TOKEN_n"
);
MulticastMessage message = MulticastMessage.builder()
.putData("score", "850")
.putData("time", "2:45")
.addAllTokens(registrationTokens)
.build();
BatchResponse response = FirebaseMessaging.getInstance().sendMulticast(message);
// See the BatchResponse reference documentation
// for the contents of response.
System.out.println(response.getSuccessCount() + " messages were sent successfully");
Python
# Create a list containing up to 500 registration tokens.
# These registration tokens come from the client FCM SDKs.
registration_tokens = [
'YOUR_REGISTRATION_TOKEN_1',
# ...
'YOUR_REGISTRATION_TOKEN_N',
]
message = messaging.MulticastMessage(
data={'score': '850', 'time': '2:45'},
tokens=registration_tokens,
)
response = messaging.send_multicast(message)
# See the BatchResponse reference documentation
# for the contents of response.
print('{0} messages were sent successfully'.format(response.success_count))
ไป
// Create a list containing up to 100 registration tokens.
// This registration tokens come from the client FCM SDKs.
registrationTokens := []string{
"YOUR_REGISTRATION_TOKEN_1",
// ...
"YOUR_REGISTRATION_TOKEN_n",
}
message := &messaging.MulticastMessage{
Data: map[string]string{
"score": "850",
"time": "2:45",
},
Tokens: registrationTokens,
}
br, err := client.SendMulticast(context.Background(), message)
if err != nil {
log.Fatalln(err)
}
// See the BatchResponse reference documentation
// for the contents of response.
fmt.Printf("%d messages were sent successfully\n", br.SuccessCount)
ค#
// Create a list containing up to 500 registration tokens.
// These registration tokens come from the client FCM SDKs.
var registrationTokens = new List<string>()
{
"YOUR_REGISTRATION_TOKEN_1",
// ...
"YOUR_REGISTRATION_TOKEN_n",
};
var message = new MulticastMessage()
{
Tokens = registrationTokens,
Data = new Dictionary<string, string>()
{
{ "score", "850" },
{ "time", "2:45" },
},
};
var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(message);
// See the BatchResponse reference documentation
// for the contents of response.
Console.WriteLine($"{response.SuccessCount} messages were sent successfully");
ส่วนที่เหลือ
สร้างคำขอแบตช์ HTTP:
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
POST /v1/projects/myproject-b5ae1/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"FCM Message",
"body":"This is an FCM notification message!"
}
}
}
...
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
POST /v1/projects/myproject-b5ae1/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"token":"cR1rjyj4_Kc:APA91bGusqbypSuMdsh7jSNrW4nzsM...",
"notification":{
"title":"FCM Message",
"body":"This is an FCM notification message!"
}
}
}
--subrequest_boundary--
บันทึกคำขอลงในไฟล์ (ในตัวอย่างนี้ batch_request.txt) จากนั้นใช้คำสั่ง cURL:
curl --data-binary @batch_request.txt -H 'Content-Type: multipart/mixed; boundary="subrequest_boundary"' https://fcm.googleapis.com/batch
สำหรับ Firebase Admin SDK การดำเนินการนี้จะใช้ sendAll()
API ภายใต้ประทุนดังที่แสดงในตัวอย่าง ค่าที่ส่งคืนคือ BatchResponse
ซึ่งรายการการตอบกลับสอดคล้องกับลำดับของโทเค็นอินพุต สิ่งนี้มีประโยชน์เมื่อคุณต้องการตรวจสอบว่าโทเค็นใดทำให้เกิดข้อผิดพลาด
โหนด js
// These registration tokens come from the client FCM SDKs.
const registrationTokens = [
'YOUR_REGISTRATION_TOKEN_1',
// …
'YOUR_REGISTRATION_TOKEN_N',
];
const message = {
data: {score: '850', time: '2:45'},
tokens: registrationTokens,
}
admin.messaging().sendMulticast(message)
.then((response) => {
if (response.failureCount > 0) {
const failedTokens = [];
response.responses.forEach((resp, idx) => {
if (!resp.success) {
failedTokens.push(registrationTokens[idx]);
}
});
console.log('List of tokens that caused failures: ' + failedTokens);
}
});
Java
// These registration tokens come from the client FCM SDKs.
List<String> registrationTokens = Arrays.asList(
"YOUR_REGISTRATION_TOKEN_1",
// ...
"YOUR_REGISTRATION_TOKEN_n"
);
MulticastMessage message = MulticastMessage.builder()
.putData("score", "850")
.putData("time", "2:45")
.addAllTokens(registrationTokens)
.build();
BatchResponse response = FirebaseMessaging.getInstance().sendMulticast(message);
if (response.getFailureCount() > 0) {
List<SendResponse> responses = response.getResponses();
List<String> failedTokens = new ArrayList<>();
for (int i = 0; i < responses.size(); i++) {
if (!responses.get(i).isSuccessful()) {
// The order of responses corresponds to the order of the registration tokens.
failedTokens.add(registrationTokens.get(i));
}
}
System.out.println("List of tokens that caused failures: " + failedTokens);
}
Python
# These registration tokens come from the client FCM SDKs.
registration_tokens = [
'YOUR_REGISTRATION_TOKEN_1',
# ...
'YOUR_REGISTRATION_TOKEN_N',
]
message = messaging.MulticastMessage(
data={'score': '850', 'time': '2:45'},
tokens=registration_tokens,
)
response = messaging.send_multicast(message)
if response.failure_count > 0:
responses = response.responses
failed_tokens = []
for idx, resp in enumerate(responses):
if not resp.success:
# The order of responses corresponds to the order of the registration tokens.
failed_tokens.append(registration_tokens[idx])
print('List of tokens that caused failures: {0}'.format(failed_tokens))
ไป
// Create a list containing up to 100 registration tokens.
// This registration tokens come from the client FCM SDKs.
registrationTokens := []string{
"YOUR_REGISTRATION_TOKEN_1",
// ...
"YOUR_REGISTRATION_TOKEN_n",
}
message := &messaging.MulticastMessage{
Data: map[string]string{
"score": "850",
"time": "2:45",
},
Tokens: registrationTokens,
}
br, err := client.SendMulticast(context.Background(), message)
if err != nil {
log.Fatalln(err)
}
if br.FailureCount > 0 {
var failedTokens []string
for idx, resp := range br.Responses {
if !resp.Success {
// The order of responses corresponds to the order of the registration tokens.
failedTokens = append(failedTokens, registrationTokens[idx])
}
}
fmt.Printf("List of tokens that caused failures: %v\n", failedTokens)
}
ค#
// These registration tokens come from the client FCM SDKs.
var registrationTokens = new List<string>()
{
"YOUR_REGISTRATION_TOKEN_1",
// ...
"YOUR_REGISTRATION_TOKEN_n",
};
var message = new MulticastMessage()
{
Tokens = registrationTokens,
Data = new Dictionary<string, string>()
{
{ "score", "850" },
{ "time", "2:45" },
},
};
var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(message);
if (response.FailureCount > 0)
{
var failedTokens = new List<string>();
for (var i = 0; i < response.Responses.Count; i++)
{
if (!response.Responses[i].IsSuccess)
{
// The order of responses corresponds to the order of the registration tokens.
failedTokens.Add(registrationTokens[i]);
}
}
Console.WriteLine($"List of tokens that caused failures: {failedTokens}");
}
ส่วนที่เหลือ
การส่งย่อยการส่งแต่ละครั้งจะส่งคืนการตอบกลับ การตอบกลับถูกคั่นด้วยสตริงขอบเขตการตอบสนองที่เริ่มต้นด้วย --batch_
--batch_nDhMX4IzFTDLsCJ3kHH7v_44ua-aJT6q
Content-Type: application/http
Content-ID: response-
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Vary: Origin
Vary: X-Origin
Vary: Referer
{
"name": "projects/35006771263/messages/0:1570471792141125%43c11b7043c11b70"
}
...
--batch_nDhMX4IzFTDLsCJ3kHH7v_44ua-aJT6q
Content-Type: application/http
Content-ID: response-
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Vary: Origin
Vary: X-Origin
Vary: Referer
{
"name": "projects/35006771263/messages/0:1570471792141696%43c11b7043c11b70"
}
--batch_nDhMX4IzFTDLsCJ3kHH7v_44ua-aJT6q--
ส่งข้อความไปยังหัวข้อ
หลังจากที่คุณสร้างหัวข้อแล้วไม่ว่าจะสมัครใช้งานอินสแตนซ์แอปไคลเอ็นต์กับหัวข้อทางฝั่งไคลเอ็นต์หรือผ่าน เซิร์ฟเวอร์ API คุณสามารถส่งข้อความไปยังหัวข้อได้ หากนี่เป็นครั้งแรกที่คุณสร้างคำขอ FCM โปรดดูคำแนะนำเกี่ยวกับ สภาพแวดล้อมเซิร์ฟเวอร์ของคุณและ FCM สำหรับข้อมูลพื้นฐานและข้อมูลการตั้งค่าที่สำคัญ
ในตรรกะการส่งของคุณบนแบ็กเอนด์ระบุชื่อหัวข้อที่ต้องการดังที่แสดง:
โหนด js
// The topic name can be optionally prefixed with "/topics/".
var topic = 'highScores';
var message = {
data: {
score: '850',
time: '2:45'
},
topic: topic
};
// Send a message to devices subscribed to the provided topic.
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);
});
Java
// The topic name can be optionally prefixed with "/topics/".
String topic = "highScores";
// See documentation on defining a message payload.
Message message = Message.builder()
.putData("score", "850")
.putData("time", "2:45")
.setTopic(topic)
.build();
// Send a message to the devices subscribed to the provided topic.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
Python
# The topic name can be optionally prefixed with "/topics/".
topic = 'highScores'
# See documentation on defining a message payload.
message = messaging.Message(
data={
'score': '850',
'time': '2:45',
},
topic=topic,
)
# Send a message to the devices subscribed to the provided topic.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)
ไป
// The topic name can be optionally prefixed with "/topics/".
topic := "highScores"
// See documentation on defining a message payload.
message := &messaging.Message{
Data: map[string]string{
"score": "850",
"time": "2:45",
},
Topic: topic,
}
// Send a message to the devices subscribed to the provided topic.
response, err := client.Send(ctx, message)
if err != nil {
log.Fatalln(err)
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)
ค#
// The topic name can be optionally prefixed with "/topics/".
var topic = "highScores";
// See documentation on defining a message payload.
var message = new Message()
{
Data = new Dictionary<string, string>()
{
{ "score", "850" },
{ "time", "2:45" },
},
Topic = topic,
};
// Send a message to the devices subscribed to the provided topic.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);
ส่วนที่เหลือ
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"topic" : "foo-bar",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message"
}
}
}
คำสั่ง cURL:
curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
"message": {
"topic" : "foo-bar",
"notification": {
"body": "This is a Firebase Cloud Messaging Topic Message!",
"title": "FCM Message"
}
}
}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
เพื่อส่งข้อความไปยังการรวมกันของหัวข้อระบุเป็นเงื่อนไขซึ่งเป็นนิพจน์บูลีนที่ระบุหัวข้อเป้าหมาย ตัวอย่างเช่นเงื่อนไขต่อไปนี้จะส่งข้อความไปยังอุปกรณ์ที่สมัครสมาชิก TopicA
และ TopicB
หรือ TopicC
:
"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"
ก่อนอื่น FCM จะประเมินเงื่อนไขใด ๆ ในวงเล็บจากนั้นจึงประเมินนิพจน์จากซ้ายไปขวา ในนิพจน์ข้างต้นผู้ใช้ที่สมัครเป็นสมาชิกหัวข้อใดหัวข้อหนึ่งจะไม่ได้รับข้อความ ในทำนองเดียวกันผู้ใช้ที่ไม่ได้สมัครสมาชิก TopicA
จะไม่ได้รับข้อความ ชุดค่าผสมเหล่านี้จะได้รับ:
-
TopicA
และTopicB
-
TopicA
และTopicC
คุณสามารถรวมหัวข้อได้สูงสุดห้าหัวข้อในนิพจน์เงื่อนไขของคุณ
เพื่อส่งไปยังเงื่อนไข:
โหนด js
// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
var condition = "'stock-GOOG' in topics || 'industry-tech' in topics";
// See documentation on defining a message payload.
var message = {
notification: {
title: '$FooCorp up 1.43% on the day',
body: '$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
},
condition: condition
};
// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
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);
});
Java
// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
String condition = "'stock-GOOG' in topics || 'industry-tech' in topics";
// See documentation on defining a message payload.
Message message = Message.builder()
.setNotification(Notification.builder()
.setTitle("$GOOG up 1.43% on the day")
.setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")
.build())
.setCondition(condition)
.build();
// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
Python
# Define a condition which will send to devices which are subscribed
# to either the Google stock or the tech industry topics.
condition = "'stock-GOOG' in topics || 'industry-tech' in topics"
# See documentation on defining a message payload.
message = messaging.Message(
notification=messaging.Notification(
title='$GOOG up 1.43% on the day',
body='$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
),
condition=condition,
)
# Send a message to devices subscribed to the combination of topics
# specified by the provided condition.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)
ไป
// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
condition := "'stock-GOOG' in topics || 'industry-tech' in topics"
// See documentation on defining a message payload.
message := &messaging.Message{
Data: map[string]string{
"score": "850",
"time": "2:45",
},
Condition: condition,
}
// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
response, err := client.Send(ctx, message)
if err != nil {
log.Fatalln(err)
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)
ค#
// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
var condition = "'stock-GOOG' in topics || 'industry-tech' in topics";
// See documentation on defining a message payload.
var message = new Message()
{
Notification = new Notification()
{
Title = "$GOOG up 1.43% on the day",
Body = "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.",
},
Condition = condition,
};
// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);
ส่วนที่เหลือ
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"condition": "'dogs' in topics || 'cats' in topics",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message",
}
}
}
คำสั่ง cURL:
curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
"notification": {
"title": "FCM Message",
"body": "This is a Firebase Cloud Messaging Topic Message!",
},
"condition": "'dogs' in topics || 'cats' in topics"
}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
ส่งชุดข้อความ
REST API และ Admin SDK รองรับการส่งข้อความเป็นกลุ่ม คุณสามารถจัดกลุ่มข้อความได้มากถึง 500 ข้อความเป็นชุดเดียวและส่งทั้งหมดในการเรียก API ครั้งเดียวโดยมีการปรับปรุงประสิทธิภาพที่สำคัญมากกว่าการส่งคำขอ HTTP แยกกันสำหรับแต่ละข้อความ
คุณลักษณะนี้สามารถใช้เพื่อสร้างชุดข้อความที่กำหนดเองและส่งไปยังผู้รับที่แตกต่างกันรวมถึงหัวข้อหรือโทเค็นการลงทะเบียนอุปกรณ์เฉพาะ ใช้คุณลักษณะนี้เช่นเมื่อคุณต้องส่งข้อความไปยังผู้ชมที่แตกต่างกันพร้อมกันโดยมีรายละเอียดที่แตกต่างกันเล็กน้อยในเนื้อหาของข้อความ
โหนด js
// Create a list containing up to 500 messages.
const messages = [];
messages.push({
notification: {title: 'Price drop', body: '5% off all electronics'},
token: registrationToken,
});
messages.push({
notification: {title: 'Price drop', body: '2% off all books'},
topic: 'readers-club',
});
admin.messaging().sendAll(messages)
.then((response) => {
console.log(response.successCount + ' messages were sent successfully');
});
Java
// Create a list containing up to 500 messages.
List<Message> messages = Arrays.asList(
Message.builder()
.setNotification(Notification.builder()
.setTitle("Price drop")
.setBody("5% off all electronics")
.build())
.setToken(registrationToken)
.build(),
// ...
Message.builder()
.setNotification(Notification.builder()
.setTitle("Price drop")
.setBody("2% off all books")
.build())
.setTopic("readers-club")
.build()
);
BatchResponse response = FirebaseMessaging.getInstance().sendAll(messages);
// See the BatchResponse reference documentation
// for the contents of response.
System.out.println(response.getSuccessCount() + " messages were sent successfully");
Python
# Create a list containing up to 500 messages.
messages = [
messaging.Message(
notification=messaging.Notification('Price drop', '5% off all electronics'),
token=registration_token,
),
# ...
messaging.Message(
notification=messaging.Notification('Price drop', '2% off all books'),
topic='readers-club',
),
]
response = messaging.send_all(messages)
# See the BatchResponse reference documentation
# for the contents of response.
print('{0} messages were sent successfully'.format(response.success_count))
ไป
// Create a list containing up to 100 messages.
messages := []*messaging.Message{
{
Notification: &messaging.Notification{
Title: "Price drop",
Body: "5% off all electronics",
},
Token: registrationToken,
},
{
Notification: &messaging.Notification{
Title: "Price drop",
Body: "2% off all books",
},
Topic: "readers-club",
},
}
br, err := client.SendAll(context.Background(), messages)
if err != nil {
log.Fatalln(err)
}
// See the BatchResponse reference documentation
// for the contents of response.
fmt.Printf("%d messages were sent successfully\n", br.SuccessCount)
ค#
// Create a list containing up to 500 messages.
var messages = new List<Message>()
{
new Message()
{
Notification = new Notification()
{
Title = "Price drop",
Body = "5% off all electronics",
},
Token = registrationToken,
},
new Message()
{
Notification = new Notification()
{
Title = "Price drop",
Body = "2% off all books",
},
Topic = "readers-club",
},
};
var response = await FirebaseMessaging.DefaultInstance.SendAllAsync(messages);
// See the BatchResponse reference documentation
// for the contents of response.
Console.WriteLine($"{response.SuccessCount} messages were sent successfully");
ส่วนที่เหลือ
สร้างคำขอแบตช์ HTTP โดยการรวมรายการคำขอย่อย:
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
POST /v1/projects/myproject-b5ae1/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"FCM Message",
"body":"This is an FCM notification message to device 0!"
}
}
}
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
POST /v1/projects/myproject-b5ae1/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"topic":"readers-club",
"notification":{
"title":"Price drop",
"body":"2% off all books"
}
}
}
...
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
POST /v1/projects/myproject-b5ae1/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"token":"cR1rjyj4_Kc:APA91bGusqbypSuMdsh7jSNrW4nzsM...",
"notification":{
"title":"FCM Message",
"body":"This is an FCM notification message to device N!"
}
}
}
--subrequest_boundary--
คุณสามารถสอบถาม BatchResponse
ส่งคืนเพื่อตรวจสอบจำนวนข้อความที่ส่งไปยัง FCM ได้สำเร็จ นอกจากนี้ยังแสดงรายการคำตอบที่สามารถใช้เพื่อตรวจสอบสถานะของแต่ละข้อความ ลำดับของการตอบกลับจะสอดคล้องกับลำดับของข้อความในรายการอินพุต
ส่งข้อความที่เปิดใช้งานการบูตโดยตรง (Android เท่านั้น)
คุณสามารถส่งข้อความไปยังอุปกรณ์ในโหมดบูตโดยตรงโดยใช้ HTTP v1 หรือ HTTP API แบบเดิม ก่อนที่จะส่งไปยังอุปกรณ์ในโหมดบูตโดยตรงตรวจสอบให้แน่ใจว่าคุณได้ทำตามขั้นตอนเพื่อเปิดใช้งานอุปกรณ์ไคลเอนต์เพื่อ รับข้อความ FCM ในโหมดบูตโดยตรง
ส่งโดยใช้ FCM v1 HTTP API
คำขอข้อความต้องมีคีย์ "direct_boot_ok" : true
ในตัวเลือก AndroidConfig
ของเนื้อหาคำขอ ตัวอย่างเช่น:
https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send
Content-Type:application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"Message":{
"token" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
"data": {
"score": "5x1",
"time": "15:10"
},
"android": {
"direct_boot_ok": true,
},
}
ส่งโดยใช้ FCM ดั้งเดิม HTTP API
คำขอข้อความต้องมีคีย์ "direct_boot_ok" : true
ที่ระดับบนสุดของเนื้อหาคำขอ ตัวอย่างเช่น:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
"direct_boot_ok" : true
}
ข้อความที่ส่งด้วยคีย์นี้ในเนื้อหาคำขอสามารถจัดการได้โดยแอปบนอุปกรณ์ที่อยู่ในโหมดบูตโดยตรง (และเมื่อไม่ได้อยู่ในโหมดนั้น)
ปรับแต่งข้อความข้ามแพลตฟอร์ม
ทั้ง Firebase Admin SDK และโปรโตคอล HTTP FCM v1 อนุญาตให้คำขอข้อความของคุณตั้งค่าฟิลด์ทั้งหมดที่มีอยู่ในออบเจ็กต์ message
ซึ่งรวมถึง:
- ชุดฟิลด์ทั่วไปที่จะตีความโดยอินสแตนซ์ของแอป ทั้งหมด ที่ได้รับข้อความ
- ชุดฟิลด์เฉพาะแพลตฟอร์มเช่น
AndroidConfig
และWebpushConfig
ตีความโดยอินสแตนซ์ของแอปที่ทำงานบนแพลตฟอร์มที่ระบุเท่านั้น
บล็อกเฉพาะแพลตฟอร์มให้ความยืดหยุ่นในการปรับแต่งข้อความสำหรับแพลตฟอร์มต่างๆเพื่อให้แน่ใจว่าได้รับการจัดการอย่างถูกต้องเมื่อได้รับ แบ็กเอนด์ FCM จะนำพารามิเตอร์ที่ระบุทั้งหมดมาพิจารณาและปรับแต่งข้อความสำหรับแต่ละแพลตฟอร์ม
เมื่อใดควรใช้ฟิลด์ทั่วไป
ใช้ฟิลด์ทั่วไปเมื่อคุณ:
- กำหนดเป้าหมายอินสแตนซ์ของแอปใน ทุก แพลตฟอร์ม - iOS, Android และเว็บ
- การส่งข้อความไปยังหัวข้อ
อินสแตนซ์ของแอปทั้งหมดโดยไม่คำนึงถึงแพลตฟอร์มสามารถตีความฟิลด์ทั่วไปต่อไปนี้:
เมื่อใดควรใช้ฟิลด์เฉพาะแพลตฟอร์ม
ใช้ฟิลด์เฉพาะแพลตฟอร์มเมื่อคุณต้องการ:
- ส่งฟิลด์ไปยังแพลตฟอร์มใดแพลตฟอร์มหนึ่งเท่านั้น
- ส่งฟิลด์เฉพาะแพลตฟอร์ม เพิ่มเติม จากฟิลด์ทั่วไป
เมื่อใดก็ตามที่คุณต้องการส่งค่าไปยังแพลตฟอร์มใดแพลตฟอร์มหนึ่งเท่านั้น อย่า ใช้ฟิลด์ทั่วไป ใช้ฟิลด์เฉพาะแพลตฟอร์ม ตัวอย่างเช่นหากต้องการส่งการแจ้งเตือนไปยัง iOS และเว็บเท่านั้น แต่ไม่ส่งไปยัง Android คุณต้องใช้ฟิลด์สองชุดแยกกันชุดหนึ่งสำหรับ iOS และอีกชุดสำหรับเว็บ
เมื่อคุณส่งข้อความพร้อม ตัวเลือกการส่ง เฉพาะให้ใช้ฟิลด์เฉพาะแพลตฟอร์มเพื่อตั้งค่า คุณสามารถระบุค่าที่แตกต่างกันต่อแพลตฟอร์มได้หากต้องการ อย่างไรก็ตามแม้ว่าคุณต้องการกำหนดค่าที่เหมือนกันในทุกแพลตฟอร์ม แต่คุณต้องใช้ฟิลด์เฉพาะแพลตฟอร์ม เนื่องจากแต่ละแพลตฟอร์มอาจตีความค่าแตกต่างกันเล็กน้อยตัวอย่างเช่นกำหนดเวลาถ่ายทอดสดบน Android เป็นเวลาหมดอายุเป็นวินาทีในขณะที่บน iOS จะกำหนดเป็น วัน หมดอายุ
ตัวอย่าง: ข้อความแจ้งเตือนพร้อมตัวเลือกสีและไอคอน
คำขอส่งตัวอย่างนี้จะส่งชื่อและเนื้อหาการแจ้งเตือนทั่วไปไปยังทุกแพลตฟอร์ม แต่ยังส่งการแทนที่เฉพาะบางแพลตฟอร์มไปยังอุปกรณ์ Android
สำหรับ Android คำขอจะกำหนดไอคอนและสีพิเศษเพื่อแสดงบนอุปกรณ์ Android ตามที่ระบุไว้ในข้อมูลอ้างอิงสำหรับ AndroidNotification สีจะถูกระบุในรูปแบบ #rrggbb และรูปภาพต้องเป็นทรัพยากรไอคอนที่วาดได้ในเครื่องของแอป Android
นี่คือการประมาณของเอฟเฟกต์ภาพบนอุปกรณ์ของผู้ใช้:
โหนด js
var admin = require("firebase-admin")
admin.initializeApp({
credential: admin.credential.applicationDefault(),
});
var topicName = 'industry-tech'
var message = {
notification: {
title: '`$FooCorp` up 1.43% on the day',
body: 'FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
},
android: {
notification: {
icon: 'stock_ticker_update',
color: '#7e55c3'
}
},
topic: topicName,
};
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);
});
Java
Message message = Message.builder()
.setNotification(Notification.builder()
.setTitle("$GOOG up 1.43% on the day")
.setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")
.build())
.setAndroidConfig(AndroidConfig.builder()
.setTtl(3600 * 1000)
.setNotification(AndroidNotification.builder()
.setIcon("stock_ticker_update")
.setColor("#f45342")
.build())
.build())
.setApnsConfig(ApnsConfig.builder()
.setAps(Aps.builder()
.setBadge(42)
.build())
.build())
.setTopic("industry-tech")
.build();
Python
message = messaging.Message(
notification=messaging.Notification(
title='$GOOG up 1.43% on the day',
body='$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
),
android=messaging.AndroidConfig(
ttl=datetime.timedelta(seconds=3600),
priority='normal',
notification=messaging.AndroidNotification(
icon='stock_ticker_update',
color='#f45342'
),
),
apns=messaging.APNSConfig(
payload=messaging.APNSPayload(
aps=messaging.Aps(badge=42),
),
),
topic='industry-tech',
)
ไป
oneHour := time.Duration(1) * time.Hour
badge := 42
message := &messaging.Message{
Notification: &messaging.Notification{
Title: "$GOOG up 1.43% on the day",
Body: "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.",
},
Android: &messaging.AndroidConfig{
TTL: &oneHour,
Notification: &messaging.AndroidNotification{
Icon: "stock_ticker_update",
Color: "#f45342",
},
},
APNS: &messaging.APNSConfig{
Payload: &messaging.APNSPayload{
Aps: &messaging.Aps{
Badge: &badge,
},
},
},
Topic: "industry-tech",
}
ค#
var message = new Message
{
Notification = new Notification()
{
Title = "$GOOG up 1.43% on the day",
Body = "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.",
},
Android = new AndroidConfig()
{
TimeToLive = TimeSpan.FromHours(1),
Notification = new AndroidNotification()
{
Icon = "stock_ticker_update",
Color = "#f45342",
},
},
Apns = new ApnsConfig()
{
Aps = new Aps()
{
Badge = 42,
},
},
Topic = "industry-tech",
};
ส่วนที่เหลือ
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"topic":"industry-tech",
"notification":{
"title":"`$FooCorp` up 1.43% on the day",
"body":"FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day."
},
"android":{
"notification":{
"icon":"stock_ticker_update",
"color":"#7e55c3"
}
}
}
}
ดู เอกสารอ้างอิง HTTP v1 สำหรับรายละเอียดทั้งหมดเกี่ยวกับคีย์ที่มีอยู่ในบล็อกเฉพาะแพลตฟอร์มในเนื้อหาข้อความ
ตัวอย่าง: ข้อความแจ้งเตือนพร้อมรูปภาพที่กำหนดเอง
คำขอส่งตัวอย่างต่อไปนี้จะส่งชื่อการแจ้งเตือนทั่วไปไปยังทุกแพลตฟอร์ม แต่จะส่งรูปภาพด้วย นี่คือการประมาณของเอฟเฟกต์ภาพบนอุปกรณ์ของผู้ใช้:
โหนด js
var admin = require("firebase-admin")
admin.initializeApp({
credential: admin.credential.applicationDefault(),
});
var topicName = 'industry-tech'
var message = {
notification: {
title: 'Sparky says hello!'
},
android: {
notification: {
image: 'https://foo.bar.pizza-monster.png'
}
},
apns: {
payload: {
aps: {
'mutable-content': 1
}
},
fcm_options: {
image: 'https://foo.bar.pizza-monster.png''
}
},
webpush: {
headers: {
image: 'https://foo.bar.pizza-monster.png''
}
},
topic: topicName,
};
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);
});
ส่วนที่เหลือ
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"topic":"industry-tech",
"notification":{
"title":"Sparky says hello!",
},
"android":{
"notification":{
"image":"https://foo.bar/pizza-monster.png"
}
},
"apns":{
"payload":{
"aps":{
"mutable-content":1
}
},
"fcm_options": {
"image":"https://foo.bar/pizza-monster.png"
}
},
"webpush":{
"headers":{
"image":"https://foo.bar/pizza-monster.png"
}
}
}
}
ดู เอกสารอ้างอิง HTTP v1 สำหรับรายละเอียดทั้งหมดเกี่ยวกับคีย์ที่มีอยู่ในบล็อกเฉพาะแพลตฟอร์มในเนื้อหาข้อความ
ตัวอย่าง: ข้อความแจ้งเตือนพร้อมการคลิกที่เกี่ยวข้อง
คำขอส่งตัวอย่างต่อไปนี้จะส่งชื่อการแจ้งเตือนทั่วไปไปยังทุกแพลตฟอร์ม แต่ยังส่งการดำเนินการเพื่อให้แอปดำเนินการเพื่อตอบสนองต่อผู้ใช้ที่โต้ตอบกับการแจ้งเตือน นี่คือการประมาณของเอฟเฟกต์ภาพบนอุปกรณ์ของผู้ใช้:
โหนด js
var admin = require("firebase-admin")
admin.initializeApp({
credential: admin.credential.applicationDefault(),
});
var topicName = 'industry-tech'
var message = {
notification: {
title: 'Breaking News....'
},
android: {
notification: {
click_action: 'news_intent'
}
},
apns: {
payload: {
aps: {
'category': 'INVITE_CATEGORY'
}
}
},
webpush: {
fcm_options: {
link: 'breakingnews.html'
}
},
topic: topicName,
};
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);
});
ส่วนที่เหลือ
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"topic":"industry-tech",
"notification":{
"title":"Breaking News...",
},
"android":{
"notification":{
"click_action":"news_intent"
}
},
"apns":{
"payload":{
"aps":{
"category" : "INVITE_CATEGORY"
}
},
},
"webpush":{
"fcm_options":{
"link":"breakingnews.html"
}
}
}
}
ดู เอกสารอ้างอิง HTTP v1 สำหรับรายละเอียดทั้งหมดเกี่ยวกับคีย์ที่มีอยู่ในบล็อกเฉพาะแพลตฟอร์มในเนื้อหาข้อความ
ตัวอย่าง: ข้อความแจ้งเตือนพร้อมตัวเลือกการแปล
คำขอส่งตัวอย่างต่อไปนี้จะส่งตัวเลือกการแปลสำหรับไคลเอ็นต์เพื่อแสดงข้อความที่แปลเป็นภาษาท้องถิ่น นี่คือการประมาณเอฟเฟกต์ภาพบนอุปกรณ์ของผู้ใช้:
โหนด js
var admin = require("firebase-admin")
admin.initializeApp({
credential: admin.credential.applicationDefault(),
});
var topicName = 'industry-tech'
var message = {
android: {
ttl: 3600000,
notification: {
body_loc_key: 'STOCK_NOTIFICATION_BODY',
body_loc_args: ['FooCorp', '11.80', '835.67', '1.43']
}
},
apns: {
payload: {
aps: {
'alert': {
'loc-key': 'STOCK_NOTIFICATION_BODY',
'loc-args': ['FooCorp', '11.80', '835.67', '1.43']
}
}
}
},
topic: topicName,
};
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);
});
ส่วนที่เหลือ
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"topic":"Tech",
"android":{
"ttl":"3600s",
"notification":{
"body_loc_key": "STOCK_NOTIFICATION_BODY",
"body_loc_args": ["FooCorp", "11.80", "835.67", "1.43"],
},
},
"apns":{
"payload":{
"aps":{
"alert" : {
"loc-key": "STOCK_NOTIFICATION_BODY",
"loc-args": ["FooCorp", "11.80", "835.67", "1.43"],
},
},
},
},
},
}'
ดู เอกสารอ้างอิง HTTP v1 สำหรับรายละเอียดทั้งหมดเกี่ยวกับคีย์ที่มีอยู่ในบล็อกเฉพาะแพลตฟอร์มในเนื้อหาข้อความ
รหัสข้อผิดพลาดของผู้ดูแลระบบ
ตารางต่อไปนี้แสดงรหัสข้อผิดพลาด FCM API ของผู้ดูแลระบบ Firebase และคำอธิบายรวมถึงขั้นตอนการแก้ปัญหาที่แนะนำ
รหัสข้อผิดพลาด | คำอธิบายและขั้นตอนการแก้ปัญหา |
---|---|
messaging/invalid-argument | มีการระบุอาร์กิวเมนต์ที่ไม่ถูกต้องให้กับเมธอด FCM ข้อความแสดงข้อผิดพลาดควรมีข้อมูลเพิ่มเติม |
messaging/invalid-recipient | ผู้รับข้อความที่ต้องการไม่ถูกต้อง ข้อความแสดงข้อผิดพลาดควรมีข้อมูลเพิ่มเติม |
messaging/invalid-payload | มีการระบุอ็อบเจ็กต์เพย์โหลดข้อความที่ไม่ถูกต้อง ข้อความแสดงข้อผิดพลาดควรมีข้อมูลเพิ่มเติม |
messaging/invalid-data-payload-key | เพย์โหลดข้อความข้อมูลมีคีย์ที่ไม่ถูกต้อง ดูเอกสารอ้างอิงสำหรับ DataMessagePayload สำหรับคีย์ที่ จำกัด |
messaging/payload-size-limit-exceeded | เพย์โหลดข้อความที่ระบุเกินขีด จำกัด ขนาด FCM ขีด จำกัด คือ 4096 ไบต์สำหรับข้อความส่วนใหญ่ สำหรับข้อความที่ส่งถึงหัวข้อขีด จำกัด คือ 2048 ไบต์ ขนาดน้ำหนักบรรทุกทั้งหมดมีทั้งคีย์และค่า |
messaging/invalid-options | มีการระบุอ็อบเจ็กต์ตัวเลือกข้อความที่ไม่ถูกต้อง ข้อความแสดงข้อผิดพลาดควรมีข้อมูลเพิ่มเติม |
messaging/invalid-registration-token | โทเค็นการลงทะเบียนที่ระบุไม่ถูกต้อง ตรวจสอบให้แน่ใจว่าตรงกับโทเค็นการลงทะเบียนที่แอปไคลเอ็นต์ได้รับจากการลงทะเบียนกับ FCM อย่าตัดทอนหรือเพิ่มอักขระเพิ่มเติมเข้าไป |
messaging/registration-token-not-registered | โทเค็นการลงทะเบียนที่ระบุไม่ได้ลงทะเบียน โทเค็นการลงทะเบียนที่ถูกต้องก่อนหน้านี้สามารถยกเลิกการลงทะเบียนได้ด้วยเหตุผลหลายประการ ได้แก่ :
|
messaging/invalid-package-name | ข้อความถูกส่งไปยังโทเค็นการลงทะเบียนที่มีชื่อแพ็กเกจไม่ตรงกับอ็อพชัน restrictedPackageName ให้มา |
messaging/message-rate-exceeded | อัตราการส่งข้อความไปยังเป้าหมายหนึ่ง ๆ สูงเกินไป ลดจำนวนข้อความที่ส่งไปยังอุปกรณ์หรือหัวข้อนี้และอย่าพยายามส่งไปยังเป้าหมายนี้อีกครั้งในทันที |
messaging/device-message-rate-exceeded | อัตราการส่งข้อความไปยังอุปกรณ์บางเครื่องสูงเกินไป ลดจำนวนข้อความที่ส่งไปยังอุปกรณ์นี้และอย่าพยายามส่งไปยังอุปกรณ์นี้อีกครั้งในทันที |
messaging/topics-message-rate-exceeded | อัตราการส่งข้อความถึงสมาชิกในหัวข้อใดหัวข้อหนึ่งสูงเกินไป ลดจำนวนข้อความที่ส่งสำหรับหัวข้อนี้และอย่าพยายามส่งไปที่หัวข้อนี้อีกครั้งในทันที |
messaging/too-many-topics | โทเค็นการลงทะเบียนสมัครเป็นสมาชิกตามจำนวนหัวข้อสูงสุดแล้วและไม่สามารถสมัครรับข้อมูลได้อีก |
messaging/invalid-apns-credentials | ไม่สามารถส่งข้อความที่กำหนดเป้าหมายไปยังอุปกรณ์ iOS เนื่องจากไม่ได้อัปโหลดใบรับรอง APNs SSL ที่จำเป็นหรือหมดอายุแล้ว ตรวจสอบความถูกต้องของใบรับรองการพัฒนาและการผลิตของคุณ |
messaging/mismatched-credential | ข้อมูลรับรองที่ใช้ในการตรวจสอบสิทธิ์ SDK นี้ไม่มีสิทธิ์ในการส่งข้อความไปยังอุปกรณ์ที่ตรงกับโทเค็นการลงทะเบียนที่ให้มา ตรวจสอบว่าโทเค็นข้อมูลรับรองและการลงทะเบียนเป็นของโครงการ Firebase เดียวกัน โปรดดู เพิ่ม Firebase ในแอปของคุณ สำหรับเอกสารเกี่ยวกับวิธีตรวจสอบสิทธิ์ Firebase Admin SDK |
messaging/authentication-error | SDK ไม่สามารถรับรองความถูกต้องกับเซิร์ฟเวอร์ FCM ตรวจสอบว่าคุณได้ตรวจสอบสิทธิ์ Firebase Admin SDK ด้วยข้อมูลรับรองซึ่งมีสิทธิ์ที่เหมาะสมในการส่งข้อความ FCM โปรดดู เพิ่ม Firebase ในแอปของคุณ สำหรับเอกสารเกี่ยวกับวิธีตรวจสอบสิทธิ์ Firebase Admin SDK |
messaging/server-unavailable | เซิร์ฟเวอร์ FCM ไม่สามารถดำเนินการตามคำขอได้ทันเวลา คุณควรลองคำขอเดิมอีกครั้ง แต่คุณต้อง:
|
messaging/internal-error | เซิร์ฟเวอร์ FCM พบข้อผิดพลาดขณะพยายามดำเนินการตามคำขอ คุณสามารถลองคำขอเดิมอีกครั้งได้ตามข้อกำหนดที่ระบุไว้ในแถว messaging/server-unavailable ด้านบน หากข้อผิดพลาดยังคงอยู่โปรดรายงานปัญหาไปยังช่องทางการสนับสนุน รายงานข้อบกพร่อง ของเรา |
messaging/unknown-error | ข้อผิดพลาดของเซิร์ฟเวอร์ที่ไม่รู้จักถูกส่งกลับ ดูการตอบสนองของเซิร์ฟเวอร์ดิบในข้อความแสดงข้อผิดพลาดสำหรับรายละเอียดเพิ่มเติม หากคุณได้รับข้อผิดพลาดนี้โปรดรายงานข้อความแสดงข้อผิดพลาดฉบับเต็มไปยังช่องทางการสนับสนุน รายงานข้อผิดพลาด ของเรา |
ส่งข้อความโดยใช้โปรโตคอลเซิร์ฟเวอร์แอปเดิม
หากคุณต้องการใช้โปรโตคอลเดิมให้สร้างคำขอข้อความตามที่แสดงในส่วนนี้ โปรดทราบว่าหากคุณส่งไปยังหลายแพลตฟอร์มผ่าน HTTP โปรโตคอล v1 สามารถทำให้คำขอข้อความของคุณง่ายขึ้น
ส่งข้อความไปยังอุปกรณ์เฉพาะ
ส่งข้อความไปยังอุปกรณ์เฉพาะการตั้งค่า to
สำคัญในการลงทะเบียน token เช่นแอปที่เฉพาะเจาะจง ดูข้อมูลการตั้งค่าไคลเอนต์สำหรับแพลตฟอร์มของคุณเพื่อเรียนรู้เพิ่มเติมเกี่ยวกับโทเค็นการลงทะเบียน
คำขอ HTTP POST
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
การตอบสนอง HTTP
{ "multicast_id": 108, "success": 1, "failure": 0, "results": [ { "message_id": "1:08" } ] }
ข้อความ XMPP
<message id="">
<gcm xmlns="google:mobile:data">
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
</gcm>
</message>
การตอบสนอง XMPP
<message id=""> <gcm xmlns="google:mobile:data"> { "from":"REGID", "message_id":"m-1366082849205" "message_type":"ack" } </gcm> </message>
เซิร์ฟเวอร์การเชื่อมต่อ XMPP มีตัวเลือกอื่น ๆ สำหรับการตอบสนอง ดู รูปแบบการตอบกลับของเซิร์ฟเวอร์
สำหรับรายการตัวเลือกข้อความทั้งหมดที่มีให้เมื่อส่งข้อความดาวน์สตรีมไปยังแอปไคลเอ็นต์โปรดดูข้อมูลอ้างอิงสำหรับโปรโตคอลเซิร์ฟเวอร์การเชื่อมต่อที่คุณเลือก HTTP หรือ XMPP
ส่งข้อความไปยังหัวข้อ
การส่งข้อความไปยังหัวข้อ Firebase Cloud Messaging นั้นคล้ายกับการส่งข้อความไปยังอุปกรณ์แต่ละเครื่องหรือไปยังกลุ่มผู้ใช้ เซิร์ฟเวอร์แอปตั้งค่า to
สำคัญที่มีค่าเช่น /topics/yourTopic
นักพัฒนาสามารถเลือกชื่อหัวข้อที่ตรงกับนิพจน์ทั่วไป: "/topics/[a-zA-Z0-9-_.~%]+"
เพื่อส่งให้กับการรวมกันของหลายหัวข้อเซิร์ฟเวอร์แอปจะต้องกำหนด condition
ที่สำคัญ (แทน to
คีย์) ให้อยู่ในสภาพแบบบูลที่ระบุหัวข้อเป้าหมาย ตัวอย่างเช่นในการส่งข้อความไปยังอุปกรณ์ที่สมัครสมาชิก TopicA
และ TopicB
หรือ TopicC
:
'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)
ก่อนอื่น FCM จะประเมินเงื่อนไขใด ๆ ในวงเล็บจากนั้นจึงประเมินนิพจน์จากซ้ายไปขวา ในนิพจน์ข้างต้นผู้ใช้ที่สมัครเป็นสมาชิกหัวข้อใดหัวข้อหนึ่งจะไม่ได้รับข้อความ ในทำนองเดียวกันผู้ใช้ที่ไม่ได้สมัครสมาชิก TopicA จะไม่ได้รับข้อความ ชุดค่าผสมเหล่านี้จะได้รับ:
- TopicA และ TopicB
- TopicA และ TopicC
คุณสามารถรวมหัวข้อได้สูงสุดห้าหัวข้อในนิพจน์เงื่อนไขของคุณและรองรับวงเล็บ ตัวดำเนินการที่รองรับ: &&
, ||
.
สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับคีย์เซิร์ฟเวอร์ของแอปโปรดดูข้อมูลอ้างอิงสำหรับโปรโตคอลเซิร์ฟเวอร์การเชื่อมต่อที่คุณเลือก HTTP หรือ XMPP ตัวอย่างในหน้านี้แสดงวิธีการส่งข้อความไปยังหัวข้อทั้งใน HTTP และ XMPP
หัวข้อคำขอ HTTP POST
ส่งไปที่หัวข้อเดียว:
https://fcm.googleapis.com/fcm/send Content-Type:application/json Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
ส่งไปยังอุปกรณ์ที่สมัครรับหัวข้อ "สุนัข" หรือ "แมว":
https://fcm.googleapis.com/fcm/send Content-Type:application/json Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
หัวข้อการตอบสนอง HTTP
//Success example: { "message_id": "1023456" } //failure example: { "error": "TopicsMessageRateExceeded" }
หัวข้อข้อความ XMPP
ส่งไปที่หัวข้อเดียว:
<message id="">
<gcm xmlns="google:mobile:data">
</gcm>
</message>
ส่งไปยังอุปกรณ์ที่สมัครรับหัวข้อ "สุนัข" หรือ "แมว":
<message id=""> <gcm xmlns="google:mobile:data"> </gcm> </message>
หัวข้อการตอบสนอง XMPP
//Success example: { "message_id": "1023456" } //failure example: { "error": "TopicsMessageRateExceeded" }
คาดว่าจะล่าช้าถึง 30 วินาทีก่อนที่ FCM Server จะส่งคืนการตอบสนองความสำเร็จหรือความล้มเหลวไปยังหัวข้อที่ส่งคำขอ ตรวจสอบให้แน่ใจว่าได้ตั้งค่าการหมดเวลาของเซิร์ฟเวอร์แอปในคำขอตามนั้น
สำหรับรายการตัวเลือกข้อความทั้งหมดโปรดดูข้อมูลอ้างอิงสำหรับโปรโตคอลเซิร์ฟเวอร์การเชื่อมต่อที่คุณเลือก HTTP หรือ XMPP
ส่งข้อความไปยังกลุ่มอุปกรณ์
การส่งข้อความไปยังกลุ่มอุปกรณ์นั้นคล้ายกับการส่งข้อความไปยังอุปกรณ์แต่ละเครื่อง ตั้งค่า to
พารามิเตอร์คีย์การแจ้งเตือนที่ไม่ซ้ำกันสำหรับกลุ่มอุปกรณ์ ดู ประเภทข้อความ สำหรับรายละเอียดเกี่ยวกับการสนับสนุนเพย์โหลด ตัวอย่างในหน้านี้แสดงวิธีการส่งข้อความข้อมูลไปยังกลุ่มอุปกรณ์ในโปรโตคอล HTTP และ XMPP
คำขอ HTTP POST ของกลุ่มอุปกรณ์
https://fcm.googleapis.com/fcm/send Content-Type:application/json Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA { "to": "aUniqueKey", "data": { "hello": "This is a Firebase Cloud Messaging Device Group Message!", } }
การตอบสนอง HTTP ของกลุ่มอุปกรณ์
นี่คือตัวอย่างของ "ความสำเร็จ" - notification_key
การ notification_key
มีโทเค็นการลงทะเบียน 2 รายการที่เชื่อมโยงกันและส่งข้อความถึงทั้งสองสำเร็จแล้ว:
{ "success": 2, "failure": 0 }
นี่คือตัวอย่างของ "ความสำเร็จบางส่วน" - notification_key
การ notification_key
มีโทเค็นการลงทะเบียน 3 รายการที่เชื่อมโยงอยู่ ข้อความถูกส่งไปยัง 1 ในโทเค็นการลงทะเบียนเท่านั้น ข้อความตอบกลับแสดงโทเค็นการลงทะเบียนที่ไม่สามารถรับข้อความ:
{ "success":1, "failure":2, "failed_registration_ids":[ "regId1", "regId2" ] }
เมื่อไม่สามารถส่งข้อความไปยังโทเค็นการลงทะเบียนอย่างน้อยหนึ่งรายการที่เชื่อมโยงกับ notification_key
เซิร์ฟเวอร์แอปควรลองอีกครั้งโดยถอยกลับระหว่างการลองใหม่
หากเซิร์ฟเวอร์พยายามส่งข้อความไปยังกลุ่มอุปกรณ์ที่ไม่มีสมาชิกการตอบกลับจะมีลักษณะดังนี้โดยมี 0 สำเร็จและ 0 ล้มเหลว:
{ "success": 0, "failure": 0 }
ข้อความ XMPP ของกลุ่มอุปกรณ์
<message id=""> <gcm xmlns="google:mobile:data"> { "to": "aUniqueKey", "message_id": "m-1366082849205" , "data": { "hello":"This is a Firebase Cloud Messaging Device Group Message!" } } </gcm> </message>
การตอบสนอง XMPP ของกลุ่มอุปกรณ์
เมื่อข้อความถูกส่งไปยังอุปกรณ์ใด ๆ ในกลุ่มสำเร็จเซิร์ฟเวอร์การเชื่อมต่อ XMPP จะตอบสนองด้วย ACK หากข้อความทั้งหมดที่ส่งไปยังอุปกรณ์ทั้งหมดในกลุ่มล้มเหลวเซิร์ฟเวอร์การเชื่อมต่อ XMPP จะตอบสนองด้วย NACK
นี่คือตัวอย่างของ "ความสำเร็จ" - notification_key
การ notification_key
มีโทเค็นการลงทะเบียน 3 รายการที่เชื่อมโยงอยู่และข้อความถูกส่งถึงพวกเขาทั้งหมดสำเร็จแล้ว:
{ "from": "aUniqueKey", "message_type": "ack", "success": 3, "failure": 0, "message_id": "m-1366082849205" }
นี่คือตัวอย่างของ "ความสำเร็จบางส่วน" - notification_key
การ notification_key
มีโทเค็นการลงทะเบียน 3 รายการที่เชื่อมโยงอยู่ ข้อความถูกส่งไปยัง 1 ในโทเค็นการลงทะเบียนเท่านั้น ข้อความตอบกลับแสดงโทเค็นการลงทะเบียนที่ไม่สามารถรับข้อความ:
{ "from": "aUniqueKey", "message_type": "ack", "success":1, "failure":2, "failed_registration_ids":[ "regId1", "regId2" ] }
เมื่อเซิร์ฟเวอร์การเชื่อมต่อ FCM ไม่สามารถส่งมอบไปยังอุปกรณ์ทั้งหมดในกลุ่ม เซิร์ฟเวอร์ของแอปจะได้รับการตอบสนอง
สำหรับรายการตัวเลือกข้อความทั้งหมดโปรดดูข้อมูลอ้างอิงสำหรับโปรโตคอลเซิร์ฟเวอร์การเชื่อมต่อที่คุณเลือก HTTP หรือ XMPP
วิธีการส่งแบบเดิมของ Firebase Admin SDK
Firebase Admin Node.js SDK รองรับวิธีการส่งข้อความ (FCM) ตาม Legacy FCM server API เมธอดเหล่านี้ยอมรับอาร์กิวเมนต์ที่แตกต่างกันเมื่อเทียบกับเมธอด send()
คุณควรใช้เมธอด send()
ทุกครั้งที่ทำได้และใช้เฉพาะวิธีการที่อธิบายไว้ในเพจนี้เมื่อส่งข้อความไปยังอุปกรณ์แต่ละเครื่องหรือกลุ่มอุปกรณ์
ส่งไปยังอุปกรณ์แต่ละเครื่อง
คุณสามารถส่งโทเค็นการลงทะเบียนไปยัง sendToDevice()
เพื่อส่งข้อความไปยังอุปกรณ์นั้น:
โหนด js
// This registration token comes from the client FCM SDKs.
var registrationToken = 'bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...';
// See the "Defining the message payload" section below for details
// on how to define a message payload.
var payload = {
data: {
score: '850',
time: '2:45'
}
};
// Send a message to the device corresponding to the provided
// registration token.
admin.messaging().sendToDevice(registrationToken, payload)
.then(function(response) {
// See the MessagingDevicesResponse reference documentation for
// the contents of response.
console.log('Successfully sent message:', response);
})
.catch(function(error) {
console.log('Error sending message:', error);
});
sendToDevice()
ยังสามารถส่งข้อความ แบบหลายผู้รับ (นั่นคือข้อความไปยังอุปกรณ์หลายเครื่อง) โดยส่งโทเค็นการลงทะเบียนอาร์เรย์แทนที่จะเป็นโทเค็นการลงทะเบียนเดียว:
โหนด js
// These registration tokens come from the client FCM SDKs.
var registrationTokens = [
'bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...',
// ...
'ecupwIfBy1w:APA91bFtuMY7MktgxA3Au_Qx7cKqnf...'
];
// See the "Defining the message payload" section below for details
// on how to define a message payload.
var payload = {
data: {
score: '850',
time: '2:45'
}
};
// Send a message to the devices corresponding to the provided
// registration tokens.
admin.messaging().sendToDevice(registrationTokens, payload)
.then(function(response) {
// See the MessagingDevicesResponse reference documentation for
// the contents of response.
console.log('Successfully sent message:', response);
})
.catch(function(error) {
console.log('Error sending message:', error);
});
sendToDevice()
ส่งคืนสัญญาที่แก้ไขด้วยอ็อบเจ็กต์ MessagingDevicesResponse
ที่มีการตอบสนองจาก FCM ประเภทการส่งคืนมีรูปแบบเดียวกันเมื่อส่งผ่านโทเค็นการลงทะเบียนเดียวหรือโทเค็นการลงทะเบียนอาร์เรย์
บางกรณีเช่นข้อผิดพลาดในการตรวจสอบสิทธิ์หรือการ จำกัด อัตราทำให้ไม่สามารถประมวลผลข้อความทั้งหมดได้ ในกรณีเหล่านี้สัญญาที่ส่งคืนโดย sendToDevice()
จะถูกปฏิเสธโดยมีข้อผิดพลาด สำหรับรายการรหัสข้อผิดพลาดทั้งหมดรวมถึงคำอธิบายและขั้นตอนการแก้ปัญหาโปรดดู ข้อผิดพลาดของ Admin FCM API
ส่งไปยังกลุ่มอุปกรณ์
การส่งข้อความกลุ่มอุปกรณ์ช่วยให้คุณสามารถเพิ่มอุปกรณ์หลายเครื่องในกลุ่มเดียว สิ่งนี้คล้ายกับการส่งข้อความตามหัวข้อ แต่รวมถึงการพิสูจน์ตัวตนเพื่อให้แน่ใจว่าการเป็นสมาชิกกลุ่มได้รับการจัดการโดยเซิร์ฟเวอร์ของคุณเท่านั้น ตัวอย่างเช่นหากคุณต้องการส่งข้อความที่แตกต่างกันไปยังโทรศัพท์รุ่นต่างๆเซิร์ฟเวอร์ของคุณสามารถเพิ่ม / ลบการลงทะเบียนไปยังกลุ่มที่เหมาะสมและส่งข้อความที่เหมาะสมไปยังแต่ละกลุ่ม การส่งข้อความกลุ่มอุปกรณ์แตกต่างจากการส่งข้อความตามหัวข้อตรงที่เกี่ยวข้องกับการจัดการกลุ่มอุปกรณ์จากเซิร์ฟเวอร์ของคุณแทนที่จะอยู่ภายในแอปพลิเคชันของคุณโดยตรง
คุณสามารถใช้การส่งข้อความกลุ่มอุปกรณ์ผ่าน XMPP เดิมหรือโปรโตคอล HTTP บนเซิร์ฟเวอร์แอปของคุณ Firebase Admin SDK สำหรับ Node.js ตามโปรโตคอลเดิมยังให้ความสามารถในการส่งข้อความกลุ่มอุปกรณ์ จำนวนสมาชิกสูงสุดที่อนุญาตสำหรับคีย์การแจ้งเตือนคือ 20
คุณสามารถสร้างกลุ่มอุปกรณ์และสร้างคีย์การแจ้งเตือนผ่านเซิร์ฟเวอร์แอปหรือไคลเอนต์ Android ดูการ จัดการกลุ่มอุปกรณ์ สำหรับรายละเอียด
sendToDeviceGroup()
อนุญาตให้คุณส่งข้อความไปยังกลุ่มอุปกรณ์โดยระบุคีย์การแจ้งเตือนสำหรับกลุ่มอุปกรณ์นั้น:
โหนด js
// See the "Managing device groups" link above on how to generate a
// notification key.
var notificationKey = 'some-notification-key';
// See the "Defining the message payload" section below for details
// on how to define a message payload.
var payload = {
data: {
score: '850',
time: '2:45'
}
};
// Send a message to the device group corresponding to the provided
// notification key.
admin.messaging().sendToDeviceGroup(notificationKey, payload)
.then(function(response) {
// See the MessagingDeviceGroupResponse reference documentation for
// the contents of response.
console.log('Successfully sent message:', response);
})
.catch(function(error) {
console.log('Error sending message:', error);
});
sendToDeviceGroup()
ส่งคืนสัญญาที่แก้ไขด้วยอ็อบเจ็กต์ MessagingDeviceGroupResponse
ที่มีการตอบสนองจาก FCM
บางกรณีเช่นข้อผิดพลาดในการตรวจสอบสิทธิ์หรือการ จำกัด อัตราทำให้ไม่สามารถประมวลผลข้อความทั้งหมดได้ ในกรณีเหล่านี้คำสัญญาที่ส่งคืนโดย sendToDeviceGroup()
จะถูกปฏิเสธโดยมีข้อผิดพลาด สำหรับรายการรหัสข้อผิดพลาดทั้งหมดรวมถึงคำอธิบายและขั้นตอนการแก้ปัญหาโปรดดู ข้อผิดพลาดของ Admin FCM API
การกำหนดเพย์โหลดข้อความ
วิธีการข้างต้นตามโปรโตคอลดั้งเดิมของ FCM ยอมรับส่วนข้อมูลข้อความเป็นอาร์กิวเมนต์ที่สองและรองรับทั้ง ข้อความแจ้งเตือนและข้อมูล คุณสามารถระบุประเภทข้อความหนึ่งหรือทั้งสองประเภทได้โดยการสร้างออบเจ็กต์ด้วย data
และ / หรือคีย์การ notification
ตัวอย่างเช่นต่อไปนี้เป็นวิธีกำหนดเพย์โหลดข้อความประเภทต่างๆ:
ข้อความแจ้งเตือน
var payload = {
notification: {
title: '$FooCorp up 1.43% on the day',
body: '$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
}
};
ข้อความข้อมูล
var payload = {
data: {
score: '850',
time: '2:45'
}
};
ข้อความรวม
var payload = {
notification: {
title: '$FooCorp up 1.43% on the day',
body: '$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
},
data: {
stock: 'GOOG',
open: '829.62',
close: '635.67'
}
};
เพย์โหลดข้อความแจ้งเตือนมีคุณสมบัติที่ถูกต้องชุดย่อยที่กำหนดไว้ล่วงหน้าและแตกต่างกันเล็กน้อยขึ้นอยู่กับระบบปฏิบัติการมือถือที่คุณกำหนดเป้าหมาย ดูเอกสารอ้างอิงสำหรับ NotificationMessagePayload
สำหรับรายการทั้งหมด
เพย์โหลดข้อความข้อมูลประกอบด้วยคู่คีย์ - ค่าที่กำหนดเองโดยมีข้อ จำกัด บางประการรวมถึงความจริงที่ว่าค่าทั้งหมดต้องเป็นสตริง ดูเอกสารอ้างอิงสำหรับ DataMessagePayload
สำหรับรายการข้อ จำกัด ทั้งหมด
การกำหนดตัวเลือกข้อความ
วิธีการข้างต้นตามโปรโตคอลดั้งเดิมของ FCM ยอมรับอาร์กิวเมนต์ที่สามซึ่งเป็นทางเลือกซึ่งระบุตัวเลือกบางอย่างสำหรับข้อความ ตัวอย่างเช่นตัวอย่างต่อไปนี้ส่งข้อความที่มีลำดับความสำคัญสูงไปยังอุปกรณ์ซึ่งจะหมดอายุหลังจาก 24 ชั่วโมง:
โหนด js
// This registration token comes from the client FCM SDKs.
var registrationToken = 'bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...';
// See the "Defining the message payload" section above for details
// on how to define a message payload.
var payload = {
notification: {
title: 'Urgent action needed!',
body: 'Urgent action is needed to prevent your account from being disabled!'
}
};
// Set the message as high priority and have it expire after 24 hours.
var options = {
priority: 'high',
timeToLive: 60 * 60 * 24
};
// Send a message to the device corresponding to the provided
// registration token with the provided options.
admin.messaging().sendToDevice(registrationToken, payload, options)
.then(function(response) {
console.log('Successfully sent message:', response);
})
.catch(function(error) {
console.log('Error sending message:', error);
});
ดูเอกสารอ้างอิงสำหรับ MessagingOptions
สำหรับรายการตัวเลือกทั้งหมดที่มี