使用Firebase推送通知

时间:2019-08-27 06:08:07

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

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

这些是我用来从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 Tester在线进行测试时,出现了此错误401 https://pushtry.com/

请问我有什么方法可以获取响应本机android应用程序的推送通知?

2 个答案:

答案 0 :(得分:0)

您可以使用邮递员。向此网址“ 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是您的令牌。此外,您还需要使用密钥Authorization为请求设置标头,该标头的值来自firebase控制台。查找可以检查此https://developer.clevertap.com/docs/find-your-fcm-sender-id-fcm-server-api-key

答案 1 :(得分:0)

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