android - 无法让 setBackgroundMessageHandler 工作
问题描述
在react-native-firebase v6中,我无法setBackgroundMessageHandler
在我的应用程序中工作。收到通知就好了,但没有执行处理程序。
我已经像指南中那样做了,但无济于事。
import { AppRegistry } from 'react-native';
import messaging from '@react-native-firebase/messaging';
import AsyncStorage from '@react-native-community/async-storage';
import App from './App';
import { name as appName } from './app.json';
AppRegistry.registerComponent(appName, () => App);
messaging().setBackgroundMessageHandler(async ({ data: { title, message } }) => {
console.log('in background');
// Save the notification locally
const notificationList = JSON.parse(await AsyncStorage.getItem('@SM_NOTIFICATIONS')) || [];
notificationList.push({ title, message, isRead: false });
await AsyncStorage.setItem('@SM_NOTIFICATIONS', JSON.stringify(notificationList));
});
除了传入的通知之外,什么也没发生。我希望代码将传入的通知保存在AsyncStorage
.
解决方案
好的。在咨询了开发团队之后,我终于弄清楚了这一点。
首先,setBackgroundMessageHandler
必须在调用之前registerComponent
。
其次,不要发送通知数据。仅发送自定义数据(静默推送)。因此,您需要使用本地通知而不是推送通知来在系统托盘中显示通知。
因为 Firebase 控制台不支持静默推送,所以我使用FCM API v1仅发送数据。这是我的例子:
POST https://fcm.googleapis.com/v1/projects/my-project/messages:send
{
"validate_only": false,
"message": {
"name": "XXX",
"data": {
"title": "BG 2",
"message": "BG BARU"
},
"topic": "C3166"
}
}
如您所见,notification
上面的 JSON 中没有字段。
推荐阅读
- c++ - LLVM KaleidoscopeJIT:如何查看错误?
- kubernetes - 尝试将加密的 AWS EFS 与 EKS 中的 efs-csi-node 连接时出现挂载错误
- sql - 在 SQL Server 中将单个值与逗号分隔值进行比较
- python - 从字符串值创建唯一的短 ID
- python - 如何使用 GnuPG (gpg) 可执行文件和依赖项 (PyInstaller) 冻结 python 应用程序
- c# - UWP 相当于 Android 的 SpannableString,内部带有图像
- python - 如何将原始电子邮件的内容解析为熊猫数据框对象
- python - 杀死所有机器中的 MPI 进程
- php - 在 mysql php 搜索中突出显示搜索词,忽略大小写敏感
- javascript - 我如何制作 div 或
- 通过向上/向下箭头键访问