反应本地firebase推动通知ios真实设备
我是全新的反应移动开发。我尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
需要在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