簡體   English   中英

條件數據字段 firebase 雲 function

[英]Conditional data field firebase cloud function

我正在嘗試將條件字段添加到 firebase 雲函數中的 fcm 通知 object 的數據字段。 到目前為止,這是我嘗試過的:

const message = <admin.messaging.Message>{
      notification: {
        title: title,
        body: body,
      },
      android: {
        priority: "high",
      },
      data: {
        "type": notificationType,
        ...notificationType == notificationTypes.liveRoomAccepted &&
        {"liveroom-id": liveroomId},
      },
      apns: {
        headers: {
          "apns-priority": "10",
        },
        payload: {
          aps: {
            "contentAvailable": true,
          },
        },
      },
      token: token,
    };

但我不斷收到這樣的錯誤:FirebaseMessagingError:數據必須只包含字符串值。

我還嘗試打印消息 object 並且數據字段只有“類型”字段。 請問正確的語法是什么?

問題是這段代碼的計算結果不是字符串值:

...notificationType == notificationTypes.liveRoomAccepted &&
        {"liveroom-id": liveroomId}

聽起來您想為特定通知類型的data添加一個額外的屬性。 我能想到的最簡單的方法是:

data: (notificationType == notificationTypes.liveRoomAccepted) 
? {
    "type": notificationType,
    "liveroom-id": liveroomId,
  }
: {
    "type": notificationType,
  }

所以我們在這里有一個用於類型檢查的三元表達式,然后是兩個單獨的對象:一個用於每個結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM