簡體   English   中英

React Native Firebase 推送通知 iOS 真實設備

[英]React Native Firebase Push Notification iOS Real Device

我是相當新的 React 移動開發。 我嘗試使用 firebase 推送通知。它在 android 模擬器/真實設備上運行良好。 但它不適用於 ios 真實設備。 我一直在嘗試在 3 天內找到解決此問題的方法。 請幫我。 此外,我在 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);

需要在 xcode 中啟用推送通知和背景模式。在此處輸入圖像描述

[1]: https://i.stack.imgur.com/h8tp7.png

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM