【发布时间】:2020-09-07 06:58:23
【问题描述】:
我无法在 IOS 上发送推送通知。这适用于android但ios不起作用。我生成了一个本地 IOS 应用程序并检查了 firebase 和 APN 的集成。本机应用程序工作。我对配置没有任何问题。但是 React-Native 不起作用。
我的 pod 文件
pod 'RNFBApp', :path => '../node_modules/@react-native-firebase/app'
pod 'RNFBAnalytics', :path => '../node_modules/@react-native-firebase/analytics'
pod 'RNFBCrashlytics', :path => '../node_modules/@react-native-firebase/crashlytics'
pod 'RNFBMessaging', :path => '../node_modules/@react-native-firebase/messaging'
pod 'RNCPushNotificationIOS', :path => '../node_modules/@react-native-community/push-notification-ios'
Appdelegate.m
#import <Firebase.h>
[FIRApp configure];
App.JS
import React, {useEffect, useState} from 'react';
import firebase from '@react-native-firebase/app';
import '@react-native-firebase/messaging';
import PushNotification from 'react-native-push-notification';
import {Platform, SafeAreaView, TextInput, Text} from 'react-native';
const App = () => {
const [token, setToken] = useState('');
const [message, setMessage] = useState('-');
const getToken = () => {
firebase
.messaging()
.getToken(firebase.app().options.messagingSenderId)
.then((x) => console.log(x) || setToken(x))
.catch((e) => setMessage('error'));
};
const requestPermissions = () => {
firebase
.messaging()
.requestPermission()
.then((status) => {
if (status === 1) {
console.log('Authorized');
onMessage();
} else {
console.log('Not authorized');
}
})
.catch((e) => console.log(e));
};
const onMessage = () => {
console.log('onMessage callback added');
firebase.messaging().onMessage((response) => {
console.log(response);
showNotification(response.data.notification);
});
firebase.messaging().onNotificationOpenedApp((remoteMessage) => {
console.log('FIREBASE IOS Background', remoteMessage);
});
};
const showNotification = (notification) => {
console.log('Showing notification');
console.log(JSON.stringify(notification));
PushNotification.localNotification({
title: notification.title,
message: notification.body,
});
};
getToken();
if (Platform.OS === 'ios') {
requestPermissions();
} else {
onMessage();
}
return (
<SafeAreaView>
<TextInput value={token} />
<Text>{message}</Text>
</SafeAreaView>
);
};
export default App;
【问题讨论】:
标签: firebase react-native react-native-ios react-native-firebase