ส่งรูปภาพในเพย์โหลดการแจ้งเตือน

FCM HTTP v1 API และ การเขียนการแจ้งเตือน รองรับการส่งลิงก์รูปภาพในเพย์โหลดของหน้าจอ สำหรับการดาวน์โหลดรูปภาพไปยังอุปกรณ์หลังการนำส่ง ฟังก์ชันนี้จะรองรับทั้งรูปภาพและวิดีโอสำหรับแอป Apple (โปรดดูขีดจำกัดของขนาดไฟล์ในเอกสารของ Apple)

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

ตั้งค่าส่วนขยายบริการการแจ้งเตือน

หากต้องการเพิ่มส่วนขยายบริการ ให้ดําเนินการตั้งค่าที่จําเป็นสําหรับ การแก้ไขและการนำเสนอการแจ้งเตือน ใน APN แล้วเพิ่ม API ตัวช่วยของส่วนขยาย FCM ใน NotificationService.m โดยเฉพาะอย่างยิ่ง แทนที่จะดำเนินการติดต่อกลับโดยใช้ self.contentHandler(self.bestAttemptContent);, กรอก FIRMessaging extensionHelper ให้สมบูรณ์ ตามที่ปรากฏ:

@interface NotificationService () <NSURLSessionDelegate>
@property(nonatomic) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property(nonatomic) UNMutableNotificationContent *bestAttemptContent;
@end

@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];

    // Modify the notification content here as you wish
    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]",
    self.bestAttemptContent.title];

  // Call FIRMessaging extension helper API.
  [[FIRMessaging extensionHelper] populateNotificationContent:self.bestAttemptContent
                                            withContentHandler:contentHandler];

}
...

สร้างคำขอส่ง

ในคำขอส่งการแจ้งเตือน ให้ตั้งค่า ApnsConfig ต่อไปนี้ ตัวเลือก:

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

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