إرسال صورة في حمولة الإشعارات

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

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

إعداد إضافة خدمة الإشعارات

لإدراج إضافة خدمة، عليك تنفيذ مهام الإعداد المطلوبة لتعديل الإشعارات وعرضها في أسماء نقاط الوصول (APN)، ثم إدراج واجهة برمجة التطبيقات مساعد إضافة 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 على النحو المعروض، يُفعِّل طلب الإرسال هذا إضافة الخدمة على الجهاز المُستلِم للاستلام من معالجة الصورة التي يتم تسليمها في الحمولة.