【发布时间】:2014-06-24 23:08:58
【问题描述】:
如果我在应用程序处于前台时收到多个推送通知。回调方法会一一执行每个推送通知。
callback : function(e) {
if (e.inBackground == 1) {
//came from background - do something.
} else {
// Titanium.UI.iPhone.setAppBadge(null);
//check type, if it is chat.
if (type == 'chat') {
//check if window is already opened or not, if so fire event handler
if (currentWindow == '_chatWindow') {
//update view directly after entering app from the background. Fire event handler
Ti.App.fireEvent('_updateChat', {});
} else if (currentWindow == '_messages') {
//refresh messages screen if on messages screen and chat message arrives
//update view directly after entering app from the background. Fire event handler
Ti.App.fireEvent('_updateMessages', {});
} else {
//display local notification
}
}
如果推送通知来自后台,则很容易处理,因为激活的推送通知是用户选择滑动的通知。但是,如果多个推送通知进入前台并说它是聊天,它将执行多次。
如何更好地处理前台的推送通知?谢谢
更新:
尝试了这段代码,运气不佳
Ti.App.addEventListener('_displayNotification', function(e) {
//store all push notifications in array
var pushArray = [];
var countPushNotifications;
//currentTime to cross reference
var currentTime = new Date();
if (currentTime - Alloy.Globals.pushTime < 3000) {
//do something
pushArray.add(e.PushNotificationData);
} else {
//after 3 seconds remove event handler
//fire event to filter array and process notification, reset time for next event
Alloy.Globals.pushTime = null;
Ti.App.removeEventListener('_displayNotification', {});
}
//first push notification, will be the current time
if(Alloy.Globals.pushTime==null){
Alloy.Globals.pushTime = currentTime;
}
});
尝试获取数组内的所有推送通知,以便进一步过滤。
更新 2:
if (Alloy.Globals.countPushNotificationsFlag == 1) {
Alloy.Globals.countPushNotificationsFlag = null;
setTimeout(function() {
Ti.App.fireEvent('_displayNotification', {
PushMessage : message
});
}, 6000);
} else {
Alloy.Globals.countPushNotificationsFlag = 1;
Ti.App.fireEvent('_displayNotification', {
PushMessage : message
});
}
我已经尝试过执行推送通知。
第一个通知 - 立即触发。 第二个通知 - 6 秒后触发。 第三次通知 - 立即。 第 4 次通知 - 6 秒后触发。
等等……
但是代码只适用于
通知 1 和 2。
当它到达第三个通知时失败。
【问题讨论】:
-
不清楚你在问什么。 “更好”是什么意思?期望的行为是什么?您当前的方法不符合这一点?
-
如果您一次收到多个推送通知,比如说 5 条消息,您会在前台收到它们。在收到它们时,假设您在上面代码中描述的聊天窗口中。默认情况下,每个推送通知将一个接一个地执行,创建一个重复的操作。在那种情况下,如果我只执行我想要的推送通知会更聪明。在那种情况下,最后一个。
-
但这不是它的工作方式——您的事件处理程序将按顺序接收事件。您需要确定是否需要该操作 - 可能存储前一个操作的时间戳并确定是否需要处理此事件。你没有说为什么有多个事件触发器是个问题
-
例如,如果我在聊天窗口中,与某人聊天。这将意味着重复的 api 调用来更新聊天窗口。当使用与聊天相关的最后一个推送通知更新它更有意义时。
-
好的,所以您担心云平台上的额外api调用?如果是这样,那么我能看到的唯一解决方案是保留时间戳并丢弃在前一个特定时间窗口内到达的额外通知
标签: ios iphone notifications titanium titanium-alloy