带推送通知的Firebase本机插件无效

Sam*_*ath 4 firebase ionic-framework ionic2 ionic3 angular

我曾尝试使用Firebase本机插件发送推送通知.但它无法正常工作(没有收到真实设备的消息).你能告诉我怎么做吗?

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

      });
}
Run Code Online (Sandbox Code Playgroud)

完成上述实现后,我尝试使用User Segmentfirebase compose消息控制台发送推送通知.

nao*_*omi 6

推送通知可能有不同的原因.我提供了一系列步骤来实现推送通知.看看你可能错过了一些东西.

在Ionic app(for android)中实现推送通知的步骤:

  1. 创建一个新的firebase项目

注:在火力地堡package name必须是相同的应用程序idconfig.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包并实现该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:+"
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在Android设备上构建您的应用 $ ionic build android

  4. 测试推送通知.您可以使用免费的firebase通知发件人.

注意: API密钥是Legacy server key在firebase项目中调用的Cloud Messaging选项卡中找到的密钥.此外,如果您要将通知发送到特定主题,则需要首先使用subscribe方法订阅此主题.