【问题标题】:Expo push notifications not working on Android standalone app世博会推送通知不适用于 Android 独立应用程序
【发布时间】:2021-04-10 16:00:49
【问题描述】:

我正在开发一个世博会应用程序,我已经添加了世博会推送通知,这适用于 iOS,但不适用于 Android。问题是由 getExpoPushTokenAsync() 引起的,因为它没有解决。

我正在使用 Expo 版本 40.0.0 并使用以下代码获取通知令牌:

export async function registerForPushNotificationsAsync(t) {
  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(t('no_access_push_notifications'));
      return;
    }
    const token = await Notifications.getExpoPushTokenAsync();
    return token;

    // this.setState({ expoPushToken: token });
  } else {
    console.log("No physical device");
  }

  if (Platform.OS === 'android') {
    Notifications.setNotificationChannelAsync('default', {
      name: 'default',
      importance: Notifications.AndroidImportance.MAX,
      vibrationPattern: [0, 250, 250, 250],
      lightColor: '#FF231F7C',
    });
  }
};

我根据 Expo docs for Android 在 Firebase 中创建了一个新项目,并将 google-services.json 复制到我的应用程序的根目录,并将其链接到我的 app.json 中

{
  "project_info": {
    "project_number": "xxxxxxxxx",
    "project_id": "xxxxxxxxx",
    "storage_bucket": "xxxxxxxxx"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "xxxxxxxxx",
        "android_client_info": {
          "package_name": "xxxxxxxxx"
        }
      },
      "oauth_client": [
        {
          "client_id": "xxxxxxxxx",
          "client_type": 3
        }
      ],
      "api_key": [
        {
          "current_key": "xxxxxxxxx"
        }
      ],
      "services": {
        "appinvite_service": {
          "other_platform_oauth_client": [
            {
              "client_id": "xxxxxxxxx",
              "client_type": 3
            }
          ]
        }
      }
    }
  ],
  "configuration_version": "1"
}

我的 app.json 包含以下代码:

"android": {
      "package": "xxxxx",
      "versionCode": 4050,
      "googleServicesFile": "./google-services.json",
      "config": {
        "googleSignIn": {
          "apiKey": "xxxxx"
        }
      },
      "useNextNotificationsApi": true
    },

对此有什么想法吗?

【问题讨论】:

  • 您是在设备上还是在 Android 模拟器上进行测试?
  • 在安卓设备上
  • 你找到解决这个问题的方法了吗?
  • 你上传你的服务器密钥了吗? docs.expo.io/push-notifications/using-fcm
  • @Timo 是的,我也上传了密钥,但仍然无法使用

标签: javascript firebase react-native ecmascript-6 expo


【解决方案1】:

Notifications.getExpoPushTokenAsync() 返回一个对象,而不是一个令牌。

Returns a Promise that resolves to an object with the following fields:

    type (string) -- Always expo.
    data (string) -- The push token as a string.

你应该使用这样的东西:

await Notifications.getExpoPushTokenAsync().then(({data}) => this.setState({expoPushToken: data}) )
console.log(this.state.expoPushToken);

在 Google Cloud Platform API 凭据控制台中设置正确的“API 限制”非常重要。为 Android Token 设置类型 Android 并添加 SHA1 指纹。您需要获取 expo android keystore 并将其从 Java 发送到 keytool 以查看指纹。

【讨论】:

    猜你喜欢
    • 2021-06-08
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 2020-02-09
    • 2021-07-07
    • 2021-11-14
    相关资源
    最近更新 更多