我的应用程序使用gcm模块监听通知并显示每个新通知的Android通知。此外,当前窗口将使用新的未读消息计数进行更新。
此窗口使用Ti.UI.createWindow({exitOnClose:true})
问题是,当用户按下后退按钮时,应用程序停止。这意味着,我不再收到任何通知,因此无法在通知栏中显示它们。
有没有办法让钛在按下后退按钮时隐藏应用程序,但不能阻止它,以便我的代码stil在后台运行?
我知道启动服务的可能性,但其缺点是我无法更新我的窗口,当用户当前可以看到它时,因为似乎无法在服务和应用程序之间进行通信。或者有办法吗?
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
随意编译并自己查看:)
答案 0 :(得分:1)
由于您设置exitOnClose
,您的应用将在关闭您创建的窗口时退出。要防止退出应用程序,您需要在创建窗口时按如下方式重置密钥。
Ti.UI.createWindow({exitOnClose: false});
如果您想在通知栏中显示通知,请确保已设置以下键
showTrayNotificationsWhenFocused:即使关注应用,这也会在托盘中显示通知。
要显示/隐藏徽章,您可以尝试以下提示。
您需要跟踪收到的通知数量,并在收到通知后进行更新。只需使用存储的值更新徽章即可。我尝试了这个解决方案,并且在我的应用程序中运行良好
答案 1 :(得分:0)
经过一番思考,我得出了以下(有点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);
});