在通知負載中發送圖像

FCM HTTP v1 API 和通知編輯器支援在顯示通知的負載中發送圖像鏈接,以便在傳送後將圖像下載到設備。通知圖像的大小限制為 1MB,否則會受到本機 Android圖像支援的限制。

建構發送請求

在通知發送請求中,設定以下AndroidConfig選項:

  • notification.image包含圖像 URL

以下範例發送請求向所有平台發送通用通知標題,但它還發送圖像。以下是使用者裝置上視覺效果的近似值:

在顯示通知中簡單繪製圖像

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 參考文件

透過如圖所示的notification設置,此發送請求使接收客戶端能夠處理有效負載中傳遞的影像。