TypeError:未定义不是一个对象(评估'_pushNotifications.pushNotifications.configure')React Native
2020-05-04
2546
我是 React Native 新手,正在尝试使用 push-notification-ios 和 react-native-push-notification 在 iOS 上创建推送通知。我正在关注一些不同的教程,因为我仍在学习它的工作原理。
当我运行我的应用程序时,我收到以下错误。
这是我的代码
const configure = async () => {
console.log('push notification configured');
PushNotificationIOS.addEventListener('registrationError', (e) => {
PushNotifcation.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 = '';
if (Platform.OS === 'ios') {
notifType = getNotificationType(
JSON.parse(notification.data.data).type,
);
} else {
notifType = getNotificationType(
notification.type,
);
}
//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,
});
});
}
export {
configure,
};
3个回答
第 5 行:您输入的是
PushNotifcation
,而不是
PushNotification
。
修复后的代码在这里:
const configure = async () => {
console.log('push notification configured');
PushNotificationIOS.addEventListener('registrationError', (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 = '';
if (Platform.OS === 'ios') {
notifType = getNotificationType(
JSON.parse(notification.data.data).type,
);
} else {
notifType = getNotificationType(
notification.type,
);
}
//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,
});
});
}
export {
configure,
};
Maycon Mesquita
2020-05-07
您已重新构建应用程序,请尝试:
-
yarn android
或者
-
cd android && ./gradlew clean && cd .. && react-native run-android
祝你好运 :)
Abd Elrahman Elshorafa
2022-01-19
你导入的路径肯定是错的
试试这个:
import PushNotification from 'react-native-push-notification';
import PushNotificationIOS from '@react-native-community/push-notificatio-ios';
Mirjalol
2022-05-10