簡體   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