titanium android custom redirection on notification

255 views Asked by At

I am developing an android application that includes gcm push notifications via ti.cloudpush module. I would like to launch specific windows for certain type of push notifications. Is theere any proper way to acieve this. Following are my workouts.

CloudPush.addEventListener('callback', function(evt) {
alert("Notification received: " + evt.payload);
//-------------------launch code
var win=Ti.UI.createWindow({
    url:'music.js',
    exitOnClose:true,
});

});

i also tried by creating pending intent.It was also not failure. thanks in advance

1

There are 1 answers

3
Michael On

In the callback you'll have a payload sent back.

alert(evt.payload);

CloudPush.addEventListener('callback', function(evt) {
    Ti.API.debug(evt.payload);

}

The payload is a JSON string of your data payload. Use JSON.parse to turn this in to an object you can use.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

You can use this payload to base the check on what intent, window or action you would like to fire.

var payload = JSON.parse(evt.payload);

if (payload... ) { 
    // Do first option

} else {
    // Do fallback option
}

Inside your callback eventEventLister from cloudPush once you have parsed the payload you could load something similar to this:

CloudPush.addEventListener('callback', function(evt) {

    ... Do payload check if statement from evt.payload...

    // Or this could be a new window, alert, etc,
    Titanium.Android.NotificationManager.notify(0, 
        Ti.Android.createNotification({
            contentTitle : "title",
            contentText : "text",
            tickerText : "custom notification!",
            contentIntent : Titanium.Android.createPendingIntent({
                intent : Titanium.Android.createIntent({
                    url : 'foo.js'
                })
            })
        })
    );
});

If you want to switch out a different window, set the url object in the intent to a custom variable that is set depending on what the evt.payload is that's sent back.

eg

intent : Titanium.Android.createIntent({
    url : presetWindowUrl
})