我试过使用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
发送推送通知。
最佳答案
推送通知不起作用可能有不同的原因。为了实现推送通知,我提供了一组要遵循的步骤。看看你是否错过了什么。
在 Ionic 应用程序(适用于 android)中实现推送通知的步骤:
- 创建一个新的 firebase project
注意:Firebase package name
必须与应用程序 id
中的相同
config.xml
.
- 下载
google-services.json
文件并将其放在您应用的根目录中。 - 添加 android 平台
$ ionic platform add android
(如果您还没有) - 安装 firebase 插件
$ ionic plugin add cordova-plugin-firebase
。
注意:您应该在将 google-services.json
文件放入您的项目后安装该插件 - 因为该文件在安装过程中被复制到平台目录中.
安装 ionic-native firebase打包并实现
onNotificationOpen
方法。将以下内容添加到您的
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:+" }
在 Android 设备上构建您的应用
$ ionic build android
- 测试推送通知。你可以使用 this免费的 Firebase 通知发送器。
注意:API key 是在您的 firebase 项目中名为 Legacy server key
的 Cloud Messaging 选项卡中找到的 key 。
此外,如果您要向特定主题发送通知,则需要先使用 subscribe 订阅该主题。方法。
关于angular - 带有推送通知的 Firebase native 插件无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44224052/