【问题标题】:Push notifications with firebase使用 firebase 推送通知
【发布时间】:2019-08-27 06:08:07
【问题描述】:

当我使用从 firebase 获得的令牌进行测试时出现此错误 错误 - 请求的身份验证(服务器)密钥包含无效或格式错误的 FCM-Token(a.k.a. IID-Token)

这些是我用来从 firebase 获取令牌的代码。

async getToken() {
  let fcmToken = await AsyncStorage.getItem('fcmToken');
  console.log("before fcmToken: ", fcmToken);
  if (!fcmToken) {
    fcmToken = await firebase.messaging().getToken();
    if (fcmToken) {
      console.log("after fcmToken: ", fcmToken);
      await AsyncStorage.setItem('fcmToken', fcmToken);
    }
  }
}

async requestPermission() {
  firebase.messaging().requestPermission()
    .then(() => {
      this.getToken();
    })
    .catch(error => {
      console.log('permission rejected');
    });
}

async checkPermission() {
  firebase.messaging().hasPermission()
    .then(enabled => {
      if (enabled) {
        console.log("Permission granted");
        this.getToken();
      } else {
        console.log("Request Permission");
        this.requestPermission();
      }
    });
}`

但是当我使用 APNS 和 GCM 在线测试仪进行测试时,我收到了这个错误 401 https://pushtry.com/

请问我有什么方法可以获取 react native android 应用程序的推送通知吗?

【问题讨论】:

    标签: firebase react-native firebase-cloud-messaging


    【解决方案1】:

    您可以使用邮递员。向此网址“https://fcm.googleapis.com/fcm/send”发出帖子请求。 你需要像这样发送一个对象:

    {
        "data":
        {
            "title": "your title",
          "body": "your body"
        },
        "notification": {
             "title": "your title",
          "body": "your body"
        },
     "registration_ids" : ["czf_nv_t1JA:APA91bGOyY3lTSG9b7Nr71xVo_Xn5RrxOIOwVfnKDv2OBanQjx1eQQyqdM3PFOd1Pjapm_DWn1R327iDyEwEeXjflJ3DyaGFF4iXmqot-OsyDt-Iz99Lu3MZZTvOSFIPiYgiaGHP5ByO"]
    }
    

    registration_ids 是您的令牌。此外,您需要为您的请求设置一个带有密钥授权的标头,它的值来自 firebase 控制台。发现您可以检查此https://developer.clevertap.com/docs/find-your-fcm-sender-id-fcm-server-api-key

    【讨论】:

    • 我已经用邮递员测试并得到成功消息。但我仍然无法在我的 android 模拟器上收到通知。 {“multicast_id”:4969739746123198930,“成功”:1,“失败”:0,“canonical_ids”:0,“结果”:[{“message_id”:“0:1566968957667332%3b9a518e3b9a518e”}]}
    • 你在真机上测试过吗?
    • 是的。我也在真机上测试过。
    【解决方案2】:

    确保授权值与您的 Firebase 云消息传递服务器密钥一致。 参考链接:-https://www.djamware.com/post/5c6ccd1f80aca754f7a9d1ec/push-notification-using-ionic-4-and-firebase-cloud-messaging

    【讨论】:

    • 我已经用邮递员测试并得到成功消息。但我仍然无法在我的 android 模拟器上收到通知。 {“multicast_id”:4969739746123198930,“成功”:1,“失败”:0,“canonical_ids”:0,“结果”:[{“message_id”:“0:1566968957667332%3b9a518e3b9a518e”}]}
    猜你喜欢
    • 1970-01-01
    • 2018-09-17
    • 2017-03-05
    • 2018-09-19
    • 1970-01-01
    • 2018-02-28
    • 2019-01-30
    • 2013-03-05
    相关资源
    最近更新 更多