Created
May 12, 2015 17:40
-
-
Save janewang/4f06a530024df3ba1f66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function($http, $cordovaPush, $ionicApp, $ionicUser, $rootScope, $log, $q) { | |
// Grab the current app | |
var app = $ionicApp.getApp(); | |
//Check for required credentials | |
if(!app || !app.app_id) { | |
console.error('PUSH: Unable to initialize, you must call $ionicAppProvider.identify() first'); | |
} | |
function init(options, metadata) { | |
var defer = $q.defer(); | |
// TODO: This should be part of a config not a direct method | |
var gcmKey = $ionicApp.getGcmId(); | |
var api = $ionicApp.getValue('push_api_server'); | |
//Default configuration | |
var config = { | |
"senderID": gcmKey, | |
"badge": true, | |
"sound": true, | |
"alert": true | |
}; | |
$cordovaPush.register(config).then(function(token) { | |
console.log('$ionicPush:REGISTERED', token); | |
defer.resolve(token); | |
if(token !== 'OK') { | |
// Success -- send deviceToken to server, and store | |
var req = { | |
method: 'POST', | |
url: api + "/api/v1/register-device-token", | |
headers: { | |
'X-Ionic-Application-Id': $ionicApp.getId(), | |
'X-Ionic-API-Key': $ionicApp.getApiKey() | |
}, | |
data: { | |
ios_token: token, | |
metadata: metadata | |
} | |
}; | |
// Push the token into the user data | |
try { | |
$ionicUser.push('push.ios_tokens', token); | |
} catch(e) { | |
console.warn('Received push token before user was identified and will not be synced with ionic.io. Make sure to call $ionicUser.identify() before calling $ionicPush.register.'); | |
} | |
$http(req) | |
.success(function (data, status) { | |
console.log('Register success', JSON.stringify(data)); | |
}) | |
.error(function (error, status, headers, config) { | |
console.log('Register error! Code:', status, error, headers); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment