I am trying to use Push notifications in an Ionic 2 ts project. I have followed the quick guide in this post to get webpack working:
Now when I try to call Push.init, it does so and then gives this warning message
Native: tried calling Push.init, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator
when I use ionic serve. Even when I uncomment the cordova.js script html tags I get this error. I understand that Push notifications haven’t been tested in Ionic 2 but I think this is related to something else since the error is about cordova. How can I get ionic-native to see cordova?
Like the error message mentioned, you’re trying to use cordova but it is not available. This is because you’re trying to use a cordova plugin in your browser (ionic servce). To my recollection, cordova is not accessible when you’re working in a browser. You should deploy your app to your device/emulator, then the cordova plugins should work.
You’re building hybrid app using ionic, not web app. Cordova is there to provide interface/bridge for browser engine to interact with device native api, which include, Push. If you’re building a web app, you can’t access device push because normal browser don’t provide those interface. Like myself, I hardly ever touch browser for development. Instead, I build directly on device and use remote debugging to view any error.
What I do is use Chrome’s Developer Tools. It has a “remote devices” option that allows attaching to apps running on device. There are probably Ionic CLI ways of running it, but I just use ionic cordova build android and adb install.
Native plugins, with a few exceptions, won’t work in a browser at all, and I don’t think any of them work under ionic serve.