FCM getToken()不返回任何东西 [英] FCM getToken() doesn't return anything

查看:1184
本文介绍了FCM getToken()不返回任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Angular2上使用Firebase Cloud Messaging,但是他们不希望我们这样做@Google。 a href =https://firebase.google.com/docs/cloud-messaging/js/client =nofollow noreferrer>这个和这个能够阻止错误。



m现在面临以下问题:
为什么 messaging.getToken()不会返回任何结果?
没有错误,没有令牌在控制台,什么都没有。



任何帮助都是值得欢迎的。谢谢。



编辑
我使用 onTokenRefresh() 。它不会改变任何东西。我觉得这是因为我没有真正插入我的Firebase文件。任何人都可以使用Angular2和AngularFire2来制作FCM?


$ b

import {Component,OnInit,Inject } from @ angular / core';
从angularfire2导入{FirebaseApp};
从'firebase'导入*作为firebase;

@Component({
templateUrl:'./+config.component.html',
styleUrls:['./+config.component.scss']
})
导出类UserConfigComponent实现OnInit {

private _messaging:firebase.messaging.Messaging;

构造函数(@Inject(FirebaseApp)private _firebaseApp:firebase.app.App){
this._messaging = firebase.messaging();

$ b $ ngOnInit(){
//如果更新了实例ID令牌,则会触发回调。
this._messaging.onTokenRefresh(function(){
this._messaging.getToken()
.then(function(refreshedToken){
console.log('Token refresh') ,refreshedToken);
})
.catch(function(err){
console.log('Unable to retrieve refreshed token',err);
});
});


requestPushNotif(){
this._messaging.requestPermission()
.then((=)=> {
console.log(' );
return this._messaging.getToken();
})
.then(function(token){
console.log(token); $ b $ (错误)=> {
console.log(错误);
});


$ b


解决方案

好吧,我找到了解决办法。问题来自Angular-CLI和AngularFire2之间的关系。



这是 here。



我不得不添加以下内容 src / tsconfig.json src / typings.d.ts

 types:[
firebase
]



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

我希望这可以帮助别人。



谢谢你,弗兰克van puffelen,你的帮助。你总是在这里帮助我的firebase问题:)

编辑我发现它并不适用于所有的浏览器。它在Firefox中工作,但不在铬...如何?

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);
      });
  }

}

解决方案

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 ?

这篇关于FCM getToken()不返回任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆