Hi,
I am making an app where I want to use Ionic Push to send push notifications to all devices. The problem is, device tokens do not register, so I can not send push notifications. Here is my code.
app.js
var iosConfig = {
'badge': true,
'sound': true,
'alert': true,
};
$ionicAppProvider.identify({
app_id: '9a817ab5',
api_key: 'ef581894f393b5ca0109017cd15355fb3ba03ea51aa53784',
name: 'Lovett',
// Set the app to use development pushes
dev_push: false
});
$cordovaPush.register(iosConfig).then(function(result) {
// Success -- send deviceToken to server, and store for future use
console.log('result: ' + result)
$rootScope.deviceToken = result;
//$http.post('http://server.co/', {user: 'Bob', tokenID: result.deviceToken})
}, function(err) {
alert('Registration error: ' + err)
});
$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
if (notification.alert) {
navigator.notification.alert(notification.alert);
}
if (notification.sound) {
var snd = new Media(event.sound);
snd.play();
}
if (notification.badge) {
$cordovaPush.setBadgeNumber(notification.badge).then(function(result) {
// Success!
}, function(err) {
// An error occurred. Show a message to the user
});
}
});
controllers.js
$scope.pushRegister = function() {
console.log('Ionic Push: Registering user');
// Register with the Ionic Push service. All parameters are optional.
$ionicPush.register({
canShowconsole.log: true, //Can pushes show an console.log 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;
}
}).then(function(result) {
submitRegister();
console.log('result: ' + result)
});
};
var submitRegister = function() {
console.log('Ionic Push: User registered');
$scope.nextSlide();
}
$rootScope.$on('$cordovaPush:tokenReceived', function(event, data) {
console.log("Successfully registered token " + data.token);
console.log('Ionic Push: Got token ', data.token, data.platform);
$scope.token = data.token;
});
@Remy1 Yes I did try that a while back to no avail. This time I tried using the Push starter app and following the directions outlined here.
I am now able to register device tokens, identify and register users for iOS but now sending pushes doesnât work although their statuses indicate they have been sent successfully.
For instance Iâve sent a push to a device (my own) and hereâs the message status:
I thought I should update everyone on the status of my push issues. I have decided not to use Ionic Push, but the Parse push service. It is based off of a REST API that is way easier than Ionic Push IMO.
I am having the same issues with Ionic Push, so I went down the Parse avenue as well. Or at least tried to.
Did you get any errors when you first tried Parse? My app crashes when running in Xcode and I get this message in my output:
*** Terminating app due to uncaught exception âNSInternalInconsistencyExceptionâ, reason: âCould not load NIB in bundle: âNSBundle </private/var/mobile/Containers/Bundle/Application/0E835245-160B-4FAD-9D86-90DFAE9E1B9C/UBA Expo.app> (loaded)â with name âViewControllerââ
Does anyone know what this means? I have Googled this for the past few days, but I am not getting a clear answer.
@mmilligan Fortunately I did not have any issues with trying Parse. It looks like your code is trying to execute something but failed because it hasnât been handled properly. You might not have initialized parse correctly.
I used the Javascript SDK from Parse for web. First include this in your index.html file: <script src="//www.parsecdn.com/js/parse-1.5.0.min.js"></script>
Then include this in your app.js: Parse.initialize("APP_ID", "JAVASCRIPT_ID");
Some other things you need to look out for are that the plugin WILL NOT work in the browser. You have to install it on a device. It will not register a browser and you WILL get the âTypeError: Cannot read property âpushNotificationâ of undefinedâ in the browser, but on a device it works fine. Note: If youâre using the ionic push you should be able to do testing in the browser. Lastly, make sure you include ng-cordova BEFORE cordova.
Ionic.io();
var push = new Ionic.Push();
var user = Ionic.User.current();
// if the user doesn't have an id, you'll need to give it one.
if (!user.id) {
user.id = Ionic.User.anonymousId();
}
user.set('name', $rootScope.user_name);
user.set('bio', $rootScope.user_bio);
user.save();
var callback = function(data) {
console.log('Registered token:', data.token);
console.log(data.token);
push.addTokenToUser(user);
user.save();
}
push.register(callback);