React Native OneSignal:TypeError:this.OSLog 不是函数

问题描述

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

enter image description here

我从 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 上消失

知道是什么导致了这个错误吗?

谢谢

解决方法

由于 OSLog 是由这个引用的,你可以 - 就像你所做的那样 - 用 console.log() 交换它。 它是 App.js 中类的本地函数