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

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