We are experiencing two problems with iOS only, but the same code is working with Android, and is also working with all of our iOS devices in dev. These problems are:
1. Our app seems to register users only occasionally on iOS (so i believe we have a problem with $ionicUser.identify)
2. Our app doesn’t seem to register iOS device tokens in production (works fine for developer, so I believe we have a problem with our certificates)
angular.module('servishero.services.push', [])
.factory('PushService', function ($state, $q, $rootScope, $cordovaDialogs, localStorageService, $ionicUser, $ionicPush, $ionicPushActions, AuthenticationService, CustomerService, API) {
$rootScope.$on('$cordovaPush:tokenReceived', function (event, data) {
console.log('Got token', data.token, data.platform);
CustomerService.saveCustomerDeviceToken(AuthenticationService.getUser(), data.token, API);
});
var popupAndRedirect = function (notification, message, title) {
$cordovaDialogs.confirm(message, title, ['OK', 'Cancel'])
.then(function (buttonIndex) {
// no button = 0, 'OK' = 1, 'Cancel' = 2
if (buttonIndex == 1) {
$ionicPushActions.run(notification);
}
});
}
var popup = function (message, title) {
$cordovaDialogs.alert(message, title, 'OK');
}
var handleAndroidPush = function (notification) {
console.log(notification);
if (notification.event == 'registered') {
// handled in $rootscope
} else if (notification.event == 'message') {
var push = notification.payload;
var message = push.message;
try {
var state = push.payload["$state"];
var title = push.payload["$title"] ? push.payload["$title"] : "ServisHero";
} catch(ex) {
console.log(ex);
}
if (state) {
popupAndRedirect(notification, message, title);
} else {
popup(message, title);
}
} else if (notification.event == 'error') {
// Do something
}
}
var handleIosPush = function (notification) {
console.log(notification);
var message = notification["alert"];
var title = notification["$title"] ? notification["$title"] : "ServisHero";
var state = notification["$state"];
if (state) {
popupAndRedirect(notification, message, title);
} else {
popup(message, title);
}
}
return {
register: function (id, mobile, email, name) {
// register with Ionic
$ionicUser.identify({
user_id: "" + id,
email: email,
name: name,
mobile: mobile
})
.then(function (returned) {
if (ionic.Platform.isIOS() || ionic.Platform.isAndroid()) {
$ionicPush.register({
canShowAlert: true, //Should new pushes show an alert on your screen?
canSetBadge: true, //Should new pushes be allowed to update app icon badges?
canPlaySound: true, //Should notifications be allowed to play a sound?
canRunActionsOnWake: false, // Whether to run auto actions outside the app
onNotification: function (notification) {
console.log('onNotification', JSON.stringify(notification));
if (ionic.Platform.isAndroid()) {
handleAndroidPush(notification);
} else {
handleIosPush(notification);
}
return true;
}
});
}
});
},
};
});
Our build info is as follows:
Cordova CLI: 5.1.1
Ionic Version: 1.0.0
Ionic CLI Version: 1.6.4
Ionic App Lib Version: 0.3.8
Plugins and Libs:
“ionic-service-core”,“version”: “0.1.10”,
“ionic-service-push”, “version”: “0.1.8”,
com.phonegap.plugins.PushPlugin 2.4.0 “PushPlugin”
If anybody is able to offer some insight, we would really appreciate the help.