gpt4 book ai didi

javascript - Titanium - 防止 exitOnClose 停止应用程序

转载 作者:行者123 更新时间:2023-12-03 12:04:01 26 4
gpt4 key购买 nike

我的应用程序使用 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

随意编译并亲自查看:)

最佳答案

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

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

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

  1. showTrayNotification

  2. showTrayNotificationsWhenFocused :即使应用程序处于焦点状态,这也会在托盘中显示通知。

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

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

关于javascript - Titanium - 防止 exitOnClose 停止应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25267803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com