繁体   English   中英

如何在 IOS 上接收推送通知?

[英]how to receive a push notification on IOS react native?

我正在尝试将 firebase 的 rest API 与本机反应一起使用。 我正在尝试使用通知请求,但我的提取不起作用。

我有这样的错误:

可能未处理的 Promise 拒绝(id:10):语法错误:JSON 解析错误:意外的 EOF

我也控制台记录我得到的响应:{“_bodyBlob”:{“_data”:{“__collector”:[Object],“blobId”:“1223679F-6B8F-4104-9085-060585EDF71E”,“name”: “1.1”,“偏移”:0,“大小”:0,“类型”:“text/html”}},“_bodyInit”:{“_data”:{“__collector”:[对象],“blobId”: “1223679F-6B8F-4104-9085-060585EDF71E”,“名称”:“1.1”,“偏移”:0,“大小”:0,“类型”:“文本/html”}},“bodyUsed”:假, “标题”:{“地图”:{“alt-svc”:“h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443 "; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43" ", "content-length": "0", "content-type": "text/html", "date": "Tue, 06 Apr 2021 14:20:39 GMT", "server": "scaffolding on HTTPServer2 ", "x-content-type-options": "nosniff", "x-frame-options": "SAMEORIGIN", "x-xss-protection": "0"}}, "ok": false, "status ": 404, "statusText": 未定义, "type": "default", "url": "https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send%20HTTP/1.1"}

这是我的获取,我在我的 app.js 的 componentdidmount 上给他打电话

Notif = async () => {
    const headers = new Headers({
      'Content-type': 'application/json',
      Authorization: 'Bearer ' + (await AsyncStorage.getItem('token')),
      
      message:{
        token:"ff_Zolz1s0mmgrovad27JG:APA91bHlV5bAXyNHI3aWGyjltdgmJP8mmGBlEC0mPBA72IIJGqoliH4gm1rCQp0szQ5JypKxNhcWcKb7JrOwUTZDmaCB02y4dS553WVDdsxbWuLeK7cqoMjTRjFtFfdMb8bVGxO65BTq",
        notification:{
          body:"This is an FCM notification message!",
          title:"FCM Message"
        }
     }
     
    });
    const options = {
      method: 'POST',
      headers: headers,
    };

    fetch('https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1', options)
      .then((response) => {
        console.log(response);
        return response.json();
      })
      .then(
        (err) => {
          console.log(err);
        },
      );
  };

首先,您正在错误地处理 fetch 请求,因为它缺少 catch 子句。 其次,检查您在请求中发送的 url,因为它似乎没有正确格式化为字符串。

旁注您可以等待获取 function 因为您已经使 Notfi function 异步并用 try/catch 块包围它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM