【问题标题】:React-Native FCM doesn't work on IOS but Android is OKReact-Native FCM 在 IOS 上不起作用,但 Android 可以
【发布时间】: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


    【解决方案1】:

    如果是 iOS,您需要提供一个证书来发送通知。

    【讨论】:

    • 配置没问题。我做他们添加功能等。
    猜你喜欢
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2020-09-20
    相关资源
    最近更新 更多