अधिसूचना पेलोड में एक छवि भेजें

एफसीएम HTTP v1 एपीआई और नोटिफिकेशन कंपोजर डिलीवरी के बाद डिवाइस पर इमेज डाउनलोड के लिए डिस्प्ले नोटिफिकेशन के पेलोड में इमेज लिंक भेजने का समर्थन करते हैं। यह कार्यक्षमता Apple ऐप्स के लिए छवियों और वीडियो दोनों का समर्थन करती है (फ़ाइल आकार सीमा के लिए Apple दस्तावेज़ देखें)।

Apple ऐप में अधिसूचना छवियों को प्राप्त करने और प्रबंधित करने में सक्षम होने के लिए, आपको एक अधिसूचना सेवा एक्सटेंशन जोड़ना होगा। अधिसूचना सेवा एक्सटेंशन आपके ऐप को अंतिम उपयोगकर्ता को अधिसूचना प्रदर्शित करने से पहले एफसीएम पेलोड में वितरित छवि को संभालने की अनुमति देता है।

अधिसूचना सेवा एक्सटेंशन सेट करें

सेवा एक्सटेंशन जोड़ने के लिए, एपीएन में अधिसूचनाओं को संशोधित करने और प्रस्तुत करने के लिए आवश्यक सेटअप कार्य करें, और फिर 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})

निम्नलिखित उदाहरण भेजें अनुरोध सभी प्लेटफ़ॉर्म पर एक सामान्य अधिसूचना शीर्षक भेजता है, लेकिन यह एक छवि भी भेजता है। यहां उपयोगकर्ता के डिवाइस पर दृश्य प्रभाव का एक अनुमान दिया गया है:

एक प्रदर्शन अधिसूचना में एक छवि का सरल चित्रण

नोड.जे.एस

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 सेट के साथ, यह भेजने का अनुरोध प्राप्तकर्ता क्लाइंट पर पेलोड में वितरित छवि को संभालने के लिए सेवा विस्तार को सक्षम बनाता है।