【问题标题】:Titanium - prevent exitOnClose from stopping the appTitanium - 防止 exitOnClose 停止应用程序
【发布时间】:2014-08-12 15:04:48
【问题描述】:

我的应用使用 gcm 模块来监听通知,并为每个新的通知显示 android 通知。此外,当前窗口会更新为未读消息的新计数。

这个窗口是使用Ti.UI.createWindow({exitOnClose:true})创建的

问题是,当用户按下后退按钮时,应用程序会停止。这意味着,我不再收到任何通知,因此无法在通知栏中显示它们。

有没有办法让钛在按下后退按钮时隐藏应用程序,但不停止它,让我的代码仍然在后台运行?

我知道启动服务的可能性,但这样做的缺点是我无法更新我的窗口,当它当前对用户可见时,因为似乎没有办法在服务和应用程序之间进行通信。或者有什么办法?

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
    }));
}

示例应用程序位于:https://github.com/VanCoding/TitaniumBackgroundNotificationsTest

随意编译并自己查看:)

【问题讨论】:

  • @Downvoter:为什么?关于如何改进问题的任何建议?如果没有,请再次投票,因为这是一个合法的问题,appcelerator 文档现在还没有回答。谢谢。

标签: javascript titanium


【解决方案1】:

由于您设置了exitOnClose,您的应用将在关闭您创建的窗口时退出。为防止退出应用程序,您需要在创建窗口时按如下方式重置密钥。

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

如果您想在通知托盘中显示通知,请确保您已设置以下键

  1. showTrayNotification

  2. showTrayNotificationsWhenFocused :即使应用获得焦点,也会在托盘中显示通知。

要显示/隐藏徽章,您可以尝试以下提示。

您需要跟踪收到的通知数量,并在收到通知后进行更新。只需使用存储的值更新徽章。我尝试了这个解决方案,并且在我的一个应用中运行良好

【讨论】:

  • 感谢您的回答。可悲的是,我没有使用钛 cloudpush 模块。我使用了一个单独的 gcm 模块,可以免费使用。此外,将 exitOnClose 设置为 true 也无济于事,因为这样可能会返回不显示任何窗口(只是一个白屏)。从那里我仍然可以按返回按钮,然后停止应用程序并导致在我再次启动应用程序之前不会收到任何通知。有什么想法吗?
  • @VanCoding:如果您在问题中添加更多信息会更好。我假设您使用的是 CloudPush 模块,请让我们知道您正在使用哪个模块,如果您添加更多代码,它将很容易帮助您。更新问题,然后我也许可以帮助你,我也会取消投票:)
  • 好的,我会为你整理一个示例应用程序:)
  • 完成!看看吧:)
【解决方案2】:

经过一番思考,我得出了以下(有点hacky)的解决方案:

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);

});

【讨论】:

    猜你喜欢
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    相关资源
    最近更新 更多