ปรับแต่งข้อความในแพลตฟอร์มต่างๆ

Firebase Admin SDKFirebase Admin SDK และ FCM v1 HTTP API ช่วยให้คำขอข้อความของคุณตั้งค่า ช่องทั้งหมดที่มีอยู่ใน message ออบเจ็กต์ได้ ซึ่งรวมถึง

  • ชุดช่องทั่วไปที่อินสแตนซ์ของแอป ทั้งหมด ที่ได้รับข้อความจะตีความ
  • ชุดช่องที่เฉพาะเจาะจงแพลตฟอร์ม เช่น AndroidConfig และ WebpushConfig ซึ่งอินสแตนซ์ของแอปที่ทำงานบนแพลตฟอร์มที่ระบุเท่านั้นที่จะตีความ

บล็อกที่เฉพาะเจาะจงแพลตฟอร์มช่วยให้คุณปรับแต่งข้อความสำหรับแพลตฟอร์มต่างๆ ได้อย่างยืดหยุ่นเพื่อให้มั่นใจว่าระบบจะจัดการข้อความอย่างถูกต้องเมื่อได้รับ แบ็กเอนด์ของ FCM จะพิจารณาพารามิเตอร์ทั้งหมดที่ระบุและปรับแต่งข้อความสำหรับแต่ละแพลตฟอร์ม

กรณีที่ควรใช้ช่องทั่วไป

ใช้ช่องทั่วไปในกรณีต่อไปนี้

  • ส่งช่องไปยังแพลตฟอร์มใดก็ได้
  • ส่งข้อความไปยังหัวข้อ

อินสแตนซ์ของแอปทั้งหมดไม่ว่าแพลตฟอร์มใดจะตีความช่องทั่วไปต่อไปนี้ได้

กรณีที่ควรใช้ช่องที่เฉพาะเจาะจงแพลตฟอร์ม

ใช้ช่องที่เฉพาะเจาะจงแพลตฟอร์มในกรณีต่อไปนี้

  • ส่งช่องไปยังแพลตฟอร์มที่เฉพาะเจาะจงเท่านั้น
  • ส่งช่องที่เฉพาะเจาะจงแพลตฟอร์ม นอกเหนือจาก ช่องทั่วไป

ใช้ช่องที่เฉพาะเจาะจงแพลตฟอร์มทุกครั้งที่ต้องการส่งค่าไปยังแพลตฟอร์มที่เฉพาะเจาะจงเท่านั้น เช่น หากต้องการส่งการแจ้งเตือนไปยังแพลตฟอร์ม Apple และเว็บเท่านั้น แต่ไม่ส่งไปยัง Android คุณต้องใช้ช่อง 2 ชุดแยกกัน ชุดหนึ่งสำหรับ Apple และอีกชุดหนึ่งสำหรับเว็บ

เมื่อส่งข้อความที่มีตัวเลือกการนำส่งที่เฉพาะเจาะจง ให้ใช้ช่องที่เฉพาะเจาะจงแพลตฟอร์มเพื่อตั้งค่าตัวเลือกเหล่านั้น คุณระบุค่าที่แตกต่างกันต่อแพลตฟอร์มได้หากต้องการ อย่างไรก็ตาม แม้ว่าคุณจะต้องการตั้งค่าที่เหมือนกันในทุกแพลตฟอร์ม คุณก็ต้องใช้ช่องที่เฉพาะเจาะจงแพลตฟอร์ม เนื่องจากแต่ละแพลตฟอร์มอาจตีความค่าแตกต่างกันเล็กน้อย เช่น Time to Live (TTL) จะตั้งค่าใน Android เป็นเวลาหมดอายุเป็นวินาที ขณะที่ใน Apple จะตั้งค่าเป็น วันที่ หมดอายุ

ข้อความแจ้งเตือนที่มีตัวเลือกการนำส่งที่เฉพาะเจาะจงแพลตฟอร์ม

คำขอส่ง HTTP v1 API ต่อไปนี้จะส่งชื่อและเนื้อหาการแจ้งเตือนทั่วไปไปยังทุกแพลตฟอร์ม แต่ยังส่งการลบล้างที่เฉพาะเจาะจงแพลตฟอร์มด้วย โดยเฉพาะอย่างยิ่ง คำขอจะดำเนินการต่อไปนี้

  • ตั้งค่า Time to Live (TTL) นานสำหรับแพลตฟอร์ม 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 และรูปภาพต้องเป็นทรัศยากรไอคอนแบบ Drawable ที่อยู่ในแอป Android

ตัวอย่างเอฟเฟกต์ภาพในอุปกรณ์ของผู้ใช้

ภาพวาดอย่างง่ายของอุปกรณ์ 2 เครื่อง โดยเครื่องหนึ่งแสดงไอคอนและสีที่กำหนดเอง

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 เกี่ยวกับคีย์ที่มีอยู่ในบล็อกที่เฉพาะเจาะจงแพลตฟอร์มใน เนื้อหาข้อความ

ข้อความแจ้งเตือนที่มีรูปภาพที่กำหนดเอง

ข้อควรทราบ

ในคำขอส่งการแจ้งเตือน ให้ตั้งค่าตัวเลือกต่อไปนี้เพื่อเปิดใช้ไคลเอ็นต์ที่รับให้จัดการรูปภาพที่ส่งในเพย์โหลด

  • สำหรับ Android ให้ตั้งค่าตัวเลือก AndroidConfig ต่อไปนี้:
    • notification.image ที่มี URL ของรูปภาพ
  • สำหรับ iOS ให้ตั้งค่าตัวเลือก ApnsConfig ต่อไปนี้:
    • fcm_options.image ที่มี URL ของรูปภาพ Apple กำหนดให้ URL ของรูปภาพมีนามสกุลไฟล์ที่ถูกต้องเพื่อระบุประเภททรัพยากรได้อย่างถูกต้อง
    • headers({ "mutable-content": 1})

คำขอส่งต่อไปนี้จะส่งชื่อการแจ้งเตือนทั่วไปไปยังทุกแพลตฟอร์ม แต่ยังส่งรูปภาพด้วย ตัวอย่างเอฟเฟกต์ภาพในอุปกรณ์ของผู้ใช้

ภาพวาดอย่างง่ายของรูปภาพในการแจ้งเตือนที่แสดง

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