0

我正在使用react-native-notifications并且前台通知在 iOS 上正常工作,但仍显示在 Android 模拟器上。我没有要测试的 Android 设备。

这段代码中有什么我遗漏的吗?

import React from 'react'
import {Platform, View} from 'react-native'
import { Notifications } from 'react-native-notifications'
import API from '../models/api';
import DeviceInfo from 'react-native-device-info'
import {connect} from 'react-redux';

class PushNotifications extends React.Component<any, any> {

    public async componentDidMount() {
        if (await DeviceInfo.isEmulator() && Platform.OS === 'ios') return null;
        this.registerDevice()
        this.registerNotificationEvents()
    }

    public registerDevice = () => {
        console.log(TAG, 'registering device');
        Notifications.events().registerRemoteNotificationsRegistered(async event => {
                // Save device
                // ...
        })
        Notifications.events().registerRemoteNotificationsRegistrationFailed(event => {
            console.error(event)
        })

        Notifications.registerRemoteNotifications()
    }

    public registerNotificationEvents = () => {
        Notifications.events().registerNotificationReceivedForeground((notification, completion) => {
            console.log('Notification Received - Foreground', notification)
            // Calling completion on iOS with `alert: true` will present the native iOS inApp notification.
            completion({ alert: false, sound: false, badge: false })
        })

        Notifications.events().registerNotificationOpened(async (notification:any, completion) => {
            console.log('Notification opened by device user', notification)
            // console.log(`Notification opened with an action identifier: ${notification.identifier}`);

            // ...

            completion()
        })

        Notifications.events().registerNotificationReceivedBackground((notification, completion) => {
            console.log('Notification Received - Background', notification)

            // Calling completion on iOS with `alert: true` will present the native iOS inApp notification.
            completion({ alert: true, sound: true, badge: true })
        })

        Notifications.getInitialNotification()
            .then(notification => {
                console.log('Initial notification was:', notification || 'N/A')
            })
            .catch(err => console.error('getInitialNotifiation() failed', err))
    }

    render() {
        return <View/>
    }
}

const s2p = state => ({
    userId: state.user._id
});

export default connect(s2p)(PushNotifications);
4

0 回答 0