【问题标题】:Preventing every push notification being executed iOS titanium防止每个推送通知被执行 iOS 钛
【发布时间】: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


【解决方案1】:

您可以使用 push 的 inBackground 属性检查是在前台还是后台收到推送,这里是 documentation

希望对你有帮助。

【讨论】:

  • 这很好用,但问题是,如果您一次收到多个推送通知,比如 5 条消息,您会在前台收到它们。在收到它们时,假设您在上面代码中描述的聊天窗口中。默认情况下,每个推送通知将一个接一个地执行,创建一个重复的操作。在那种情况下,如果我只执行我想要的推送通知会更聪明。在那种情况下,最后一个。
【解决方案2】:

我没有 Titanium 经验来为您提供实际代码,但这是您需要采取的方法:

  1. 当您收到通知时,请检查(全局)布尔值 receivedNotification 是否为假
  2. 如果为 false,则将其设置为 true,将事件存储到全局中并安排 setTimeout 函数处理事件,例如 3 秒,然后将 receivedNotification 重置为 false
  3. 如果receivedNotification 为真,则将全局事件更新为较新的通知

在通过计时器触发的流程事件方法中,您将执行您当前在第一部分代码中执行的操作。

这将确保在收到事件后不超过 3 秒处理事件,并且最多每 3 秒处理一次事件。

您的代码看起来非常接近,但您尝试立即触发第一个事件,然后在延迟后触发后续事件。不幸的是,我不相信这是可能的,因为您无法查看是否有立即排队的事件。我认为您总是会产生延迟,但您可以调整延迟以在响应性和减少 API 调用之间找到平衡 -

if (Alloy.Globals.countPushNotificationsFlag == null) {

    Alloy.Globals.countPushNotificationsFlag = 1;

    Alloy.Globals.messageToPush=message;

    setTimeout(function() {
          Alloy.Globals.countPushNotificationsFlag = null;
          Ti.App.fireEvent('_displayNotification', {
            PushMessage : Alloy.Globals.messageToPush
          });
    }, 3000);

 }
 else {
     Alloy.Globals.messageToPush=message;
 }

【讨论】:

  • 我尝试了类似的路线,它成功了一半 - 请参阅原始答案。干杯。
猜你喜欢
  • 1970-01-01
  • 2015-07-12
  • 2016-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
相关资源
最近更新 更多