FCM getToken() doesn't return anything

耗尽温柔 提交于 2019-12-08 00:33:13

问题


I'm trying to use Firebase Cloud Messaging with Angular2 but it's like they don't want us to do that @Google.

I've been following this and this to be able to stop errors.

I'm now facing the following problem: Why does messaging.getToken() returns nothing? No error, no token in the console, nothing.

Any help is more than welcome. Thank you.

EDIT I've updated the code bellow with my try using onTokenRefresh(). It does not change anything. I feel like it comes from the fact that I'm not really plugged to my firebase files. Anybody have been able to make FCM with Angular2 and AngularFire2?

import {Component, OnInit, Inject} from '@angular/core';
import { FirebaseApp } from "angularfire2";
import * as firebase from 'firebase';

@Component({
  templateUrl: './+config.component.html',
  styleUrls: ['./+config.component.scss']
})
export class UserConfigComponent implements OnInit {

  private _messaging: firebase.messaging.Messaging;

  constructor(@Inject(FirebaseApp) private _firebaseApp: firebase.app.App) {
    this._messaging = firebase.messaging();
  }

  ngOnInit() {
    // Callback fired if Instance ID token is updated.
    this._messaging.onTokenRefresh(function() {
      this._messaging.getToken()
        .then(function(refreshedToken) {
          console.log('Token refreshed.', refreshedToken);
        })
        .catch(function(err) {
          console.log('Unable to retrieve refreshed token ', err);
        });
    });
  }

  requestPushNotif() {
    this._messaging.requestPermission()
      .then(() => {
        console.log('have permission');
        return this._messaging.getToken();
      })
      .then(function(token) {
        console.log(token);
      })
      .catch((error) => {
        console.log(error);
      });
  }

}

回答1:


Ok I've found the solution. The problem came from the relation between the Angular-CLI and AngularFire2.

It's here.

I had to add the following in src/tsconfig.json and in src/typings.d.ts:

"types": [
  "firebase"
]

and

declare var require: any;
declare var module: any;

I hope this can help someone.

Thank you, Frank van Puffelen, for your help. You are always here to help on my firebase questions :)

EDIT I just found out that it does not work in all browser. It works in firefox but not in chrome... How come ?



来源:https://stackoverflow.com/questions/40912591/fcm-gettoken-doesnt-return-anything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!