Hello, has anyone a successfull experience in using onesignal plugin with ionic (v8) capacitor (v7)?
I recently had to migrate a old ionic4 app that used the old version of onesignal plugin, but after importing “import { OneSignal } from ‘@awesome-cordova-plugins/onesignal/ngx’;” on android emulator i have this issue:
*Native: tried calling OneSignal.xxxxxxx, but the OneSignal plugin is not installed. *
It seems I must install like this “ionic cordova plugin add onesignal-cordova-plugin” but this is not possible inside capacitor project.
On official onesignal guide Ionic/Cordova/Capacitor SDK setup, there are some Instructions for Ionic + Capacitor, but they don’t say anything more, I should reproduce the functionalities that I used in old project, but i don’t really find any help on the web.
Here the code I should reproduce:
// 1. DEBUGGING
// 0 = NONE, 1 = FATAL, 2 = ERROR, 3 = WARN, 4 = INFO, 5 = DEBUG, 6 = VERBOSE
this.oneSignal.setLogLevel({logLevel: 5, visualLevel: 0});
// 2. INIT PROCESS
// startInit('onesignal app id','google firebase project number')
this.oneSignal.startInit('xxxxxxxxx', 'yyyyyyy');
// 3. DISPLAYING
// to control when to show notifications while using smartphone
this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.Notification);
// 4. EVENTS
// Received handler: Called when the app receives a notification. Do something when a notification is received.
this.oneSignal.handleNotificationReceived().subscribe(data => {
alert('>>>>> Notifica ricevuta: '+JSON.stringify(data, null, 3));
});
// Opened handler: Called when the user opens or taps an action on a notification. Do something when a notification is opened
this.oneSignal.handleNotificationOpened().subscribe(data => {
console.log('>>>>Notification opened: '+JSON.stringify(data));
});
// Test to see if user has already granted consent: If user hasn't granted consent then
window.plugins.OneSignal.setRequiresUserPrivacyConsent(true);
// Display a modal window asking for GDPR consent: Capture the consent (or not) and update OneSignal
window.plugins.OneSignal.provideUserConsent(true);
//to get onesignalPlayerId when subscribed
this.oneSignal.addSubscriptionObserver().subscribe( state => {
if (!state.from.subscribed && state.to.subscribed) {
alert(">>>>Subscribed for OneSignal push notifications!");
state.to.userId // get player id
alert(">>>>Push Subscription state changed: "+JSON.stringify(state));
let onesignalPlayerId = state.to.userId;
alert(">>>>onesignalPlayerId: "+onesignalPlayerId);
this.initDevice(uuid,onesignalPlayerId);
}
});
this.oneSignal.getPermissionSubscriptionState().then(res =>{
let status: any = res;
alert(">>>>>>getPermissionSubscriptionState: "+JSON.stringify(status));
status.permissionStatus.hasPrompted; // Bool
status.permissionStatus.status; // iOS only: Integer: 0 = Not Determined, 1 = Denied, 2 = Authorized
status.permissionStatus.state; //Android only: Integer: 1 = Authorized, 2 = Denied
status.subscriptionStatus.subscribed; // Bool
status.subscriptionStatus.userSubscriptionSetting; // Bool
status.subscriptionStatus.userId; // String: OneSignal Player ID
status.subscriptionStatus.pushToken; // String: Device Identifier from FCM/APNs
});
// 5. ENDINIT PROCESS
this.oneSignal.endInit();
}
and then
this.oneSignal.setExternalUserId(externalUserId)
But nothing of these functions seem to be available in the suggested plugin in the guide.
I know there is a new approach based on firebase google messaging and capacitor push notifications plugin Push Notifications - Firebase | Capacitor Documentation, but we really should be able to make onesignal works since we have many applications based on that.
Any help would be really appreciated, thank a lot.