Im quite new react mobile development. I tried push notifications with firebase. It working nice on android emulator/real device. But its not working on ios real device. I've been trying to find a solution for this issue in 3 days. Please help me. Also, i added apns key in firebase and xcode capabilities backgroundmodes and push notifications.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.