I’m trying to add push notifications to my Ionic app. However, even though the request sends with the correct tokens, I inevitably get a 400 error?
Here is the code (designed to register, get tokens and send back a push notification):
app.controller('DashCtrl', function ($scope, $rootScope, $ionicUser, $ionicPush, $cordovaPush, $http) {
// Identifies a user with the Ionic User service
$scope.identifyUser = function () {
$scope.user = $ionicUser.get();
if (!$scope.user.user_id) {
$scope.user.user_id = $ionicUser.generateGUID();
console.log($scope.user.user_id);
};
// Register with the Ionic Push service. All parameters are optional.
$ionicPush.register({
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) {
// Handle new push notifications here
// console.log(notification);
return true;
}
}, {
user_id: $scope.user.user_id,
});
$rootScope.$on('$cordovaPush:tokenReceived', function (event, data) {
$scope.deviceToken = data.token;
$scope.postFunction();
});
};
$scope.postFunction = function () {
console.log("Running function");
console.log("in device token");
var authorizeString = "Basic" + " " + btoa("my private key + a colon");
console.log(authorizeString);
var postConf = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Ionic-Application-Id': 'bb72e0a4',
'Authorization': authorizeString
}
};
var pushObject = {
"tokens": [
$scope.deviceToken
],
"notification": {
"alert": "Hello World!",
"ios": {
"badge": 1,
"sound": "ping.aiff",
"expiry": 1423238641,
"priority": 10,
"contentAvailable": true,
"payload": {
"key1": "value",
"key2": "value"
}
},
"android": {
"collapseKey": "foo",
"delayWhileIdle": true,
"timeToLive": 300,
"payload": {
"key1": "value",
"key2": "value"
}
}
}
};
console.log(pushObject.tokens[0]);
$http.post('https://push.ionic.io/api/v1/push', pushObject, [postConf]).success(console.log("Success!"));
console.log(pushObject);
};
});
Also, I can add users and see them on my dashboard, though it does display push as not configured in apps.ionic.io. Also, I do believe I have all the correct dependencies as I followed the Ionic Push Quick Start.
Thanks for any help in resolving this problem.