React Native:Firebase通知未显示

时间:2019-08-19 13:30:47

标签: firebase react-native firebase-cloud-messaging firebase-notifications react-native-firebase

当尝试从Firebase发送测试通知时,我的应用程序未收到任何事件。在通知编写器中,我专门选择了我的iOS设备的fcmToken

Notification composer

我已检查我是否使用了正确的fcmToken和身份验证密钥。

与我使用的react-native-firebase的版本有关吗?还是初始化例程在react-navigator中?

  • react-native-firebase@5.5.6
  • react-navigation@3.11.1

Navigator.js

export default class Navigator extends Component {
    constructor(props) {
        super(props)
    }

    async componentDidMount() {
        FirebaseService.checkPermission();

FirebaseService.createNotificationListeners();         }

    //Remove listeners allocated in createNotificationListeners()
    componentWillUnmount() {
        FirebaseService.notificationListener();
        FirebaseService.notificationOpenedListener();
    }

    render() {
        return (
            <View style={{ flex: 1 }}>
                    <Route
                        onNavigationStateChange={(prevState, currentState) => {
                            const currentScreen = getActiveRouteName(currentState);
                            const prevScreen = getActiveRouteName(prevState);
                            nav.activeRouteName = currentScreen;

                            if (prevScreen !== currentScreen) {

                                if (currentScreen == "captureView") {
                                    this.resetSafearea("never")
                                } else {
                                    this.resetSafearea("always")
                                }
                            }
                        }}
                    />
            </View>
        );
    }
}

FirebaseService.js

import AsyncStorage from '@react-native-community/async-storage';
import Firebase from "react-native-firebase";

export default class FirebaseService {

    //1
    static checkPermission = async () => {
        const enabled = await Firebase.messaging().hasPermission();
        if (enabled) {
            FirebaseService.getToken();
        } else {
            FirebaseService.requestPermission();
        }
    }

    //2
    static requestPermission = async () => {
        try {
            await Firebase.messaging().requestPermission();
            // User has authorised
            FirebaseService.getToken();
        } catch (error) {
            // User has rejected permissions
            console.log('permission rejected');
        }
    }

    //3
    static getToken = async () => {
        let fcmToken = await AsyncStorage.getItem('fcmToken');
        if (!fcmToken) {
            fcmToken = await Firebase.messaging().getToken();
            if (fcmToken) {
                // user has a device token
                await AsyncStorage.setItem('fcmToken', fcmToken);
            }
        }
        return fcmToken;
    }

    static createNotificationListeners = async () => {
        /*
        * Triggered when a particular notification has been received in foreground
        * */
        FirebaseService.notificationListener = Firebase.notifications().onNotification((notification) => {
            const { title, body } = notification;
            console.log("notification", notification);
        });

        /*
        * If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
        * */
        FirebaseService.notificationOpenedListener = Firebase.notifications().onNotificationOpened((notificationOpen) => {
            const { title, body } = notificationOpen.notification;
            console.log("notification", notification);
        });

        /*
        * If your app is closed, you can check if it was opened by a notification being clicked / tapped / opened as follows:
        * */
        const notificationOpen = await Firebase.notifications().getInitialNotification();
        if (notificationOpen) {
            const { title, body } = notificationOpen.notification;
            console.log("notification", notification);
        }
        /*
        * Triggered for data only payload in foreground
        * */
        FirebaseService.messageListener = Firebase.messaging().onMessage((message) => {
            //process data message
            console.log("message", JSON.stringify(message));
        });

        console.log("Notification listeners created.");
    }       
}

1 个答案:

答案 0 :(得分:0)

原因是我的系统上没有安装推送通知证书(显示它没有错误)。

您将需要一个用于开发,一个需要用于生产https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_certificate-based_connection_to_apns