首页 > 解决方案 > 使用托管工作流的 Expo 推送通知不起作用

问题描述

我不想在 expo 中创建我的新应用程序,并通过托管工作流程做出本机反应。我有一个付费的苹果开发者帐户,我的所有凭据都上传到我的 expo 帐户。iOS 和 Android 的构建运行良好。特别是我的 Apple Push Notification Service 密钥也已上传到 expo 帐户。但是当我尝试使用 Expo Notification Tool 发送通知时,我的手机没有收到任何信息。但是,如果我尝试使用 Expo Go 启动我的应用程序,它就可以工作。因此,在生产模式下,我的通知不起作用。但是我成功获得了我的 Expo Push 令牌,并且我发送通知的权限还可以。我按照此文档设置我的通知:https://docs.expo.dev/push-notifications/push-notifications-setup/ 这是我在 App.js 中的代码:

    async registerForPushNotificationsAsync() {
        let token;
        if (Constants.isDevice) {
          const { status: existingStatus } = await Notifications.getPermissionsAsync();
          let finalStatus = existingStatus;
          if (existingStatus !== 'granted') {
            const { status } = await Notifications.requestPermissionsAsync();
            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;
      }
    
      _handleNotification = async notification => {
        console.log(notification);
        if (notification.remote) {
          Vibration.vibrate();                                                  
          const notificationId = Notifications.presentLocalNotificationAsync({      
            title: "Follow @technoplato",  
            body: "To learn yourself goodly (also follow PewDiePie)",                                             
            ios: { _displayInForeground: true }                                
          });                                                                       
        } 
        this.setState({ notification: notification });
      };
    
      _handleNotificationResponse = response => {
        console.log(response);
      };
    
      componentDidMount() {
        this.registerForPushNotificationsAsync().then(token => {
          this.setState({expoPushToken: token})
          alert('Push Token: ' + token);
        });
    
        Notifications.addNotificationReceivedListener(this._handleNotification);
        
             Notifications.addNotificationResponseReceivedListener(this._handleNotificationResponse);
      }

这是我的 app.json :

{
  "expo": {
    "name": "Spirit",
    "slug": "spirit-app",
    "version": "1.0.2",
    "orientation": "portrait",
    "icon": "./assets/icon.png",
    "splash": {
      "image": "./assets/splash.png",
      "resizeMode": "contain",
      "backgroundColor": "#ffffff"
    },
    "updates": {
      "fallbackToCacheTimeout": 0
    },
    "assetBundlePatterns": [
      "**/*"
    ],
    "ios": {
      "supportsTablet": false,
      "bundleIdentifier": "com.ml-dev.spirit.app"
    },
    "android": {
      "adaptiveIcon": {
        "foregroundImage": "./assets/adaptive-icon.png",
        "backgroundColor": "#FFFFFF"
      },
      "package": "com.ml.dev.spirit.app"
    },
    "web": {
      "favicon": "./assets/favicon.png"
    }
  }
}

你有什么想法导致这个问题吗?

标签: react-nativepush-notificationnotificationsexpoapple-push-notifications

解决方案


推荐阅读