首页 文章

使用React Native上的Expo发送多个推送通知

提问于
浏览
1

我正在尝试为我的应用设置推送通知 . 我正在使用Expo push-functionality

它到目前为止工作得很好,但现在我想向多人发送推送通知 . 在那种情况下,Expo告诉我像这样发送HTTP-Body:

[{
  "to": "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
  "sound": "default",
  "body": "Hello world!"
}, {
  "to": "ExponentPushToken[yyyyyyyyyyyyyyyyyyyyyy]",
  "badge": 1,
  "body": "You've got mail"
}]

我在这里苦苦挣扎 . 在我的应用程序中,我将其设置为:

fetch('https://exp.host/--/api/v2/push/send', {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Accept-encoding': 'gzip, deflate',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({tokenArray})
    });

tokenArray来自firebase调用:

firebase.database().ref(`users/${user.uid}/`).once('value', snapshot => {
          const token = snapshot.val().token;
            tokenArray.push({
              to: token,
              title: 'Check out and Entvag!',
              body: `Help ${creator} to decide!`
            })

我用令牌获取数组并将其发送到HTTP

这是我从服务器获得的响应(错误):

Response: Response {
  "_bodyInit": "{\"errors\":[{\"code\":\"API_ERROR\",\"message\":\"child \\\"to\\\" fails because [\\\"to\\\" is required], \\\"value\\\" must be an array.\"}]}",
  "_bodyText": "{\"errors\":[{\"code\":\"API_ERROR\",\"message\":\"child \\\"to\\\" fails because [\\\"to\\\" is required], \\\"value\\\" must be an array.\"}]}",
  "headers": Headers {
    "map": Object {
      "cache-control": Array [
        "public, max-age=0",
      ],
      "content-length": Array [
        "122",
      ],
      "content-type": Array [
        "application/json; charset=utf-8",
      ],
      "date": Array [
        "Wed, 31 Jan 2018 02:19:39 GMT",
      ],
      "server": Array [
        "nginx/1.11.3",
      ],
      "strict-transport-security": Array [
        "max-age=15724800; preload",
      ],
      "vary": Array [
        "Accept-Encoding, Origin",
      ],
      "x-content-type-options": Array [
        "nosniff",
      ],
    },
  },
  "ok": false,
  "status": 400,
  "statusText": undefined,
  "type": "default",
  "url": "https://exp.host/--/api/v2/push/send",
}

我认为它与数组结构的方式有关(或者由JSON.stringify构成???)在console.log上,Array(在JSON.stringify之后)看起来像这样:

{"tokenArray":[{"to":"ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]","title":"Check out and Entvag!","body":"Help  to decide: thisVal or thatVal}"}]}

我如何构建它看起来像世博会想要的阵列(第一个代码片段)?或者你们有任何其他想法我做错了什么?谢谢大家!

1 回答

  • 0

    我很久以前就解决了这个错误,但忘了在Stackoverflow上更新它 . 错误在这里:

    fetch('https://exp.host/--/api/v2/push/send', {
          method: 'POST',
          headers: {
            Accept: 'application/json',
            'Accept-encoding': 'gzip, deflate',
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({tokenArray})
        });
    

    我通过使用{}来传递tokenArray作为对象 . 这是工作代码段:

    fetch('https://exp.host/--/api/v2/push/send', {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Accept-encoding': 'gzip, deflate',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(tokenArray)
    });
    

相关问题