react-native-push-notification onRegister 正确触发,但 onNotification 从未捕获

问题描述 投票:0回答:1

在 Android 上我没有问题,在 iOS 上我能够捕获

onRegister
事件并正确获取令牌,但是当我测试推送通知时,事件
onNotification
似乎不起作用:它永远不会触发。

我的配置代码是:

const configure = async () => {
    console.log('push notification configured');
    PushNotificationIOS.addEventListener('registrationError', (e) => { alert(JSON.stringify(e)) });

    PushNotification.configure({

        onRegister: function (token) {
            //process token
            alert('Token! ' + JSON.stringify(token));
            console.log('[CATCHED] onRegister:', token);
            db.setToken(token).catch(
                console.log('[ERROR] device push token has not been saved on the database'),
            );
        },

        onNotification: async function (notification) {
            console.log('[CATCHED] onNotification: ' + JSON.stringify(notification));
            let notifType = '';
            //console.log('notification received: ' + JSON.stringify(notification));
            if (Platform.OS === 'ios') {
                notifType = getNotificationType(
                    JSON.parse(notification.data.data).type,
                );
            } else {
                notifType = getNotificationType(
                    notification.type,
                );
            }
            //console.log('notification type: >>>>>>>>>>>>>>>> ' + notifType);
            //console.log('notification s: ------------------> ' + JSON.stringify(notification));
            switch (notifType) {
                {...}
            }
            // process the notification
            // required on iOS only
            if (Platform.OS === 'ios') {
                notification.finish(PushNotificationIOS.FetchResult.NoData);
            }

        },
        senderID: '-----',

        permissions: {
            alert: true,
            badge: true,
            sound: true,
        },

        popInitialNotification: true,
        requestPermissions: true,
    });
};

在 iOS 中,控制台日志

[CATCHED] onRegister:
会正确触发,但
[CATCHED] onNotification:
永远不会触发。

ios react-native push-notification
1个回答
0
投票

推送通知文档中建议不要将配置放在任何组件或应用程序内部, https://www.npmjs.com/package/react-native-push-notification#usage

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.