Handling notifications

I'm using CordovaPush plugin to handle notifications (Android only atm). I have a piece of code like this:

$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
  switch(notification.event) {

    case 'registered':
      // register
      break;

    case 'message':
      // handle message
      break;

The case 'message' seems to be called in two situations:

  • The app is closed (or in background) and the user taps on the received notification
  • The notification is received while the user has the app open

Is there a way for me to tell the difference between these two?

What I'm trying to implement is for tapping on the notification to open a specific $state (page if you will), and if the user is looking at the app I want to show a toast.

Any ideas?

Check the foreground property:

    case 'message':
        if ( event.foreground ) {
        }
        else {
        }