أرسل صورة في حمولة الإخطار

تدعم واجهة برمجة تطبيقات FCM HTTP v1 ومؤلف الإشعارات إرسال روابط الصور في حمولة إشعار العرض، لتنزيل الصورة على الجهاز بعد التسليم. تدعم هذه الوظيفة كلاً من الصور ومقاطع الفيديو لتطبيقات Apple (راجع وثائق Apple لمعرفة حدود حجم الملف).

لتتمكن من تلقي صور الإشعارات والتعامل معها في تطبيق Apple، يجب عليك إضافة ملحق خدمة الإشعارات . يسمح ملحق خدمة الإشعارات لتطبيقك بالتعامل مع الصورة التي يتم تسليمها في حمولة FCM قبل عرض الإشعار للمستخدم النهائي.

قم بإعداد ملحق خدمة الإعلام

لإضافة ملحق خدمة، قم بتنفيذ مهام الإعداد المطلوبة لتعديل وتقديم الإشعارات في APNs، ثم قم بإضافة واجهة برمجة تطبيقات مساعد ملحق 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);
  });

استراحة

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 كما هو موضح، يمكّن طلب الإرسال هذا ملحق الخدمة على العميل المتلقي من التعامل مع الصورة المسلمة في الحمولة.