react-native 错误:[未处理的承诺拒绝:错误:获取 Expo 令牌时遇到错误:TypeError:网络请求失败。]

Posted

技术标签:

【中文标题】react-native 错误:[未处理的承诺拒绝:错误:获取 Expo 令牌时遇到错误:TypeError:网络请求失败。]【英文标题】:react-native error:[Unhandled promise rejection: Error: Error encountered while fetching Expo token: TypeError: Network request failed.] 【发布时间】:2020-08-11 17:39:35 【问题描述】:
Error logs: [Unhandled promise rejection: Error: Error encountered while fetching Expo token: TypeError: Network request failed.]
* [native code]:null in construct
- node_modules\@babel\runtime\helpers\construct.js:19:9 in _construct
- node_modules\@babel\runtime\helpers\wrapNativeSuper.js:26:22 in Wrapper
* [native code]:null in construct
* http://192.168.1.4:19015/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&minify=false&hot=false:107658:321 in _createSuperInternal
- node_modules\@unimodules\react-native-adapter\build\errors\CodedError.js:8:8 in constructor
- node_modules\expo-notifications\build\getExpoPushTokenAsync.js:35:22 in fetch._catch$argument_0
- node_modules\promise\setimmediate\core.js:37:13 in tryCallOne
- node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:135:14 in _callTimer
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:183:16 in _callImmediatesPass
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:446:30 in callImmediates
* [native code]:null in callImmediates
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:396:6 in __callImmediates
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:144:6 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:373:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:143:4 in flushedQueue
* [native code]:null in flushedQueue
* [native code]:null in callFunctionReturnFlushedQueue

我的代码:

  import Constants from "expo-constants";
    import * as Notifications from "expo-notifications";
    import * as Permissions from "expo-permissions";
    import React,  useState, useEffect, useRef  from "react";
    import  MainLayout  from "./src/MainLayout";
    //TODO import fonts
    /*var uuid = require('react-native-uuid');
    console.log(uuid.v4())*/
    
    export default function App() 
      registerForPushNotificationsAsync().then((token) => console.log(token));
      return <MainLayout />;
    
    
    async function registerForPushNotificationsAsync() 
      let token;
      if (Constants.isDevice) 
        const  status: existingStatus  = await Permissions.getAsync(
          Permissions.NOTIFICATIONS
        );
        let finalStatus = existingStatus;
        if (existingStatus !== "granted") 
          const  status  = await Permissions.askAsync(Permissions.NOTIFICATIONS);
          finalStatus = status;
        
        if (finalStatus !== "granted") 
          alert("Failed to get push token for push notification!");
          return;
        
        token = (await Notifications.getExpoPushTokenAsync()).data;
        console.log(token);
       else 
        alert("Must use physical device for Push Notifications");
      
    
      if (Platform.OS === "android") 
        Notifications.setNotificationChannelAsync("default", 
          name: "default",
          importance: Notifications.AndroidImportance.MAX,
          vibrationPattern: [0, 250, 250, 250],
          lightColor: "#FF231F7C",
        );
      
    
      return token;
    

我想实现一个通知推送机制,取本站代码https://docs.expo.io/push-notifications/overview/

【问题讨论】:

【参考方案1】:

您可能需要看一下这个问题:https://github.com/wix/react-native-notifications/issues/440,特别是我在这里留下的评论:https://github.com/wix/react-native-notifications/issues/440#issuecomment-680415269

【讨论】:

以上是关于react-native 错误:[未处理的承诺拒绝:错误:获取 Expo 令牌时遇到错误:TypeError:网络请求失败。]的主要内容,如果未能解决你的问题,请参考以下文章

React Native 未处理的承诺拒绝 | React-Native,异步,

设置firebase时出错,可能会出现未处理的承诺拒绝

使用 axios 时可能出现未处理的 Promise 拒绝、网络错误

反应本机中未处理的承诺拒绝错误?

未处理的承诺拒绝:错误:URL 格式错误,无法解析

*可能的未处理承诺拒绝(id:0):类型错误:未定义不是对象(评估'result.cancelled')云图像上传