簡體   English   中英

TypeError: Undefined is not an object (評估 '_pushNotifications.pushNotifications.configure') React Native

[英]TypeError: Undefined is not an object (evaluating '_pushNotifications.pushNotifications.configure') React Native

我是 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; };

第 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,
};

您已重新構建您的應用程序嘗試:

  • yarn android

或者

  • cd android &&./gradlew clean && cd.. && react-native run-android

祝你好運:)

您的導入必須是錯誤的路徑

嘗試這個:

import PushNotification from 'react-native-push-notification';
import PushNotificationIOS from '@react-native-community/push-notificatio-ios';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM