Van Coding
Van Coding

Reputation: 24554

Titanium - prevent exitOnClose from stopping the app

My app uses the gcm module to listen for notifications and displays android notifications for every new notification that comes in. Additionally, the current window gets updated with the new count of unread messages.

This window is created using Ti.UI.createWindow({exitOnClose:true})

The problem is, that when the user presses the back button, the application stops. This means, I don't receive any more notifications and thus cannot display them in the notifications bar.

Is there a way to make titanium hide the app when pressing the back button, but not stop it, so that my code stil is running in the background?

I know of the possibility to start a service, but the downside of this is that i cannot update my window, when it's currently visible to the user, since there seems to be no way to communicate between the service and the app. Or is there a way?

app.js

//this is the most important line in this code.
//if I do exitOnClose:true, I stop receiving notifications every 5 seconds when pressing the back button (not good!, I want to keep getting notifications)
//if I do exitOnClose:false, I go back to a blank, "powered by titanium" window, when pressing the back button (not good!, I want the app to go to the background)
var win = Ti.UI.createWindow({exitOnClose:true});


//not part of the question
var label = Ti.UI.createLabel({text:"0"});
win.add(label);
win.open();
var notifications = [];

//listen for notifications (not part of the question)
listenForNotifications(function(notification){

    //handle the notification
    notifications.push(notification);

    //update window
    label.text = "Notification Count: "+notifications.length;

    //display notification in title bar
    displayNotificationInTitleBar(notification);
})

//this function is just dummy code to simulate listening for notifications in background using the gcm module
//it simulates a new notification every 5 seconds with an always increasing id
//it actually does not matter what module I use for notifications, Just take it as given that there runs code in the background,
//that I don't want to stop, after the user taps the backbutton
function listenForNotifications(cb){
    var i = 0;
    setInterval(function(){
        cb({id:i++});
    },5000);
}

//This function is actually not part of the question, it's just a sample
function displayNotificationInTitleBar(notification){
    var intent = Ti.Android.createIntent({
        action: Ti.Android.ACTION_MAIN,
        packageName:"com.company.backgroundnotificationstest",
        className:"com.company.backgroundnotificationstest.BackgroundnotificationstestActivity",
        flags:Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED  | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP
    });
    intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);
    intent.putExtra("notificationid",notification.id);

    Titanium.Android.NotificationManager.notify(notification.id, Titanium.Android.createNotification({
        contentTitle: "New Notification",
        contentText : "ID: "+notification.id,
        contentIntent: Ti.Android.createPendingIntent({
            intent:intent,
            type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY
        }),
        flags : Titanium.Android.ACTION_DEFAULT | Titanium.Android.FLAG_AUTO_CANCEL | Titanium.Android.FLAG_SHOW_LIGHTS
    }));
}

The sample app is available at: https://github.com/VanCoding/TitaniumBackgroundNotificationsTest

Feel free to compile it and see it yourself :)

Upvotes: 0

Views: 753

Answers (2)

Van Coding
Van Coding

Reputation: 24554

After a bit of thinking, I came to the following (a bit hacky) solution:

win.addEventListener("android:back",function(){ //listen for the back-button-tap event

    e.cancelBubble = true;  //prevent the back-button default action


    //display the home screen
    var intent = Ti.Android.createIntent({
        action: Ti.Android.ACTION_MAIN,
        flags:Ti.Android.FLAG_ACTIVITY_NEW_TASK
    });
    intent.addCategory(Ti.Android.CATEGORY_HOME);
    Ti.Android.currentActivity.startActivity(intent);

});

Upvotes: 1

Anand
Anand

Reputation: 5332

Since you set exitOnClose your app will exit on closing the window you have created. To prevent exiting from the app you need to reset the key as follows while creating the window.

Ti.UI.createWindow({exitOnClose: false});

If you would like to show the notifications in the notifications tray, make sure that you have set the following keys

  1. showTrayNotification

  2. showTrayNotificationsWhenFocused : This will display the notification in the tray even when the app is focused.

To show/hide a badge, you can try the following tip.

You need to keep track how many notifications you received and you need to update it upon receiving the notification. Just update the badge using the value stored. I tried this solution and is working great in one of my app

Upvotes: 1

Related Questions