首页 > 解决方案 > React Native OneSignal:TypeError:this.OSLog 不是函数

问题描述

我在我的应用程序上使用 OneSignal 进行推送通知。按照他们的教程(https://documentation.onesignal.com/docs/react-native-sdk-setup)之后,一切似乎都在 Android 上运行。但是在IOS上我收到了这个错误:

在此处输入图像描述

我从 OneSignal 教程中复制了完全相同的代码:

// Root.js

import React from 'react';
import OneSignal from 'react-native-onesignal';
import { ONESIGNAL_KEY } from '@env';
import { Provider } from 'react-redux';

import { ThemeProvider } from 'styled-components';
import { theme } from 'utils/theme';
import store from 'store';
import App from './App';

class Root extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      isSubscribed: false,
    };
  }

  async componentDidMount() {
    // /* O N E S I G N A L   S E T U P */
    OneSignal.setAppId(ONESIGNAL_KEY);
    OneSignal.setLogLevel(6, 0);
    OneSignal.setRequiresUserPrivacyConsent(false);
    OneSignal.promptForPushNotificationsWithUserResponse((response) => {
      this.OSLog('Prompt response:', response);
      console.log('Prompt response:', response);
    });

    /* O N E S I G N A L  H A N D L E R S */
    OneSignal.setNotificationWillShowInForegroundHandler(
      (notifReceivedEvent) => {
        this.OSLog(
          'OneSignal: notification will show in foreground:',
          notifReceivedEvent,
        );
        let notif = notifReceivedEvent.getNotification();
        const button1 = {
          text: 'Cancel',
          onPress: () => {
            notifReceivedEvent.complete();
          },
          style: 'cancel',
        };
        const button2 = {
          text: 'Complete',
          onPress: () => {
            notifReceivedEvent.complete(notif);
          },
        };
        Alert.alert('Complete notification?', 'Test', [button1, button2], {
          cancelable: true,
        });
      },
    );
    OneSignal.setNotificationOpenedHandler((notification) => {
      this.OSLog('OneSignal: notification opened:', notification);
    });
    OneSignal.setInAppMessageClickHandler((event) => {
      this.OSLog('OneSignal IAM clicked:', event);
    });
    OneSignal.addEmailSubscriptionObserver((event) => {
      this.OSLog('OneSignal: email subscription changed: ', event);
    });
    OneSignal.addSubscriptionObserver((event) => {
      this.OSLog('OneSignal: subscription changed:', event);
      console.log({ event });
      this.setState({ isSubscribed: event.to.isSubscribed });
    });
    OneSignal.addPermissionObserver((event) => {
      this.OSLog('OneSignal: permission changed:', event);
    });
    const deviceState = await OneSignal.getDeviceState();
    this.setState({
      isSubscribed: deviceState.isSubscribed,
    });
  }

  render() {
    return (
      <Provider store={store}>
        <ThemeProvider theme={theme}>
          <App />
        </ThemeProvider>
      </Provider>
    );
  }
}

export default Root;

如果我评论该行this.OSLog('Prompt response:', response);,则错误在 IOS 上消失

知道可能导致此错误的原因是什么吗?

谢谢

标签: react-nativeonesignalreact-native-onesignal

解决方案


由于 OSLog 是由它引用的,因此您可以 - 就像您一样 - 与 console.log() 交换它。它是 App.js 中类的本地函数


推荐阅读