React-Native FCM在IOS上不起作用,但是Android可以

时间:2020-09-07 06:58:23

标签: firebase react-native react-native-ios react-native-firebase

我无法在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;

1 个答案:

答案 0 :(得分:0)

如果是iOS,则需要提供证书以发送通知。