【问题标题】:Expo Push Notification not working on standalone app世博推送通知不适用于独立应用程序
【发布时间】:2021-06-08 15:06:18
【问题描述】:

我使用 expo 推送系统设置了推送通知,并且在通过 ExpoGo 使用该应用程序时效果很好。但是在Play商店安装的应用程序中,它不起作用。调试应用程序我发现

token = (await Notifications.getExpoPushTokenAsync()).data;

没有返回任何内容,因为它之后的警报从未运行。

async function registerForPushNotificationsAsync() {
    let token;
    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;
    }
    alert('1')
    token = (await Notifications.getExpoPushTokenAsync()).data;
    alert('2')
    if (Platform.OS === 'android') {
      Notifications.setNotificationChannelAsync('default', {
        name: 'default',
        importance: Notifications.AndroidImportance.MAX,
        vibrationPattern: [0, 250, 250, 250],
        lightColor: '#FF231F7C',
      });
    }

    //sending the token to my api
    api.put('xxx', {
      notification_token: token,
    });
  }

我该怎么办?我有什么遗漏吗?我只是使用了文档中给出的代码https://docs.expo.io/push-notifications/overview/

【问题讨论】:

    标签: android reactjs react-native expo push


    【解决方案1】:

    我找到了解决办法。

    关键是,在设置推送通知的文档中,出于某种原因,他们没有提到有必要设置一个 firebase 帐户以在独立应用程序中推送通知,即使我使用的是 expo 系统。

    在此链接https://github.com/expo/expo/issues/9770中发现了解决方案

    并使用此链接 https://docs.expo.io/push-notifications/using-fcm/ 设置 FCM

    【讨论】:

    • 感谢@Antony Leme 的解决方案,我按照文档完成了配置,但您能告诉我您从哪里发送通知吗?
    • 不知道我有没有收到你的问题,但我在 Laravel 有一个后端,我向 expo 服务器发送请求以发送通知。
    • 感谢您的回复。你在 Laravel 中使用了哪个依赖项?
    • 这就是我的做法pastebin.com/KDcun4hg
    • 谢谢@Antony Leme!因此,无需使用 FCM 即可实现独立 IOS 和 Android 的推送通知
    【解决方案2】:

    expo push notification documentation page 不是很清楚地提到,推送通知需要几个步骤才能在独立的 android/ios 应用程序中工作。虽然它在文档中提到页面底部的“下一步”,但从第一段来看,并不清楚它们是否将 expo 应用程序通知与独立的 android/ios 应用程序通知区分开来。相反,它觉得 Expo 会处理所有事情,就好像独立应用程序不需要更多步骤一样。这当然只适用于 Expo 应用程序。但是,当我们进入 Next Steps 页面时,我们会在“凭据”部分找到最重要的信息。

    这就是它适用于 Android 独立应用程序的方式(我已经这样做了)。我们需要设置我们自己的 Firebase 项目,正如这里所说的 Using FCM for Push Notifications。如果您使用 Expo 服务器发送推送通知,请确保按照“上传服务器凭据”的步骤进行操作。您将看到在您的 Expo 凭证中添加了 FCM 服务器密钥。

    这不应该花费太多时间,但对我来说,设置这些额外步骤需要 1 个小时左右,因为我是第一次学习这些。

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 1970-01-01
      • 2020-02-09
      • 2021-03-02
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      相关资源
      最近更新 更多