反应本地firebase推动通知ios真实设备

发布于 2025-02-03 21:56:55 字数 3318 浏览 3 评论 0原文

我是全新的反应移动开发。我尝试使用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);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2025-02-10 21:56:55

需要在Xcode中启用推送通知和背景模式。 enter Image Description

https://i.sstatic.net/c7mvf.png“ rel =“ nofollow noreferrer”> [1]:https://i.sstatic.net/h8tp7.png

need to enable push notification and background modes in xcode.enter image description here

[1]: https://i.sstatic.net/h8tp7.png

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文