Search code examples
javascriptandroidangularjscordovangcordova

ngCordova notificationReceived event not fired


I am developing application on ionic (angular + cordova), to get access to cordova plugins i use ngCordova (cordova plugins implementation for angular). I have a problem with a $cordovaPush plugin in android.

I successfully register a user by senderID in GCM:

$cordovaPush.register({
    senderID: MY_SENDER_ID
});

I get a regid from a $cordovaPush:notificationReceived event:

$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification){
    if(notification.event === 'registered'){
         if(notification.regid.length > 0) {
             ServerCommunicatorService.makeRequest('POST', 'auth/update-gcm-id', {gcm_id: notification.regid});
         }
     }
});

Then i have a such listener to show notifications and do some actions in controllers when in foreground:

$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification){
    if(notification.event === 'message'){
        $cordovaToast.show(notification.message, 'long', 'center');
        $rootScope.$broadcast('notificationReceived', notification);
    }
});

And finally the problem: After the successfully registration, when i send notifications from server i get them well (i'm foreground). If i background the app by hitting the Home button on my device, i receive a status bar notification. Selecting that notification from the status bringing my app to the front and i get the notification in event handler. Sinse now all is good. But! When i completely exit the app and send notif from server, i get it in status bar. I click it and my app is relaunching and after this i get ALL notifications in status bar and cant get them in listener.

In documentation said:

Finally, should you completely exit the app by hitting the back button from the home page, you may still receive a notification. Touching that notification in the notification tray will relaunch your app and allow you to process the notification (COLDSTART). In this case the coldstart flag will be set on the incoming event.

What could be the problem? Thanks in advance!!


Solution

  • Ok the solution was to register each time the application launches (even after touching notification in the notification tray).