我试图使用Firebase native plugin发送推送通知。但它不起作用(无法将消息发送到真正的设备)。你能告诉我怎么做吗?
应用组件
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)中实现推送通知的步骤:
创建新的firebase project
注意:firebasepackage name
必须与id
下载config.xml
文件并将其放在应用程序的根目录中。
添加android平台google-services.json
(如果还没有)
安装firebase插件$ ionic platform add android
。
注意:您应该在将$ ionic plugin add cordova-plugin-firebase
文件放入项目后安装该插件,因为在安装过程中将此文件复制到平台目录中。
安装ionic-native firebase包并实现google-services.json
方法。
在onNotificationOpen
文件中添加以下内容:
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:+"
}
在Android设备上构建应用程序
build.gradle
测试推送通知。您可以使用this免费的firebase通知发件人。
注意:api密钥是在firebase项目中名为
$ ionic build android
的云消息选项卡中找到的密钥。另外,如果要将通知发送到特定主题,则需要首先使用subscribe方法订阅此主题。