0

I'm trying to start with OneSignal SDK on a Ionic with React app, but I can't find any guide for React, all I find is for angular and I'm not pratice of typescript.

Where do I need to initialize OneSignal SDK?

I'm trying to do it in the App.componentDidMount such as:

componentDidMount() {
    this.props.oneSignal.startInit(
        "XXXXXX-XXX-XXX-X-XXXXX",
        "YYYYYYYYYY"
    );

    this.props.oneSignal.inFocusDisplaying(
        this.props.oneSignal.OSInFocusDisplayOption.InAppAlert
    );

    this.props.oneSignal.handleNotificationReceived().subscribe(() => {
        // do something when notification is received
    });

    this.props.oneSignal.handleNotificationOpened().subscribe(() => {
        // do something when a notification is opened
    });

    this.props.oneSignal.endInit();
}

but the IDE continues giving me errors about this.oneSignal does not exists in the type xxx.

Antonello Gatto
  • 1,603
  • 2
  • 16
  • 29

1 Answers1

1

My fault. I was importing this way:

import { OneSignal } from '@ionic-native/onesignal/ngx';

This the correct way for React.js:

import { OneSignal } from '@ionic-native/onesignal';
Antonello Gatto
  • 1,603
  • 2
  • 16
  • 29
  • How would this be used inside of a functional component constructor rather than the class structure? I'm not having much luck have an instance of OneSignal to work with. – Nick Jul 28 '20 at 21:43
  • @Nick just creating an instance let oneSignal = OneSignal.startInit(api_key, app_id); then all operations can be done on the instance such as oneSignal.addSubscriptionObserver() etc – Antonello Gatto Jul 29 '20 at 23:37