0

I am trying to implement the pushbots code into my ionic app to have push notifications via - Pushbots docs

What I can't seem to figure out is where the following code goes:

if(PushbotsPlugin.isAndroid()){
    PushbotsPlugin.initializeAndroid("PUSHBOTS_APP_ID", "GCM_SENDER_ID");
}

does it go in the below code if so where abouts:

    .run(function($ionicPlatform, $ionicAnalytics, $window) {

  $ionicPlatform.ready(function() {


  /*  $ionicAnalytics.register();*/
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }

    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

or would it go in the config?

Sole
  • 3,100
  • 14
  • 58
  • 112

1 Answers1

0

The initialization code for Pushbots should go into a function that is called when the 'deviceready' event is fired. In your case, given you have an Ionic project, the code should go into a $ionicPlatform.ready(function() {}) code block.

You can use the existing block:

.run(function($ionicPlatform, $ionicAnalytics, $window) {

  $ionicPlatform.ready(function() {
    if(PushbotsPlugin.isAndroid()){
        PushbotsPlugin.initializeAndroid("PUSHBOTS_APP_ID", "GCM_SENDER_ID");
    }

    /*  $ionicAnalytics.register();*/
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }

    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

Or you can create a separate block, to keep things clean and untangled:

.run(function($ionicPlatform, $ionicAnalytics, $window) {

  $ionicPlatform.ready(function() {
    if(PushbotsPlugin.isAndroid()){
        PushbotsPlugin.initializeAndroid("PUSHBOTS_APP_ID", "GCM_SENDER_ID");
    }
  });

  $ionicPlatform.ready(function() {
    /*  $ionicAnalytics.register();*/
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }

    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})
Alin Pandichi
  • 955
  • 5
  • 15
  • Hi Alin, i get a error in the console which is: Uncaught ReferenceError: PushbotsPlugin is not defined – Sole Aug 17 '15 at 15:48
  • You need to install the plugin (*cordova plugin add com.pushbots.push*) and add the script tag to your index.html: ** – Alin Pandichi Aug 17 '15 at 15:50
  • Actually, scratch that: the script tag should not be necessary – Alin Pandichi Aug 17 '15 at 15:59
  • With that script tag i get the error: Uncaught ReferenceError: module is not defined and without the tag i still get that error – Sole Aug 17 '15 at 16:15
  • 1
    Before the "module is not defined" error, isn't there any other error? Here's a blogpost of an example app with Pushbots: http://thejackalofjavascript.com/pushbots-and-cordova/ There's no mention of the script tag being required. And it shouldn't be required, Cordova should handle the loading of the plugin JS file on its own. When I mentioned the script tag, I was thinking about something else (bower components, not cordova plugins) – Alin Pandichi Aug 17 '15 at 16:17
  • Hi @AlinPandichi I followed your link, I am able to register my device in pushbot's but unable to receive the notification to the device's and in pushbot's Dashboard it is showing as message delivered ... If you have any working example please post it – Anil kumar Aug 18 '15 at 05:23
  • Here is the GitHub repo with the demo app done by the same guy: https://github.com/arvindr21/pushbots-cordova – Alin Pandichi Aug 18 '15 at 06:36