【问题标题】:Unable to send push notification - failed to fetch a valid Google OAuth2 access token无法发送推送通知 - 未能获取有效的 Google OAuth2 访问令牌
【发布时间】:2019-11-19 23:44:12
【问题描述】:

我正在尝试从基于 Web 的客户端向类似客户端发送推送通知。我已经设法在接收端实现了,但是由于以下错误,我无法发送:

通过“凭据”属性提供给 initializeApp() 的凭据实现未能获取有效的 Google OAuth2 访问令牌,并出现以下错误:“获取访问令牌时出错:发出请求时出错:无法获取。错误代码:未定义”。

我的 firebase 初始化文件:

import firebase from "firebase";

var firebaseConfig = {
  apiKey: "AIzaSyBhQV8zHeo7piFalXcA14v6hV*****",
  authDomain: "*****-demo-a82ca.firebaseapp.com",
  databaseURL: "https://*****-demo-a82ca.firebaseio.com",
  projectId: "*****demo-a82ca",
  storageBucket: "",
  messagingSenderId: "52526034576",
  appId: "1:52526034576:web:7b271318a*****"
};

// Initialize Firebase
const firebaseApp = firebase.initializeApp(firebaseConfig);

export default firebaseApp.firestore();
export const firebaseMessaging = firebaseApp.messaging()
firebaseMessaging.initializeApp(firebaseConfig);

以及发送的 js 文件:

import { firebaseMessaging } from "@/firebase/init";

sendNotification() {
  this.broadcastingAnnouncement = true;
  console.log("Selected audience: ", this.selectedAudience);

  var registrationToken =
    "ezdWAWsKo48:APA91bF************"; //Client to receive the notification

  var message = {
    data: {
      title: "Hello World",
      body: "Test message"
    },
    token: registrationToken
  };

firebaseMessaging
    .messaging()
    .send(message)
    .then(response => {
      // Response is a message ID string.
      console.log("Successfully sent notification:", response);
    })
    .catch(error => {
      console.log("Error sending message:", error);
    });
}

这可能是我的问题? (我已经确认我的 api 密钥是正确的)

【问题讨论】:

  • 你能检查你的本地机器时间吗?可能是操作系统时钟未同步
  • 你能从你的服务器检查出站连接吗?错误代码:未定义可能是连接外部服务的错误。
  • 查看这篇文章是否有帮助enter link description here

标签: javascript firebase firebase-cloud-messaging


【解决方案1】:

我不确定,但您似乎正在尝试使用client side SDK 发送推送消息,尽管您应该使用Admin SDK

上面的代码看起来很奇怪:

  1. 你为什么在你firebaseMessaging对象上调用initializeApp(),哪个doesn't have such method
  2. 为什么叫firebaseMessaging.messaging()firebaseMessaging对象已经是Messaging实例了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    相关资源
    最近更新 更多