带有推送通知的 Firebase 本机插件无法正常工作

Firebase native plugin with push notifications is not working

我试过使用Firebase native plugin发送推送notifications.But它不起作用(没有收到消息到真实设备)。你能告诉我怎么做吗?

app.component.ts

constructor(platform: Platform, private firebase: Firebase) {

    platform.ready().then(() => {
        this.firebase.getToken()
            .then(token => console.log(`The token is ${token}`)) // save the token server-side and use it to push notifications to this device
            .catch(error => console.error('Error getting token', error));

 this.firebase.onNotificationOpen()
            .subscribe(res => {
                if (res.tap) {
                    // background mode
                    console.log("background");
                    console.log(res);
                    alert(res);
                } else if (!res.tap) {
                    // foreground mode
                    console.log("foreground");
                    console.log(res);
                    alert(res);
                }
            });

      });
}

在上述实施之后,我尝试在 firebase compose 消息控制台上使用 User Segment 发送推送通知。

推送通知不起作用可能有多种原因。为了实现推送通知,我提供了一组要遵循的步骤。看看你是不是漏掉了什么。

在 Ionic 应用程序中实现推送通知的步骤(适用于 android):

  1. 新建一个firebase project

注意: Firebase package name 必须与应用程序 id 相同 config.xml.

  1. 下载 google-services.json 文件并将其放在应用程序的根目录中。
  2. 添加 android 平台 $ ionic platform add android(如果您还没有)
  3. 安装 firebase 插件 $ ionic plugin add cordova-plugin-firebase

注意:您应该在将 google-services.json 文件放入项目后安装插件 - 因为此文件在安装过程中被复制到平台目录中。

  1. 安装ionic-native firebase package and implement the onNotificationOpen方法。

  2. 将以下内容添加到您的 build.gradle 文件中:

    buildscript {
    // ...
        dependencies {
            // ...
            classpath 'com.google.gms:google-services:3.1.0'
        }
    }
    
    //....
    
    dependencies {
        // SUB-PROJECT DEPENDENCIES START
        // ...
        compile "com.google.firebase:firebase-core:+"
        compile "com.google.firebase:firebase-messaging:+"
    }
    
  3. 在 Android 设备上构建您的应用 $ ionic build android

  4. 测试推送通知。您可以使用 this 个免费的 firebase 通知发送器。

注意: API 键是在您的 firebase 项目中名为 Legacy server key 的云消息传递选项卡中找到的键。 此外,如果您要向特定主题发送通知,则需要先使用 subscribe 方法订阅该主题。