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

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})

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

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

โหนด 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);
  });

พักผ่อน

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 ตามที่แสดง คำขอส่งนี้จะทำให้ส่วนขยายบริการบนไคลเอ็นต์ที่รับสามารถจัดการรูปภาพที่จัดส่งในเพย์โหลดได้