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

Posted

技术标签:

【中文标题】如何在 IOS react native 上接收推送通知?【英文标题】:how to receive a push notification on IOS react native? 【发布时间】:2021-04-06 14:23:29 【问题描述】:

我正在尝试将 firebase 的其余 API 与 react native 一起使用。我正在尝试使用通知请求,但我的 fetch 不起作用。

我有这样的错误:

可能的未处理承诺拒绝(id:10): SyntaxError: JSON Parse error: Unexpected EOF

我也 console.log 我得到的响应: “_bodyBlob”:“_data”:“__collector”:[对象],“blobId”:“1223679F-6B8F-4104-9085-060585EDF71E”,“名称”:“1.1”,“偏移量”:0,“大小”:0,“类型”:“文本/html”,“_bodyInit”:“_data”:“__collector”:[对象],“blobId”:“1223679F-6B8F-4104-9085-060585EDF71E” , "name": "1.1", "offset": 0, "size": 0, "type": "text/html", "bodyUsed": false, "headers": "map": " 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"", "内容长度": "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”:未定义,“类型”:“默认”,“网址”:“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);
        ,
      );
  ;

【问题讨论】:

【参考方案1】:

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

附注您可以等待 fetch 函数,因为您已经使 Notfi 函数异步并用 try/catch 块包围它。

【讨论】:

以上是关于如何在 IOS react native 上接收推送通知?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 ios 中单击通知时接收数据,当应用程序被杀死时,(react-native-fcm 模块在 IOS 中获取推送通知)

无法在 ios 中接收来自 react-native-firebase 的通知消息

react-native-navigation ios - 将 UIViewController 推送到导航堆栈

iOS 无法在 react-native-firebase 中接收通知

React Native Geolocation 在 IOS 上无法正常工作

React Native Push Notification - react-native-fcm - ios无法构建