angular - 带有推送通知的 Firebase native 插件无法正常工作

标签 angular firebase ionic-framework ionic2 ionic3

我试过使用Firebase native plugin发送推送通知。但它不起作用(没有向真实设备发送消息)。你能告诉我该怎么做吗?

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打包并实现 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 key 是在您的 firebase 项目中名为 Legacy server key 的 Cloud Messaging 选项卡中找到的 key 。 此外,如果您要向特定主题发送通知,则需要先使用 subscribe 订阅该主题。方法。

关于angular - 带有推送通知的 Firebase native 插件无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44224052/

相关文章:

Bootstrap 中的 Angular2 提供程序与 @component

mongodb - 使用另一个 API 进行 Firebase 身份验证

reactjs - React Hooks 不适用于 Firebase Cloud Functions 错误 : Invariant Violation: Invalid hook call

angular - 如何使用 ion-col 响应式设置大型动态表数据

node.js - ionic - 错误 : Cannot find module 'gulp-sass'

javascript - 如何在 primeNg 表中创建打印方法

Angular react 形式错误 : Must supply a value for form control with name:

angular - 有没有办法在图像路径中使用 angular base href 作为变量?

ios - Firebase 实时数据库不保存注册页面的数据

即使安装 Ionic 框架后 Android SDK 错误