我想构建一个应用程序并使用 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
最佳答案
得到这个工作。感谢大家的帮助! 使用的引用资料-
为
工作- ionic 3.20.1
- Cordova 8.1.2
我遵循的步骤
使用
ionic cordova platform removeandroid
移除了我的安卓平台然后我重新创建了它ionic cordova platform add android
.只是为了避免我和我的老 friend 一起出现的任何错误 安卓版。得到
google-services.json
并将其放置在rootDirectoryOfApp\platforms\android\app
- 然后我运行
$ ionic cordova plugin add phonegap-plugin-push $ npm install --save @ionic-native/push@4
- 编辑
config.xml
寻找<platform name="android">
在那之下我 写了<resource-file src="google-services.json" target="app/google-services.json" />
编辑
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 },
打开
app.module.ts
并导入import { Push } from '@ionic-native/push';
在提供商下添加推送... providers: [ StatusBar, SplashScreen, Push, ....
然后在提供者中 我进口了
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)); }
现在在我想使用它的地方导入那个提供者,然后调用 该功能来自那里。但只有在
之后才调用它this.platform.ready().then(() => {
或者登录成功。
我分享这个是因为我觉得它不难,而且网上的指南很困惑 如果您发现它有误或不适用于您的情况,请发表评论。
关于firebase - 如何在 ionic android 应用程序中启用 firebase 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54456431/