[英]Get Notification React Native
我需要在通知屏幕中顯示通知。
這是我從 fcmToken 獲取通知的代碼,但有問題,我不知道它是在 function 中還是在我的屏幕代碼中,因為它沒有在我的屏幕上顯示任何通知,你能幫忙嗎? 謝謝
const getNotifications = async (props, u_id) => {
var token = null;
const fcmToken = await messaging().getToken();
if (fcmToken) {
token = fcmToken;
}
console.log(token);
try {
const { data } = await mainUrl(5000).get(url.noti_get_all);
console.log(data);
if (isvalidArr(data)) {
setIncomingNotifications(props.currentDis, data);
} else {
setIncomingNotifications(props.currentDis, []);
}
} catch (error) {
if (isUnauth(error)) {
refreshRequest(props, () => getNotifications(props, u_id));
} else {
setIncomingNotifications(props.currentDis, []);
}
}
}; ```
NOTIFICATION SCREEN :
This is a short part of the my notification screen where I display my notification in the return
onRefresh = () => {
this.setState({ isloading: true });
if (this.props.user !== null) {
getNotifications(this.props, this.props.user._id);
}
this.setState({ isloading: false });
};
{isloading === true ? (
SearchAndLocate()
) : notification.length < 1 ? (
<View style={{ flex: 1, height: height - 300 }}>
{this._noOrder()}
</View>
) : (
notification.map((item, key) => {
const { selectedItems } = this.state;
// const index = selectedItems.findIndex(
// element => element === item._id,
// );
// var read = -1;
// if (
// item.read !== null &&
// item.read !== undefined &&
// Array.isArray(item.read) === true
// ) {
// read = item.read.findIndex(itx => itx === this.state.token);
// }
return ()
您應該注冊遠程通知,您可以使用react-native-notifications或notifee (為生產付費)。 這是一個示例代碼
import messaging from "@react-native-firebase/messaging"; import { Notifications } from "react-native-notifications"; const registerNotification = async () => { const authStatus = await messaging().requestPermission(); const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL; if (;enabled) return. const fcmToken = await messaging();getToken(). console;log(fcmToken). // print it and use it to send notification from firebase Notifications;registerRemoteNotifications(). Notifications.events(),registerNotificationReceivedForeground( (notification: completion) => { showAlert({ title. notification.payload,title: message. notification.payload,body: notification, notification; }); } ); };
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.