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
250 stars 197 forks source link

TypeError: Cannot read property 'OneSignal' of undefined #305

Closed noor07 closed 6 years ago

noor07 commented 6 years ago

Runtime Error Uncaught (in promise): TypeError: Cannot read property 'OneSignal' of undefined TypeError: Cannot read property 'OneSignal' of undefined at http://localhost:8100/build/main.js:803:31 at t.invoke (http://localhost:8100/build/polyfills.js:3:14976) at Object.onInvoke (http://localhost:8100/build/vendor.js:4982:33) at t.invoke (http://localhost:8100/build/polyfills.js:3:14916) at r.run (http://localhost:8100/build/polyfills.js:3:10143) at http://localhost:8100/build/polyfills.js:3:20242 at t.invokeTask (http://localhost:8100/build/polyfills.js:3:15660) at Object.onInvokeTask (http://localhost:8100/build/vendor.js:4973:33) at t.invokeTask (http://localhost:8100/build/polyfills.js:3:15581) at r.runTask (http://localhost:8100/build/polyfills.js:3:10834)

Hey Everyone, i got an error in IONIC 3, i already install the plugin and go through the onesignal docs, but there is still an error

T-Munyaka commented 6 years ago

Hi there, you can no longer run 'ionic serve' when you have this plugin installed, that is why you see this error. You will need to emulate your app / run it on a real device OR simply click the 'close' button and you can still view your app, but whenever you make an update, the error will show again.

charis-the-programmer commented 6 years ago

@T-Munyaka @noor07

In order to avoid this problem, you can put your one signal initialization code in an if statement that check whether you are running the app on a device or a browser. Change the code in your app.component.ts to this:

// check if in browser or on device before running OneSignal Code
      if(platform.is('core') || platform.is('mobileweb')) {
        console.log("Platform is core or is mobile web");
      } else {
        var notificationOpenedCallback = function(jsonData) {
          console.log('notificationOpenedCallback: ' + JSON.stringify(jsonData));
        };

        window["plugins"].OneSignal
          .startInit("<< APPID>>", "<<GOOGLE PROJECT ID>>")
          .handleNotificationOpened(notificationOpenedCallback)
          .endInit(); 
      }
YnsDev1 commented 6 years ago

@charis-the-programmer

your code it's not working for me the error still showing

charis-the-programmer commented 6 years ago

lets see your code in app.component.ts

ahayder commented 6 years ago

@charis-the-programmer your code is working for me.