johnnyfittizio johnnyfittizio - 2 months ago 34
Android Question

Ionic Push notification: How to handle onClick?

THE SITUATION:

I am using Ionic Push notifications in my app.

Everything is working fine.

When the user send a message, the notification will be fired through a cURL request, and it will arrive only if the receiver has the app off or in background.

I have just a problem handling the onClick callback.

Following the documentation I have put the callback function in the onNotification parameter.


"onNotification": This function will be called when your device
receives a notification, and provided with the notification object
received.


What I need to do, when the user click on the notification, is to redirect him to a certain page.

It is working.

The problem is that when the app is in FOREGROUND, the user will be redirected to that page everytime a cURL request is made... regardless of the fact that the notification has been received (app off or background) or not (app foreground)...

THE CODE:

.run(function($ionicPlatform, $rootScope, $location, $state) {

$ionicPlatform.ready(function() {

var push = new Ionic.Push({

"debug": true,
"onNotification": function(notification)
{
if ( $state.current.name != 'app.attendee-chat' )
{
$rootScope.get_my_account_data();
$location.path('app/attendee_list');
}
},
"onRegister": function(data)
{
console.log(data.token);
},

});


THE QUESTION:

How can I manage to setup a callback function that will be called ONLY when the user actually click on the notification, and not when he is inside the app?

I have to do some tricks inside the onNotification or there is a possibility to setup a onClick callback?

Thank you!

Answer

THE SOLUTION:

Ok I managed it using the AppStatus. By checking it, I will apply the redirecting only if the status of the app is asleep or closed.

if (notification.app.asleep || notification.app.closed)

THE CODE:

var push = new Ionic.Push({

    "debug": true,
    "onNotification": function(notification) 
    {
        $rootScope.get_my_account_data();

        if (notification.app.asleep || notification.app.closed) 
        {
            // the app was asleep or was closed, so do the redirect 
            $location.path('app/attendee_list');
        }

        },
        "onRegister": function(data) 
        {
            console.log(data.token);
        },

    });

THE DOCUMENTATION:

If you want to know more about the properties of push message here is the documentation:

http://docs.ionic.io/api/client/pushmessage/

Comments