FCM onNotification离子没有做任何事情



我正在使用Ionic 4,目前我的应用程序确实订阅了一个主题,但onNotification.subcribe根本不会启动

我正在使用poster进行测试,从我所看到的情况来看,我需要将click_action添加到通知下的json负载中,以启动onNotification,但当我这样做时,我会收到一个错误这是FCM在我添加click_action 时在poster中返回的错误

我在安卓系统上测试,而不是在iOS 上测试

"code": 400,
"message": "Invalid JSON payload received. Unknown name "click_action" at 'message.notification': Cannot find field.",
"status": "INVALID_ARGUMENT"

这就是我的json负载看起来像的样子

{
"message":{
"data":{
"route":"dashboard"
},
"topic":"6616",
"notification":{
"title":"user id ",
"body":"body test 2",
"click_action":"FCM_PLUGIN_ACTIVITY"
}
}

你知道我做错了什么吗?

因此,对于安卓/谷歌生态系统,有效负载与APN/iOS需要不同的处理方式不同。

click_action仅与Google/Android 相关

请参阅跨平台通知消息示例:https://firebase.google.com/docs/cloud-messaging/concept-options#example-带有特定平台交付选项的通知消息

请参阅此处的更多信息,以正确配置每个平台的通知对象和有效载荷:https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-有效载荷支持

以下是如何在ionic中使用谷歌fcm通知的说明。

您需要启动一个带有通知服务的firebase项目,下载google-services.json

电容器具有内置的推送通知功能。使用此在您的应用程序/页面中导入推送

安装:

ionic start capApp blank
ionic integrations enable capacitor
npx cap init
? App name: CapApp
? App Package ID: com.mydomain.myappname
ionic build
npx cap add android

代码:

imports in home.page.ts
import {
Plugins,
PushNotification,
PushNotificationToken,
PushNotificationActionPerformed } from '@capacitor/core';
const { PushNotifications } = Plugins;
export class HomePage implements OnInit {
ngOnInit() {
console.log('Initializing HomePage');
// Register with Apple / Google to receive push via APNS/FCM
PushNotifications.register();
// On success, we should be able to receive notifications
PushNotifications.addListener('registration', 
(token: PushNotificationToken) => {
alert('Push registration success, token: ' + token.value);
}
);
// Some issue with our setup and push will not work
PushNotifications.addListener('registrationError', 
(error: any) => {
alert('Error on registration: ' + JSON.stringify(error));
}
);
// Show us the notification payload if the app is open on our device
PushNotifications.addListener('pushNotificationReceived', 
(notification: PushNotification) => {
alert('Push received: ' + JSON.stringify(notification));
}
);
// Method called when tapping on a notification
PushNotifications.addListener('pushNotificationActionPerformed', 
(notification: PushNotificationActionPerformed) => {
alert('Push action performed: ' + JSON.stringify(notification));
}
);
}

离子4 的另一种方法

离子4反应应用程序使用电容

有了用于fcm的Ionic cordova插件,工作和测试就不那么容易了,我认为电容器是一个更好的解决方案!

最新更新