Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular and ionicPush not working on android

Trying to migrate to ionicPush but getting errors anyway I do it:

Option 1 - Angular method

When I put $ionicPush.init as per guide in the app.js or anywhere for that matter, getting:

Uncaught TypeError: $ionicPush.init is not a function

When I check $ionicPush it has 2 methods, register and unregister. So clearly it gets imported, but for whatever reason doesnt have .init

Top of app.js looks like this:

.run(function(AppRootService, $ionicPlatform, $ionicPush, $cordovaSplashscreen,$window, $timeout) {
    $ionicPlatform.ready(function() {
    $ionicPush.init({
        "debug": true,
        "onNotification": function(notification) {
            var payload = notification.payload;
            console.log(notification, payload);
              },
        "onRegister": function(data) {
            console.log(data.token);
              }
        });

    $ionicPush.register();

Option 2 - Regular JS way

Put this code in app.js after $ionicPlatform.ready()

var push = new Ionic.Push({
          "debug": true,
          "onNotification": function(notification) {
            var payload = notification.payload;
            console.log(notification, payload);
          },
          "onRegister": function(data) {
            console.log(data.token);
          }
        });

        push.register(function(token) {
          console.log("Device token:",token.token);
        });
        Ionic.io();

Still nothing, this time error is Uncaught ReferenceError: Ionic is not defined

Ran both of these:

ionic add ionic-platform-web-client
ionic plugin add phonegap-plugin-push

Moved around Ionic.io(), not luck

like image 716
rodling Avatar asked Jan 29 '16 04:01

rodling


1 Answers

I think you have to init io and activate debug/dev mode.

Here is the all steps...

ionic add ionic-platform-web-client
ionic plugin add phonegap-plugin-push
ionic io init
ionic config set dev_push true

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    var push = new Ionic.Push({
      "debug": true
    });

    push.register(function(token) {
      console.log("Device token:",token.token);
    });
  });
})

Checkout this page for setting up push for platforms (iOS, Android) http://docs.ionic.io/docs/push-from-scratch

like image 112
karma Avatar answered Sep 18 '22 05:09

karma