firebase - 如何在 ionic android 应用程序中启用 firebase 通知

标签 firebase ionic-framework cordova-plugin-fcm

我想构建一个应用程序并使用 firebase 进行通知,在谷歌上进行了大量搜索,但没有找到任何好的指南和解决方案,我尝试的一切都以一些错误告终。我尝试了 ionic 文档,但是在 ionic v4 之后它们都很乱,它们显示了关于 v4 的所有内容,我的应用程序几乎完成了,只剩下这个东西了。 我将不胜感激任何帮助。 知道如何进行吗?我很可能没有正确配置 Firebase。我已将 google-services.json 放在根目录中,没有问题。但在那之后一切都超出了我的理解

 AN ERROR OCCURRED WHILE RUNNING ionic cordova plugin add phonegap-plugin-push --variable SENDER_ID-150482406038 --SAVE EXIT CODE 1

最佳答案

得到这个工作。感谢大家的帮助! 使用的引用资料-

工作
  1. ionic 3.20.1
  2. Cordova 8.1.2

我遵循的步骤

  1. 使用 ionic cordova platform removeandroid 移除了我的安卓平台然后我重新创建了它 ionic cordova platform add android .只是为了避免我和我的老 friend 一起出现的任何错误 安卓版。

  2. 得到 google-services.json并将其放置在 rootDirectoryOfApp\platforms\android\app

  3. 然后我运行$ ionic cordova plugin add phonegap-plugin-push $ npm install --save @ionic-native/push@4
  4. 编辑 config.xml寻找 <platform name="android">在那之下我 写了<resource-file src="google-services.json" target="app/google-services.json" />
  5. 编辑 package.json寻找 "phonegap-plugin-push"并编辑它 像这样

    "phonegap-plugin-push": {
        "ANDROID_SUPPORT_V13_VERSION": "27.+", // already there
        "FCM_VERSION": "11.6.2", // already there
        "SENDER_ID": "numeric key obtain from firebase console" // added
      },
    
  6. 打开 app.module.ts并导入 import { Push } from '@ionic-native/push';在提供商下添加推送 ... providers: [ StatusBar, SplashScreen, Push, ....

  7. 然后在提供者中 我进口了import { Push, PushObject, PushOptions } from '@ionic-native/push'; 然后在构造函数中我添加了 private push: Push, 在该提供者的类中,我编写了如下函数

    推送设置(){

    // to check if we have permission
    this.push.hasPermission()
    
    .then((res: any) => {
    if (res.isEnabled) {
    console.log('We have permission to send push notifications');
    } else {
    
    console.log('We do not have permission to send push notifications');
    }
    });    
    // Create a channel (Android O and above). You'll need to provide the id, description and importance properties.
    
    this.push.createChannel({
    id: "testchannel1",
    description: "My first test channel",
    // The importance property goes from 1 = Lowest, 2 = Low, 3 = Normal, 4 = High and 5 = Highest.
    importance: 3
    
    }).then(() => console.log('Channel created'));
    // Delete a channel (Android O and above)
    this.push.deleteChannel('testchannel1').then(() => console.log('Channel deleted'));  
    
    
    
    // Return a list of currently configured channels
    this.push.listChannels().then((channels) => console.log('List of channels', channels))    
    // to initialize push notifications  
    
    const options: PushOptions = {
    android: {
    senderID:"150482406038",
    },
    
    ios: {
    alert: 'true',
     badge: true,
    sound: 'false'
    },     
    };
    
    const pushObject: PushObject = this.push.init(options);     
    pushObject.on('notification').subscribe((notification: any) => console.log('Received a notification', notification));     
    pushObject.on('registration').subscribe((registration: any) => console.log('Device registered', registration));     
    pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
    }
    
  8. 现在在我想使用它的地方导入那个提供者,然后调用 该功能来自那里。但只有在
    之后才调用它 this.platform.ready().then(() => {或者登录成功。

我分享这个是因为我觉得它不难,而且网上的指南很困惑 如果您发现它有误或不适用于您的情况,请发表评论。

关于firebase - 如何在 ionic android 应用程序中启用 firebase 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54456431/

相关文章:

android - 使用 FCM 的 Ionic 2 推送通知

ios - Firebase 插件无法在 IOS (Ionic) 上运行

android - 使用 Firebase Storage 和 Firebase Firestore 创建离线第一个应用程序的最佳方法是什么? #AskFirebase

angular - 使用 Angular2/Ionic2 删除 Firebase 用户

javascript - 如何刷新 ionic View

ionic-framework - ionic 存储模块给出角度为 5 的 OpaqueToken 错误

firebase - 如何使用云功能身份验证触发 flutter/Dart

ios - 对 Firebase 查询实现感到困惑

android - 如何在 ionic 中关闭应用程序时使设备 token 不注销

android - 如何增加 FCM 推送通知文本的字体大小