OneSignal / OneSignal-Cordova-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your Ionic, PhoneGap CLI, PhoneGap Build, Cordova, or Sencha Touch app with OneSignal. Supports Android, iOS, and Amazon's Fire OS platforms. https://onesignal.com
Other
251 stars 197 forks source link

OneSignal.getDeviceState(), type definition wrong #738

Open ludufre opened 2 years ago

ludufre commented 2 years ago

Description:

The type definition of the method OneSignal.getDeviceState is Promise<DeviceState> but the correct call need a callback OneSignal.getDeviceState((state) => { .. }).

I'm using Ionic, so I import with import OneSignal from 'onesignal-cordova-plugin';. To use the correct call of the getDeviceState I had to use:

window['plugins'].OneSignal.getDeviceState(function (stateChanges) {
        console.log(stateChanges);
});

If I use as Promise I get error:

[ng] [console.error]: "Unhandled Promise rejection:" "deviceStateReceivedCallBack is not a function. (In 'deviceStateReceivedCallBack(new OSDeviceState(json))', 'deviceStateReceivedCallBack' is undefined)" "; Zone:" "<root>" "; Task:" "Promise.then" "; Value:" {} "deviceStateCallback@http://172.29.7.2:8100/vendor.js:188370:36\ncallbackFromNative@http://172.29.7.2:8100/cordova.js:295:63\n@http://172.29.7.2:8100/plugins/cordova-plugin-ionic-webview/src/www/ios/ios-wkwebview-exec.js:129:35\n@http://172.29.7.2:8100/polyfills.js:173:53\n@http://172.29.7.2:8100/polyfills.js:1331:41\n@http://172.29.7.2:8100/polyfills.js:218:61\ndrainMicroTaskQueue@http://172.29.7.2:8100/polyfills.js:633:46\npromiseReactionJob@[native code]"

Environment

cordova-ios@6.2.0
code-push 3.0.1 "CodePushAcquisition"
com-sarriaroman-photoviewer 1.2.4 "PhotoViewer"
com.darktalker.cordova.screenshot 0.1.5 "Screenshot"
com.googlemaps.ios 3.9.0 "Google Maps SDK for iOS"
cordova-plugin-add-swift-support 2.0.2 "AddSwiftSupport"
cordova-plugin-advanced-http 3.2.2 "Advanced HTTP plugin"
cordova-plugin-androidx-adapter 1.1.3 "cordova-plugin-androidx-adapter"
cordova-plugin-app-version 0.1.12 "AppVersion"
cordova-plugin-badge 0.8.8 "Badge"
cordova-plugin-camera 6.0.0 "Camera"
cordova-plugin-code-push 2.0.0 "CodePush"
cordova-plugin-device-motion 2.0.1 "Device Motion"
cordova-plugin-device 2.0.3 "Device"
cordova-plugin-dialogs 2.0.2 "Notification"
cordova-plugin-document-viewer 1.0.0 "SitewaertsDocumentViewer"
cordova-plugin-file 6.0.2 "File"
cordova-plugin-fingerprint-aio 4.0.2 "FingerprintAllInOne"
cordova-plugin-geolocation 4.1.0 "Geolocation"
cordova-plugin-googlemaps 2.7.1 "cordova-plugin-googlemaps"
cordova-plugin-inappbrowser 5.0.0 "InAppBrowser"
cordova-plugin-ionic-keyboard 2.2.0 "cordova-plugin-ionic-keyboard"
cordova-plugin-ionic-webview 5.0.0 "cordova-plugin-ionic-webview"
cordova-plugin-local-notification 0.9.0-beta.4 "LocalNotification"
cordova-plugin-nativegeocoder 3.4.1 "NativeGeocoder"
cordova-plugin-secure-storage-echo 5.1.1 "SecureStorage"
cordova-plugin-shake 0.7.0 "Shake Gesture Detection"
cordova-plugin-splashscreen 6.0.0 "Splashscreen"
cordova-plugin-statusbar 2.4.3 "StatusBar"
cordova-plugin-unique-device-id2 2.0.0 "UniqueDeviceID"
cordova-plugin-whitelist 1.3.5 "Whitelist"
cordova-plugin-zip 3.1.0 "cordova-plugin-zip"
cordova.plugins.diagnostic 6.1.0 "Diagnostic"
ionic-plugin-deeplinks 1.0.22 "Ionic Deeplink Plugin"
onesignal-cordova-plugin 3.0.0 "OneSignal Push Notifications"

Steps to Reproduce Issue:

Add import OneSignal from 'onesignal-cordova-plugin';

Try to call:

OneSignal.getDeviceState().then((deviceState) => {
        console.log(deviceState);
}, (err) => {
        console.error(err);   
});
nan-li commented 2 years ago

@ludufre Thank you, we will update this. In the meantime, does OneSignal.getDeviceState((state) => { .. }) work for you?

ludufre commented 2 years ago

@ludufre Thank you, we will update this. In the meantime, does OneSignal.getDeviceState((state) => { .. }) work for you?

I can't compile with that. I'm temporary have using:

window['plugins'].OneSignal.getDeviceState((state) => { .. });
davequested commented 2 years ago

+1 we have the same issue. We're using Ionic5, Capacitor3, OneSignal plugin v3.0.0. Can't use promise, have to use window['plugins'] notation.

7freaks-otte commented 2 years ago

Would love to see promises implemented. Until now the TypeScript types are just wrong in order to be able to compile I used:

(OneSignal as any).getDeviceState((status) => { ... });

Sad thing is that the purpose of types is gone that way unless I manually import DeviceState:

import { DeviceState } from 'onesignal-cordova-plugin/types/Subscription';
(OneSignal as any).getDeviceState((status: DeviceState) => { ... });

Alternatively wrap in Promise for async:

const status: DeviceState = await new Promise((resolve) => (OneSignal as any).getDeviceState(resolve));