Skip to content

Instantly share code, notes, and snippets.

@meyerzinn
Created October 5, 2015 02:38
Show Gist options
  • Save meyerzinn/35bf5a1baff40154a41d to your computer and use it in GitHub Desktop.
Save meyerzinn/35bf5a1baff40154a41d to your computer and use it in GitHub Desktop.
Ionic Push

First, be sure to configure $ionicAppProvider (do so right after declaring the app module), like so:

app.config(['$ionicAppProvider', function($ionicAppProvider) {
  // Identify app
  $ionicAppProvider.identify({
    // Your App ID
    app_id: 'xxx',
    // The public API key services will use for this app
    api_key: '80xxxxxx',
    // Your GCM sender ID/project number (Uncomment if supporting Android)
    dev_push: false, // I am not in dev stage. Set true if true.
    gcm_id: 'xxx'
    
  });

}])

The next part could be done several ways. I chose to make a controller and activate it via the DOM (to ensure it is loaded after the actuall program so it doesn't hold up the user).

.controller('AppCtrl', function($scope, $rootScope, $ionicPush, $ionicUser, $ionicPlatform, $ionicPopup) {
  $rootScope.$on('$cordovaPush:tokenReceived', function(event, data) {
    console.log('Got token', data.token, data.platform);
  });
  //Basic registration
  $scope.pushRegister = function() {
      $ionicPlatform.ready(function (){
    $ionicPush.register({
      debug: true,
      canShowAlert: true, //Can pushes show an alert on your screen?
      canSetBadge: true, //Can pushes update app icon badges?
      canPlaySound: true, //Can notifications play a sound?
      canRunActionsOnWake: true, //Can run actions outside the app,
      onNotification: function(notification) {
            var payload = notification.payload;
            console.log(notification, payload);
            $ionicPopup.alert(notification);
      }
    }).then(function(deviceToken) {
        $scope.token = deviceToken;
    });
      }
    );
  }
  $scope.identifyUser = function() {
    console.log('Identifying user');

    $scope.user = $ionicUser.get();
    if(!$scope.user.user_id) {
      // Set your user_id here, or generate a random one
      $scope.user.user_id = $ionicUser.generateGUID()
    };

    $ionicUser.identify($scope.user);
  }
})

Finally, for the DOM part:

<div ng-controller="AppCtrl" ng-init="identifyUser(); pushRegister()"></div>

And bam!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment