Onesignal not working in ionic 6

Hi,
i have a lot of apps with onesignal plugin, i have to update apps, but when i run application on Xcode the console return this error:

WARN: Native: tried calling OneSignal.startInit, but the OneSignal plugin is not installed.

WARN: Install the OneSignal plugin: ‘ionic cordova plugin add onesignal-cordova-plugin’

I already installed with this two command:

$ ionic cordova plugin add onesignal-cordova-plugin 
$ npm install @awesome-cordova-plugins/onesignal

I tried to follow the OneSignal official guide and substitute my code with them examples, but i need the getIds() function to return the user id

getPlayer_id(){
    return this.oneSignal.getIds()
  }

Someone can help me?

PS:If doesn’t exist, for me, is good to open an issue to fix it or change the ionic documentation guide

Hi,
I installed only onesignal-cordova-plugin for application (both ios and android).
import OneSignalPlugin from ‘onesignal-cordova-plugin’;//app

OneSignalPlugin.setAppId(environment.ONESIGNAL_APP_ID); // on device ready

and if you want to get the Player id, use:
OneSignalPlugin.getDeviceState((response) => {
//response.userId
})

Hi,
i’ve already tried it.
But if i want to do return the response value to use it in another service/component?
Example:

service.ts

...
getUserId(){
   let temp;
   OneSignal.getDeviceState( (response) => {
      temp = response.userId
   }
   return temp;
}

component.ts

this.service.getUserId().then( data => {
//some code
})

//other idea
this.playerId =. this.service.getUserId()

I’ve already tried with this two method but it doesn’t work, i tried to use async await but nothing.

My actually idea is to use nativeStorage and use it to exchange the value.

use promises

serviceFunction (){
return new Promise((resolve, reject) => {
OneSignal.getDeviceState( (response) => {
resolve(response.userId);
}
})
}

componentFunction(){
this.service.serviceFunction().then((userId) => {
//userId
})
}

Thanks for the reply. I thought about it before but didn’t know how to set it up.
Now I tried to use the example you gave me but it fails me, the promise fails due to an undefined value. Do I need to use await async?

getUserId(){
    return new Promise<any>((resolve, reject) => {
      OneSignal.getDeviceState((response) => {
        resolve(response.userId);
      })
    })
   }

error

Error: Uncaught (in promise): TypeError: window.cordova.exec is not a function. (In ‘window.cordova.exec(deviceStateCallback, function(){}, “OneSignalPush”, “getDeviceState”, )’, ‘window.cordova.exec’ is undefined)

I found the error, I was calling the init function in the wrong place respect to my program logic.
Thanks for the help @buffthus

I recommend to modify the ionic documentation following the new onesignal documentation

The current ionic documentation (and capacitor and onesignal) is an abomination when it comes to using onesignal with ionic 6. This comes mainly from the fact that at first the @awesome package added types for onesignal 1 and 2, but onesignal@3 added their own TS support.

I finally got it working by looking at one of the onesignal examples, Ill try to update the documentation as far as I can next week to make sure nobody has to waste their time on it.