firebase - React Native:管理 RNFirebase 上的通知
问题描述
我已经使用react-native-firebase
库成功实现了一个基本的通知功能,一切都按预期工作,信息被正确接收并准备好用于我尚未确定的目的。对于通知处理部分,我的代码目前看起来像这样:
componentDidMount() {
/**
* When app on foreground, rewrap received notification and re-send it as notification using channelId
* A workaround because channelId never set by default by FCM API so we need to rewrap to make sure it is
* shown on user's notification tray
*/
this.notificationListener = firebase.notifications().onNotification((notification) => {
//data object must have channelId props as a workaround for foreground notification on Android
console.log('Notif ', notification);
notification.android.setChannelId(notification.data.channelId);
firebase.notifications().displayNotification(notification);
});
//On Notification tapped, be it from foreground or background
this.notificationOpen = firebase.notifications().onNotificationOpened((notificationOpen) => {
//body and title lost if accessed from background, taking info from data object by default
const notification = notificationOpen.notification;
console.log('Open ', notification)
Alert.alert(notification.data.title, notification.data.body);
});
//When notification received when app is closed
this.initialNotification = firebase.notifications().getInitialNotification()
.then((notificationOpen) => {
//body and title lost if accessed this way, taking info from data object where info will persist
if (notificationOpen) {
const notification = notificationOpen.notification;
console.log('Initial ', notification)
Alert.alert(notification.data.title, notification.data.body);
}
});
}
componentWillUnmount() {
this.notificationListener();
this.initialNotification()
this.notificationOpen();
}
上面的代码让我可以使用我从firebase控制台或我的同事在上述范围内设置的php服务器发送的任何信息(不确定服务器端实现是如何完成的,但它给了我完全相同的通知对象)。
所以这很好,但问题是当我从firebase控制台在IOS上设置徽章时,一旦我打开通知,徽章就不会消失。我一直在试图弄清楚是否有任何额外的位我必须添加到上面的块中以编程方式减少徽章计数器,但到目前为止还没有运气。
因此,如果这里有人可以向我展示如何在 Android 和IOS,将不胜感激:)
解决方案
事实证明,只要打开应用程序,简单firebase.notifications().setBadge(0)
的根目录componentDidMount()
就会清除徽章计数。
也可能需要使用firebase.notifications().removeAllDeliveredNotifications()
或firebase.notifications().cancelAllNotifications()
从通知托盘中删除它们。
推荐阅读
- javascript - 我想使用 Next.js API 路由将图像上传到 Imgur
- nginx - 从 Laravel 中的 curl 请求中获取标头
- flutter - 在 vsCode 中格式化 .dart 文件(颤振)
- flutter - Flutter Update 2.0.6 以前的库(如 Expanded、ListView 等)的问题
- android - 如果发布构建机器发生更改,我们是否需要重新生成和更新密钥库和别名?
- node.js - 我无法从 nodejs 应用程序中的 mongodb 快速检索文件
- heroku - 如何在 Hostinger 上托管 Heroku DNS
- http - Apache HTTP 代理为某些目标 IP 工作并从其他目标接收 502 Bad Request
- jmeter - JMeter 使用 ForEach 控制器并行执行
- java - 如何使用 RestAssured.JsonPath 从 JSON 中获取特定属性的值