title: Preventing every push notification being executed iOS titanium
body: <p>If I get multiple push notifications whilst app is in foreground. The callback method will execute every push notification one by one.</p>
  
  <pre><code>        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
  
                      }
  
                  }
  </code></pre>
  
  <p>If the push notification has come from the background it is easy to deal with, as the push notification that is activated is the one the user chooses to swipe. However, if multiple push notifications come into the foreground and say it's chat, it will execute them multiple times.</p>
  
  <p>How can I handle push notifications in the foreground better? Thanks</p>
  
  <p>Update:</p>
  
  <p>tried this code without much luck</p>
  
  <pre><code>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 &lt; 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;
      }
  
  
  });
  </code></pre>
  
  <p>Trying to get all the push notifications inside the array, for further filtering.</p>
  
  <p>Update 2:</p>
  
  <pre><code>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
                              });
  
                          }
  </code></pre>
  
  <p>I have tried to execute push notifications alternatively.</p>
  
  <p>1st notification - fires instantly.
  2nd notification - fires after 6 seconds.
  3rd notification - instantly.
  4th notification - fires after 6 seconds.</p>
  
  <p>and so on...</p>
  
  <p>however the code only works for </p>
  
  <p>notification 1 and 2. </p>
  
  <p>Fails when it hits the 3rd notification.</p>