3

I'm trying to initialize OneSignal to a PhoneGap application.

I tried many ways. But always it gives following error,

Uncaught TypeError: Cannot read property 'OneSignal' of undefined", source: file:///android_asset/www/js/app.js error.

This is my app.js file

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
oneSignal();
checkConnection();}

function oneSignal(){
console.log("It is calling");
    var notificationOpenedCallback = function(jsonData) {
        console.log('didReceiveRemoteNotificationCallBack: ' + JSON.stringify(jsonData));
    };

    window.plugins.OneSignal.init("my app id",
                             {googleProjectNumber: "my project number"},
                             notificationOpenedCallback);



 // Show an alert box if a notification comes in when the user is in your app.
    window.plugins.OneSignal.enableInAppAlertNotification(true);}

Somebody please help me to initialize OneSignal to my PhoneGap application.

Hardik Leuwa
  • 3,282
  • 3
  • 14
  • 28
Choxmi
  • 1,584
  • 4
  • 29
  • 47
  • Onesignal, can you please post the plugin link? The source from where you got it? Also the purpose for the same. – Kailas Apr 18 '16 at 06:59
  • [link](https://onesignal.com/) This plugin is for push notifications. – Choxmi Apr 18 '16 at 07:02
  • Did you install the plugin, and cross verified it exists in the plugin folder? The error usually comes in case the plugin was not installed successfully. – Kailas Apr 18 '16 at 07:24
  • 1
    Yes I installed the plug-in. Thank you very much for your help. And now it is working. It was a duplication problem of config.xml file. Thanks again for the help. – Choxmi Apr 18 '16 at 07:38
  • 1
    There are three (3) different systems for Corodova/Phonegap [CLI, SDK or Build](https://github.com/jessemonroy650/top-phonegap-mistakes/blob/master/new-to-Phonegap.md#001). You are using CLI. *Phonegap Build* is a cloud-based build system by Adobe/Phonegap. I have updated your tags. –  Apr 23 '16 at 03:47

2 Answers2

1

Just try installing your plugin, cross verify for it's availability in the plugins folder. Also confirm the corresponding update has been made in config.xml and fetch.json (in the plugins folder). The error usually arises in case the plugin is not installed properly.

Hope this helps... :)

Kailas
  • 7,350
  • 3
  • 47
  • 63
1

The best way to initialize OneSignal is by adding the code in your controller.js file and make sure its in one of your controllers - it might be login, logout, anything.

in other words you need to add the code inside a set controller that responds to a certain action.

Cant explain it better but try that -..

Good luck