sendPushNotificationsAsync的“ to”必须是字符串,“ to”必须是数组错误

时间:2019-12-10 05:37:45

标签: javascript reactjs react-native push-notification expo

我目前正在使用expo-server-sdk发送多个推送通知,但出现以下错误:

  

{错误:“值”必须是一个对象,位置0处的“值”失败   因为[child to失败,因为[to必须是一个字符串,所以to必须是   数组]]。

有效的推送令牌被传递给expo.chunkPushNotifications,但是当将块传递给expo.sendPushNotificationsAsync(chunk)时,我得到了上面提到的错误消息:

(async () => {
  for (let chunk of chunks) {
    try {
      let ticketChunk = await expo.sendPushNotificationsAsync(chunk);
      tickets.push(...ticketChunk);
    } catch (error) {
      console.error(error);
    }
  }
})();

该块具有以下形式:

[ { to:
     { notificationToken: 'ExponentPushToken[some_push_token]' },
    sound: 'default',
    body: 'This is a test notification',
    data: { withSome: 'data' } },
]

这意味着这些块位于对象数组的数组中:

[ [ { to: [Object],
      sound: 'default',
      body: 'This is a test notification',
      data: [Object] },
]]

1 个答案:

答案 0 :(得分:1)

根据文档,to必须是stringstring[],并且您要在此处发送object

尝试将有效负载更改为

    [
        {
            to: 'ExponentPushToken[some_push_token]',
            sound: 'default',
            body: 'This is a test notification',
            data: { withSome: 'data' },
        },
    ]