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

how to enable firebase notifications in ionic android application

我想构建一个应用程序并使用 firebase 进行通知,在 google 上进行了大量搜索,但没有找到任何好的指南和解决方案,我尝试的一切都以一些错误告终。我尝试了 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

我一直在使用本教程:https://medium.com/@felipepucinelli/how-to-add-push-notifications-in-your-cordova-application-using-firebase-69fac067e821 和 Android 推送通知开箱即用。祝你好运!

^ 你可能想试试 Chrillewoodz 提到的 cordova-plugin-firebase 插件

成功了。谢谢大家的帮助! 参考文献使用-

工作
  1. 离子 3.20.1
  2. 科尔多瓦 8.1.2

我遵循的步骤

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

  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 下添加 Push ... providers: [ StatusBar, SplashScreen, Push, ....

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

    pushSetup(){

    // 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(() => {或登录成功时。

我分享了这个,因为我发现它有点困难,并且在网上得到了令人困惑的指南 如果您发现它有误或不适用于您的情况,请发表评论。