【问题标题】:Firebase native plugin with push notifications is not working带有推送通知的 Firebase 本机插件不起作用
【发布时间】:2017-10-28 16:11:13
【问题描述】:

我尝试使用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 发送推送通知。

【问题讨论】:

  • 您能解释一下不工作是什么意思吗?你安装插件了吗?您是否为您的应用配置了 Firebase?
  • 是的。这些是基本的东西,不。不工作意味着没有收到消息到真实设备@naomi
  • 我已经用这个插件实现了分析,它工作正常。问题在于推送通知。 @娜奥米
  • 你需要调用onNotificationOpen在回调ionicframework.com/docs/native/firebase/#onNotificationOpen获取通知数据

标签: angular firebase ionic-framework ionic2 ionic3


【解决方案1】:

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

在 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 密钥是在您的 firebase 项目中名为 Legacy server key 的云消息传递选项卡中找到的密钥。 此外,如果您要向特定主题发送通知,则需要先使用subscribe 方法订阅该主题。

【讨论】:

  • 哦..帮助很大。我会尝试您的步骤并再次回来。我绝对没有按照您的顺序执行这些步骤。一个问题,我使用@987654338的方式怎么样@方法。正确吗?当我在浏览器上进行测试时,我无法运行该应用程序。在 subscribe 行之后它给出了此运行时错误 Uncaught (in promise): cordova_not_available。你知道为什么吗?
  • 堆栈跟踪core.es5.js:1084 文件ERROR Error: Uncaught (in promise): cordova_not_available at c (polyfills.js:3) at polyfills.js:3 at t.invokeTask (polyfills.js:3) at Object.onInvokeTask (core.es5.js:4116) at t.invokeTask (polyfills.js:3) at r.runTask (polyfills.js:3) at o (polyfills.js:3) at HTMLDocument.invoke (polyfills.js:3)
  • cordova 仅在设备上运行的应用程序中可用,在您的代码前添加 if(window.cordova)
  • 现在它给出了这个错误Property 'cordova' does not exist on type 'Window'.。我怎样才能避免它?
  • 这个Uncaught (in promise): cordova_not_available只显示subscribe方法。同插件的其他promise方法没有问题。你知道为什么吗?
猜你喜欢
  • 2017-04-29
  • 1970-01-01
  • 1970-01-01
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多