我已经安装了react-native-firebase v6.2
,但现在我找不到实现云消息传递的好帮助。
任何人都可以帮助如何在没有通知模块的情况下使用云消息传递?
因为通知模块已从v6中删除,我决定只使用云消息传递,但我不知道该怎么做。
远程通知的权限和请求正常。这是我发送给自己的代码,但它在 android 上不起作用,请提供任何帮助
const sendNotification = async ()=> {
try {
const firebaseAPIKey = "QEOEKDjjIS1E6ZDKSKJXAxdkIGk3qTb4FXCMs";
const notifyMessage = {
registration_ids: ["fheeeEaBwK0HM:eoOELDL6BUEdNUhRvJsjBv3x9dPLybZqqBoqJ_wzcE5_pbl-e1nnIsLhbzJd5-_R4MNhVYtuCLXWjWSguLfKTOqXR0gEfhgukdIw-AexChDjy8tI6u7f04i5xtOkaAJgU"],
notification: {
"title": "Muhammad Wafa",
"body": "Hi dear this is a simple message.",
"vibrate": 1,
"sound": 1,
"show_in_foreground": true,
"priority": "high",
"content_available": true
}
}
let headers = new Headers({
"Content-Type": "application/json",
"Authorization": `key=${firebaseAPIKey}`
});
let response = await fetch("https://fcm.googleapis.com/fcm/send", {method: "POST", headers, body: JSON.stringify(notifyMessage)});
console.log('response', response);
} catch (err) {
console.log('Error in : sendNotification', err);
}
}
如果你的应用位于前台,则不会看到任何通知。
通知模块在 react-native-firebase-v6 中不再可用,这意味着现在要显示本地推送通知,您必须依赖任何其他库,例如 react-native-push-notification。除此之外,其他事情应该可以正常工作。
这是我的应用程序.js-
import messaging from '@react-native-firebase/messaging';
// ... other imports
const App = () => {
useEffect(() => {
// Notification caused app to open from background state
const onNotificationOpenedListener = messaging().onNotificationOpenedApp(
remoteMessage => {
console.log(remoteMessage, 'App.useEffect.onNotificationOpenedListener');
}
);
// Check whether an initial notification is available
// Notification caused app to open from quit state
messaging()
.getInitialNotification()
.then(remoteMessage => {
if (remoteMessage) {
console.log(remoteMessage, 'App.useEffect.getInitialNotification');
}
});
// Foreground state messages
const onMessageReceivedListener = messaging().onMessage(
async remoteMessage => {
// show local notification using react-native-push-notification
showLocalPushNotification(remoteMessage);
}
);
return () => {
// unsubscribe all listeners
onNotificationOpenedListener();
onMessageReceivedListener();
};
}, []);
return (
<Provider store={store}>
<Navigation />
</Provider>
);
};
用于显示本地推送通知的实用程序-
// todo: implement click handler as firebase-messaging handlers are not fired
export const showLocalPushNotification = remoteMessage => {
const { notification = {}, data = {} } = remoteMessage;
PushNotification.localNotification({
//smallIcon: 'ic_notification', // todo uncomment when resource is added
channelId: '', // todo
messageId: 'google:message_id',
title: notification.title,
message: notification.body,
sound: 'default',
data
});
};
唯一的问题是,当单击本地通知时,通知处理程序(onNotificationOpenedApp/getInitialNotification
(不会触发。我可以继续使用 react-native-push-notification 注册通知处理程序,但最终我将有两个用于相同功能的处理程序。
还没有找到解决方案。
我的索引.js-
import { AppRegistry } from 'react-native';
import messaging from '@react-native-firebase/messaging';
import App from './App';
import { name as appName } from './app.json';
// Register background handler
messaging().setBackgroundMessageHandler(async remoteMessage => {
// Data only messages will be ignored when app is in Background, it can be handled here
console.log(remoteMessage, 'index.setBackgroundMessageHandler');
});
AppRegistry.registerComponent(appName, () => App);
无需在 Android 清单中注册服务.xml