Firebase Admin SDK และ FCM v1
HTTP API ช่วยให้คำขอข้อความของคุณตั้งค่า
ฟิลด์ทั้งหมดที่มีในออบเจ็กต์
message
ได้ ซึ่งรวมถึง
- ชุดฟิลด์ทั่วไปที่อินสแตนซ์ของแอปทั้งหมดที่ได้รับข้อความจะตีความ
- ชุดฟิลด์เฉพาะแพลตฟอร์ม เช่น
AndroidConfig
และWebpushConfig
ซึ่งตีความโดยอินสแตนซ์แอปที่ทํางานบนแพลตฟอร์มที่ระบุเท่านั้น
การบล็อกเฉพาะแพลตฟอร์มช่วยให้คุณปรับแต่งข้อความสำหรับแพลตฟอร์มต่างๆ ได้อย่างยืดหยุ่นเพื่อให้มั่นใจว่าระบบจะจัดการข้อความอย่างถูกต้องเมื่อได้รับ แบ็กเอนด์ของ FCM จะพิจารณาทุกพารามิเตอร์ที่ระบุและปรับแต่ง ข้อความสำหรับแต่ละแพลตฟอร์ม
กรณีที่ควรใช้ฟิลด์ทั่วไป
ใช้ฟิลด์ทั่วไปเมื่อคุณทำสิ่งต่อไปนี้
- ส่งฟิลด์ไปยังแพลตฟอร์มใดก็ได้
- ส่งข้อความไปยังหัวข้อ
อินสแตนซ์ของแอปทั้งหมดไม่ว่าจะเป็นแพลตฟอร์มใดก็ตามสามารถตีความฟิลด์ทั่วไปต่อไปนี้ ได้
กรณีที่ควรใช้ฟิลด์เฉพาะแพลตฟอร์ม
ใช้ช่องเฉพาะแพลตฟอร์มเมื่อต้องการทำสิ่งต่อไปนี้
- ส่งฟิลด์ไปยังแพลตฟอร์มที่เฉพาะเจาะจงเท่านั้น
- ส่งฟิลด์เฉพาะแพลตฟอร์มนอกเหนือจากฟิลด์ทั่วไป
เมื่อใดก็ตามที่คุณต้องการส่งค่าไปยังแพลตฟอร์มที่เฉพาะเจาะจงเท่านั้น ให้ใช้ ฟิลด์เฉพาะแพลตฟอร์ม เช่น หากต้องการส่งการแจ้งเตือนไปยังแพลตฟอร์ม Apple และเว็บเท่านั้น แต่ไม่ส่งไปยัง Android คุณต้องใช้ฟิลด์ 2 ชุดแยกกัน ชุดหนึ่งสำหรับ Apple และอีกชุดหนึ่งสำหรับเว็บ
เมื่อส่งข้อความที่มีตัวเลือกการนำส่งที่เฉพาะเจาะจง ให้ใช้ฟิลด์เฉพาะแพลตฟอร์มเพื่อตั้งค่า คุณสามารถระบุค่าที่แตกต่างกันต่อแพลตฟอร์มได้หากต้องการ อย่างไรก็ตาม แม้ว่าคุณต้องการตั้งค่าที่เหมือนกันในทุกแพลตฟอร์ม คุณก็ต้องใช้ฟิลด์เฉพาะแพลตฟอร์ม เนื่องจากแต่ละแพลตฟอร์มอาจตีความค่าแตกต่างกันเล็กน้อย เช่น เวลาในการใช้งานจะตั้งค่าใน Android เป็นเวลาหมดอายุในหน่วยวินาที ส่วนใน Apple จะตั้งค่าเป็นวันที่หมดอายุ
ข้อความแจ้งเตือนพร้อมตัวเลือกการนำส่งเฉพาะแพลตฟอร์ม
คำขอส่ง HTTP v1 API ต่อไปนี้จะส่งชื่อการแจ้งเตือนและเนื้อหาทั่วไปไปยังทุกแพลตฟอร์ม แต่ก็ส่งการลบล้างเฉพาะแพลตฟอร์มบางรายการด้วย โดยคำขอต้องมีลักษณะดังนี้
- ตั้งค่าเวลาที่จะคงอยู่ไว้นานสำหรับแพลตฟอร์ม Android และเว็บ ขณะเดียวกันก็ตั้งค่าลำดับความสำคัญของข้อความ APNs (แพลตฟอร์ม Apple) เป็นการตั้งค่าต่ำ
- ตั้งค่าคีย์ที่เหมาะสมเพื่อกำหนดผลลัพธ์ของการแตะของผู้ใช้ใน
การแจ้งเตือนบน Android และ Apple —
click_action
และcategory
ตามลำดับ
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Match update",
"body":"Arsenal goal in added time, score is now 3-0"
},
"android":{
"ttl":"86400s",
"notification"{
"click_action":"OPEN_ACTIVITY_1"
}
},
"apns": {
"headers": {
"apns-priority": "5",
},
"payload": {
"aps": {
"category": "NEW_MESSAGE_CATEGORY"
}
}
},
"webpush":{
"headers":{
"TTL":"86400"
}
}
}
}
ดูข้อมูลเพิ่มเติมได้ที่หน้าข้อมูลอ้างอิง HTTP v1 เพื่อดูรายละเอียดเพิ่มเติม เกี่ยวกับคีย์ที่มีอยู่ในบล็อกเฉพาะแพลตฟอร์มในเนื้อหาของข้อความ ดูข้อมูลเพิ่มเติมเกี่ยวกับการสร้างคำขอส่งที่มีเนื้อหาข้อความได้ที่ ส่งข้อความโดยใช้ FCM HTTP v1 API
ข้อความแจ้งเตือนที่มีตัวเลือกสีและไอคอน
ในตัวอย่างต่อไปนี้ คำขอส่งจะส่งชื่อการแจ้งเตือนและเนื้อหาทั่วไปไปยังทุกแพลตฟอร์ม แต่จะส่งการลบล้างเฉพาะแพลตฟอร์มบางรายการไปยังอุปกรณ์ Android ด้วย
สำหรับ Android คำขอจะตั้งค่าไอคอนและสีพิเศษเพื่อแสดงบนอุปกรณ์ Android ดังที่ระบุไว้ในการอ้างอิงสำหรับ AndroidNotification สีจะระบุในรูปแบบ #rrggbb และรูปภาพต้องเป็นไอคอนที่วาดได้ ซึ่งเป็นทรัพยากรในเครื่องของแอป Android
ตัวอย่างเอฟเฟกต์ภาพบนอุปกรณ์ของผู้ใช้
Node.js
const topicName = 'industry-tech';
const 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,
};
getMessaging().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',
)
Go
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",
}
C#
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",
};
REST
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 เพื่อดูรายละเอียดเพิ่มเติมเกี่ยวกับคีย์ที่มีอยู่ในบล็อกเฉพาะแพลตฟอร์มในเนื้อหาข้อความ
ข้อความแจ้งเตือนพร้อมรูปภาพที่กำหนดเอง
คำขอส่งต่อไปนี้จะส่งชื่อการแจ้งเตือนทั่วไปไปยังทุกแพลตฟอร์ม แต่จะส่งรูปภาพด้วย ต่อไปนี้คือตัวอย่างเอฟเฟกต์ภาพบนอุปกรณ์ของผู้ใช้
Node.js
const topicName = 'industry-tech';
const message = {
notification: {
title: 'Sparky says hello!'
},
android: {
notification: {
imageUrl: '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,
};
getMessaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
REST
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 เพื่อดูรายละเอียดเพิ่มเติมเกี่ยวกับคีย์ที่มีอยู่ในบล็อกเฉพาะแพลตฟอร์มในเนื้อหาข้อความ
ข้อความแจ้งเตือนพร้อมการคลิกที่เชื่อมโยง
คำขอส่งต่อไปนี้จะส่งชื่อการแจ้งเตือนทั่วไปไปยังทุกแพลตฟอร์ม แต่จะส่งการดำเนินการให้แอปทำเพื่อตอบสนองต่อผู้ใช้ที่ โต้ตอบกับการแจ้งเตือนด้วย ตัวอย่างเอฟเฟกต์ภาพ ในอุปกรณ์ของผู้ใช้
Node.js
const topicName = 'industry-tech';
const message = {
notification: {
title: 'Breaking News....'
},
android: {
notification: {
clickAction: 'news_intent'
}
},
apns: {
payload: {
aps: {
'category': 'INVITE_CATEGORY'
}
}
},
webpush: {
fcmOptions: {
link: 'breakingnews.html'
}
},
topic: topicName,
};
getMessaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
REST
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1