0
votes

I am trying to set up push notifications with Ionic, mainly by following the official docs: https://docs.ionic.io/services/push/.

However I'm not sure where to call certain bits of code, e.g. $ionicPush.register(). Has anyone come across a recent / up-to-date tutorial that's helpful with this? Tutorials that I've come across use deprecated bits of code, e.g. ionic add.

1
are you using ionic user?Raj Sharma

1 Answers

0
votes

If you are using Ionic User as well here is the flow I use. These are various functions

    $scope.signupForm={};//user object
    $scope.beginSignUp=function(){
     //sign up 

      var details = {
        'email': $scope.signupForm.email,
        'password': $scope.signupForm.pass,
        'details':{
          'name':$scope.signupForm.dn,
          'image':$scope.signupForm.dp
        }
      } 

      Ionic.Auth.signup(details).then(signupSuccess, function(err){

      });
    }

    $scope.signupSuccess=function(){

        console.log("ionic sign up sucess, now login the user");
        var authProvider = 'basic';
        var authSettings = { 'remember': true };
        var loginDetails = {
          'email': $scope.signupForm.email,
          'password': $scope.signupForm.pass
        };

        Ionic.Auth.login(authProvider, authSettings, loginDetails)
        .then(authSuccess, function(err){


        });
    }

    function authSuccess(){

        console.log("Ionic Login Success ");
        var ionic_user = Ionic.User.current();
        ionic_user.details.name =$scope.signupForm.dn;
        ionic_user.details.image = $scope.signupForm.dp;
        ionic_user.save();


        registerPush();


        $ionicHistory.nextViewOptions({
              disableBack: true
        });

        $state.go('home', {}, {reload: true});
    }

function registerPush(){
   console.log("Calling Register Push");
   $ionicPush.init({
    "debug": true,
    "onNotification": function(notification) {
      var payload = notification.payload;
      console.log(notification, payload);
    },
    "onRegister": function(data) {
      console.log(data.token);
      Ionic.User.current();
      $ionicPush.saveToken(data.token);
      //unregister after checking

    },
    "pluginConfig": {
      "ios": {
        "badge": true,
        "sound": true
      },
      "android": {
         "iconColor": "#FA2B2E",
         "senderID": "GCM Project No.",
         "icon":"notification"
      }  
    } 
  });
  $ionicPush.register();
}

Use the model signupForm for storing user's email, password etc etc and call beginSignUp() on a button click. Or if it is a login call signupSuccess()