无法在Angular上生成FCM令牌

时间:2019-05-04 22:02:16

标签: angular firebase

我无法获得FCM令牌。 SW已成功注册,也已授予许可以进行通知。我得到了“授予权限”日志,但是FCM承诺似乎未返回任何内容。

export class MessagingService {
  messaging = firebase.messaging()
  currentMessage = new BehaviorSubject(null)

  constructor(private db: AngularFireDatabase, private afMessaging: AngularFireMessaging) {
    this.messaging.usePublicVapidKey("......")
  }
  updateToken(token) {
    console.log(!!!localStorage.getItem("uid"))
    if (!!!localStorage.getItem("uid")) {
      const data = { [localStorage.getItem("uid")]: token }
      this.db.object('fcmTokens/').update(data)
    }
  }

  getPermission() {
    this.afMessaging.requestPermission
      .subscribe(
        () => {
          console.log('Permission granted!');

          this.afMessaging.requestToken
            .subscribe(
              (token) => { console.log('Permission granted! Save to the server!', token); },
              (error) => { console.error(error); },
            );

        });


  }

  receiveMessage() {
    this.messaging.onMessage((payload) => {
      console.log("Message received. ", payload);
      this.currentMessage.next(payload)
    });

  }
}

0 个答案:

没有答案