【问题标题】:Send Expo Push Notification to an Array of Token向令牌数组发送 Expo 推送通知
【发布时间】:2025-12-02 02:05:01
【问题描述】:

我正在尝试使用来自 api 的令牌数组向多个设备发送推送通知。当我只发送到一个对象时,推送通知会正确发送,但是当我通过一组对象发送时,它会在请求中给出错误 400。

//assembling the array of tokens coming from the api
let tokenArray = [];
this.dataObj.notification_tokens.map((item) => {
  tokenArray.push({
    to: item.push_token,
    sound: "default",
    title: this.dataObj.push_title,
    body: this.dataObj.push_message,
  });
});

//passing the array as a parameter of the Expo Push Notification request. I receive 400 error
 await fetch("https://exp.host/--/api/v2/push/send", {
  mode: "no-cors",
  method: "POST",
  headers: {
    Accept: "application/json",
    "Accept-encoding": "gzip, deflate",
    "Content-Type": "application/json",
  },
  body: JSON.stringify(tokenArray),
}).then((res) => {
  if (res) {
    this.dataObj.push_title = "";
    this.dataObj.push_message = "";
    this.goSuccess("Push Notification sending!");
  }
});

如果我将单个对象作为Expo请求的参数传递,则推送通知正常发送

【问题讨论】:

    标签: javascript vue.js push-notification expo


    【解决方案1】:

    我找不到使用前端的解决方案,所以我创建了一个端点来通过后端发送推送。所以后端负责所有的批量运输。

    const dataObj = {
      sound: "default",
      title: this.dataObj.push_title,
      body: this.dataObj.push_message,
    };
    await this.$http
      .post(`/push/send-notification`, dataObj)
      .then((response) => {
        if (!response.data.success) {
          this.goError(response.data.message);
        } else {
          this.goSuccess(response.data.message);
          this.dataObj.push_title = "";
          this.dataObj.push_message = "";
        }
      })
      .catch(function (error) {
        console.log(error);
      });

    【讨论】: