当应用程序处于活动状态时,通过FCM发送通知



即使应用程序在前台,我如何才能让推送消息保持未读状态?

我正在使用ionic创建一个应用程序。我使用firebase云消息的推送通知(更新了dependency的cordova插件fcm(订阅消息,并使用后端发送消息。后端请求示例:

{
"headers": {
"Authorization": "key = blahblah",
"Content-Type": "application / json",
"project_id": "111111111111"
},
"json": {
"registration_ids": [
"token"
],
"time_to_live": 1200,
"data": {
"title": "title",
"body": "body",
"sound": "default",
"badge": 1,
"click_action": "FCM_PLUGIN_ACTIVITY"
}
}
}

我还尝试发送通知密钥,而不是日期密钥。尝试添加到根

{
...
"android": {
"ttl": "1200s",
"priority": "high",
"notification": {
"click_action": "FCM_PLUGIN_ACTIVITY"
}
},
...
"apns":{
"headers":{
"apns-priority":10,
"apns-expiration": date('U') + 1200
},
"payload":{
"aps":{
"badge":1,
"category":"FCM_PLUGIN_ACTIVITY"
}
}
}
}

结果是一样的——当应用程序在后台时,推送不会显示在托盘中。

如果应用程序在前台,我如何让推送保持未读状态,并仅在用户点击时在this.fcm.onNotification().subscribe(...)调用我的操作?

p.s.我试着使用cordova插件本地通知,但使用它引起了一些冲突-subscribe操作在ios 中没有启动

我在应用程序中使用ionic和cordova。

我正在使用插件〔cordova plugin firebasex〕(https://github.com/dpa99c/cordova-plugin-firebasex)以接收推送。

要发送推送,请使用以下json:

{
"registration_ids": [
"token"
],
"notification":{
"title":"Ionic 4 Notification",
"body":"Notification sent from POSTMAN",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY",
"icon":"notification_icon"
},
"data":{
"email":"teste@gmail.com"
},
"priority":"high"
}

对于android;"通知";当应用程序处于后台时显示通知的字段。

如果你在前台,你必须使用插件[cordova-plugin-local notifications]自己显示通知(https://github.com/katzer/cordova-plugin-local-notifications)

我的代码:

constructor(private firebaseCordova: FirebaseX) {}
private initializePushApp() {
this.checkNotificationPermission(false);
this.onMessageApp();
}
checkNotificationPermission(requested) {
try {
this.firebaseCordova.hasPermission().then(hasPermission => {
if (hasPermission) {
this.getTokenApp();
} else if (!requested) {
this.firebaseCordova.grantPermission().then(value => {
this.checkNotificationPermission(true);
});
} else {
// Denied
console.log("Notifications won't be shown as permission is denied");
}
});
} catch (e) {
console.error(e);
}
}
onMessageApp() {
try {
this.firebaseCordova.onMessageReceived().subscribe(data => {
this.showNotificationCordova(data);
});
} catch (e) {
console.error(e);
}
}
showNotificationCordova(notification) {
if ('background' === notification.tap) {
// click on notification in background
alert(notification.title);
} else {
this.clickNotificationSub = this.localNotifications.on('click').subscribe(data => {
// click on notification in foreground
alert(notification.title);
});
this.localNotifications.hasPermission().then(permission => {
if (permission) {
this.localNotifications.schedule({
id: 1,
title: notification.title,
text: notification.body,
icon: notification.image ? notification.image : 'notification_icon',
});
} else {
this.localNotifications.requestPermission().then(value => {
if (value) {
this.localNotifications.schedule({
id: 1,
title: notification.title,
text: notification.body,
icon: notification.image ? notification.image : 'notification_icon',
});
} else {
console.log("Notifications won't be shown as permission is denied");
}
});
}
});
}
}

当您输入此条件"background"===通知。点击"在后台中单击了通知">

相关内容

  • 没有找到相关文章

最新更新