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

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

问题描述

我正在尝试将 Firebase Cloud Messaging 与 Angular2 结合使用,但好像他们不希望我们这样做@Google.

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

我一直在关注this这个能够阻止错误.

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

我现在面临以下问题:为什么 messaging.getToken() 什么都不返回?没有错误,控制台中没有令牌,什么都没有.

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

欢迎任何帮助.谢谢.

编辑我已经尝试使用 onTokenRefresh() 更新了下面的代码.它不会改变任何东西.我觉得这是因为我并没有真正连接到我的 firebase 文件.有人能用 Angular2 和 AngularFire2 制作 FCM 吗?

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

}

推荐答案

好的,我找到了解决方案.问题来自于 Angular-CLI 和 AngularFire2 之间的关系.

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

这里.

我必须在 src/tsconfig.jsonsrc/typings.d.ts 中添加以下内容:

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

"types": [
  "firebase"
]

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

我希望这可以帮助某人.

I hope this can help someone.

感谢您的帮助,弗兰克·范·普费伦 (Frank van Puffelen).你总是在这里帮助我解决 firebase 问题:)

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

EDIT 我刚刚发现它不适用于所有浏览器.它在 Firefox 中有效,但在 chrome 中无效......怎么会这样?

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天全站免登陆