在 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:
永远不会触发。
推送通知文档中建议不要将配置放在任何组件或应用程序内部, https://www.npmjs.com/package/react-native-push-notification#usage