[英]React Native Firebase Push Notification iOS Real Device
Im quite new react mobile development.我是相当新的 React 移动开发。 I tried push notifications with firebase. It working nice on android emulator/real device.
我尝试使用 firebase 推送通知。它在 android 模拟器/真实设备上运行良好。 But its not working on ios real device.
但它不适用于 ios 真实设备。 I've been trying to find a solution for this issue in 3 days.
我一直在尝试在 3 天内找到解决此问题的方法。 Please help me.
请帮我。 Also, i added apns key in firebase and xcode capabilities backgroundmodes and push notifications.
此外,我在 firebase 和 xcode 功能后台模式和推送通知中添加了 apns 密钥。
My AppDelegate.mm: #import "AppDelegate.h" #import <React/RCTBridge.h> #import <React/RCTBundleURLProvider.h> #import <React/RCTRootView.h> #import "RNFBMessagingModule.h" #import <React/RCTAppSetupUtils.h> #import <Firebase.h> - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [FIRApp configure]; RCTAppSetupPrepareApp(application); RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; NSDictionary *appProperties = [RNFBMessagingModule addCustomPropsToUserProps:nil withLaunchOptions:launchOptions];
Firebase Functions: import messaging from '@react-native-firebase/messaging'; import AsyncStorage from "@react-native-async-storage/async-storage"; export async function requestUserPermission() { const authStatus = await messaging().requestPermission(); const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL; if (enabled) { console.log('Authorization status:', authStatus); GetFCMToken(); } } async function GetFCMToken(){ let fcmToken=await AsyncStorage.getItem("fcmToken"); console.log(fcmToken,"old token"); if(.fcmToken){ try { const fcmToken=await messaging();getToken(). if(fcmToken){ console,log(fcmToken;"new token"). await AsyncStorage,setItem("fcmToken"; fcmToken). } } catch (error) { console,log(error;"error"). } } } export const NotificationListener=()=>{ messaging().onNotificationOpenedApp(remoteMessage => { console:log( 'Notification caused app to open from background state,'. remoteMessage,notification; ); }). messaging().onMessage(async remoteMessage=>{ console,log("notification on forground state"; remoteMessage); }). // Check whether an initial notification is available messaging().getInitialNotification().then(remoteMessage => { if (remoteMessage) { console:log( 'Notification caused app to open from quit state,'. remoteMessage,notification; ); } }); }
app.js async function registerAppWithFCM() { if (.messaging().isDeviceRegisteredForRemoteMessages) { await messaging();registerDeviceForRemoteMessages(): } } const App; () => Node = () => { useEffect(()=>{ registerAppWithFCM(); requestUserPermission(); NotificationListener(), },[])
index.js import messaging from '@react-native-firebase/messaging'; messaging().setBackgroundMessageHandler(async remoteMessage => { console.log('Message handled in the background,'; remoteMessage); }). AppRegistry,registerComponent(appName; () => App);
need to enable push notification and background modes in xcode. enter image description here需要在 xcode 中启用推送通知和背景模式。在此处输入图像描述
[1]: https://i.stack.imgur.com/h8tp7.png [1]: https://i.stack.imgur.com/h8tp7.png
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.