Then I registered for Google API GCM. I used curl to test send messages again. I turned off dev_push. The push notification is sent when the app is open or minimized.
However after I close the app, the push notification doesn’t work anymore. I thought this would be something to do with the identification / registration of the app. So I opened the app again and retested it without pressing identify/register and it still worked.
What am I doing something wrong? Does Ionic Push work when the app is close?
My second question is, for those who are experienced with ionic and push notification, I can’t seem to find much help on Ionic Push, probably since it’s in alpha. Is it easier to go with another service for example the one in Ng Cordova or Push Woosh? What are your experiences with that?
Thanks a lot in advance for your feedback / advice.
Here is my code:
angular.module('starter.controllers', [])
.controller('DashCtrl', function($scope, $rootScope, $ionicUser, $ionicPush) {
// Handles incoming device tokens
$rootScope.$on('$cordovaPush:tokenReceived', function(event, data) {
alert("Successfully registered token " + data.token);
console.log('Ionic Push: Got token ', data.token, data.platform);
$scope.token = data.token;
});
// Identifies a user with the Ionic User service
$scope.identifyUser = function() {
console.log('Ionic User: Identifying with Ionic User service');
var user = $ionicUser.get();
if(!user.user_id) {
// Set your user_id here, or generate a random one.
user.user_id = $ionicUser.generateGUID();
}
// Add some metadata to your user object.
angular.extend(user, {
name: 'Ionitron',
bio: 'I come from planet Ion'
});
// Identify your user with the Ionic User Service
$ionicUser.identify(user).then(function(){
$scope.identified = true;
alert('Identified user ' + user.name + '\n ID ' + user.user_id);
});
};
// Registers a device for push notifications and stores its token
$scope.pushRegister = function() {
console.log('Ionic Push: Registering user');
// 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;
}
});
};
})
Don’t use ionic push as it is in alpha. There are bound to be changes and problems. Use either push woosh or custom push notification implementation.
make sure that your push plugin is wrapped in device ready. NgCordova does not have this out of the box so $cordovaPush:tokenReceived will work if the plugins are ready and available otherwise it will not.
Don’t know if this is relevant to you but we were using Ionic Push to manage notifications. The thing is we weren’t able to receive notifications when app was closed and it was not an Ionic problem. The test phone is a MIUI and it has special permission to prevent apps from auto-starting. If you happen to have a custom Android so check if you have this kind of permissions activated.
Then again, this may completely irrelevant to your case.
I switched my server side to use directly the APNs & GCM (Instead of Ionic.io),
and in the client side, i use the cordova plugin directly, without the Ionic.push,
and finally everything works… Thank god…
I don’t know why the Ionic team let people use it when it’s not ready yet…