javascript - Firebase FCM 没有被 onMessage() 捕获
问题描述
我在将 FCM 实施到我的反应本机应用程序中时遇到了麻烦。文档一点都不好,所以必须来这里寻求帮助。我设置了一个云功能,它将向给定的令牌发送通知。唯一的问题是,没有任何错误,并且该函数不发送通知。
云功能
exports.sendNotification = functions.https.onCall((fcmToken) => {
console.log(fcmToken);
console.log("in the func");
const message = {
notification: {
title: "850",
body: "2:45",
},
token: fcmToken,
};
// Send a message to the device corresponding to the provided
// registration token.
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log("Successfully sent message:", response);
})
.catch((error) => {
console.log("Error sending message:", error);
});
// See documentation on defining a message payload.
});
反应原生监听器
useEffect(() => {
const unsubscribe = messaging().onMessage(async (remoteMessage) => {
Alert.alert('A new FCM message arrived!', JSON.stringify(remoteMessage))
})
调用云函数
const token = await messaging().getToken()
functions()
.httpsCallable('sendNotification')(token)
.then(async (response) => {
console.log(response)
})
.catch((error) => {
console.log(error)
})
解决方案
我认为你不应该在使用效果中使用它。
在 Appregistry 之前,我在 index.js 上使用它
import messaging from '@react-native-firebase/messaging';
async function onMessageReceived(message) {
console.log("background message: " + JSON.stringify(message.data));
}
messaging().onMessage(onMessageReceived);
function HeadlessCheck({ isHeadless }) {
if (isHeadless) {
// App has been launched in the background by iOS, ignore
return null;
}
return <App />;
}
AppRegistry.registerComponent(appName, () =>HeadlessCheck );
像那样
推荐阅读
- php - 视图和我的控制器如何“连接”
- ansible - 如何使用官方 stdout_callback 插件显示调用模块参数?
- python - Chropleth 地图没有显示我想要的
- java - 线程“main”java.sql.SQLException 中的异常:IO 错误:网络适配器无法建立连接
- grails - 当存在单个元素时遍历数组
- css - Textarea 扩展超出填充并且不齐平
- django - 通过翻译为使用 Django 在 url 中指定的语言代码返回字段
- android - 如何将大尺寸视频导入 Android Studio?
- cmd - 启用 Windows 服务以使用命令行
- r - 即使小数点后的所有内容都为零,也要找到小数点