Angular 如何在Ionic2中从FCM保存设备令牌

Angular 如何在Ionic2中从FCM保存设备令牌,angular,cordova,typescript,ionic2,ionic3,Angular,Cordova,Typescript,Ionic2,Ionic3,我正在使用FCM插件为ionic2做推送通知。 参考: 我跟着 它工作正常,我可以从firebase控制台接收推送。现在我想构建自己的服务器,让管理员用自己的后端发送推送通知 我面临的一个问题是:我可以获得设备令牌,但是,我不知道如何保存它。下面是我获取令牌的代码: initializeApp() { this.platform.ready().then(() => { // Okay, so the platform is ready and our plugins

我正在使用FCM插件为ionic2做推送通知。 参考:

我跟着

它工作正常,我可以从firebase控制台接收推送。现在我想构建自己的服务器,让管理员用自己的后端发送推送通知

我面临的一个问题是:我可以获得设备令牌,但是,我不知道如何保存它。下面是我获取令牌的代码:

initializeApp() {
    this.platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      StatusBar.styleDefault();

       FCMPlugin.getToken(
                function (token) {
                    console.log(token); //I can successfully get the token, but I don't know how to return it.
                    alert(token);
                },
                function (err) {
                    console.log('error retrieving token: ' + err);
                }
            );
我尝试过很多方法,比如“返回值”、“存储到有价值的文件”;但是,我仍然不知道如何从“FCMPlugin.getToken”函数中获取它


有人能帮忙吗?谢谢

您可以使用箭头功能,如下所示:

initializeApp() {

    // ...

    FCMPlugin.getToken(
       (token) => { this.saveToken(token); },
       (err) => { this.showError(err); }
    );

    // ...

}

// ...

private saveToken(token: string): void {
    // Save the token in the storage...
}

private showError(error: any): void {
    // Show the error to the user
}
不同之处在于,现在您正在使用箭头函数

箭头函数表达式的语法比函数短 表达式和不绑定自己的this参数、super或 新目标

因此,当您在arrow函数中使用
时,它仍将引用组件实例(而不是当前函数)。

登录()函数中,您可以使用此代码

this.push.register().then((t: PushToken) => {
  return this.push.saveToken(t);
}).then((t: PushToken) => {
  console.log('Token saved:', t.token);
});
当用户登录你的应用时,你可以保存他们的设备令牌