无法发送推送通知-无法获取有效的Google OAuth2访问令牌

时间:2019-07-10 10:52:17

标签: javascript firebase firebase-cloud-messaging

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

通过“ credential”属性提供给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密钥正确)

1 个答案:

答案 0 :(得分:0)

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

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

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