@@ -82,41 +82,56 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
8282
8383
8484 func application( application: UIApplication ! , didReceiveRemoteNotification userInfo: NSDictionary ! , fetchCompletionHandler completionHandler: ( ( UIBackgroundFetchResult ) -> Void ) ! ) {
85- println ( " Push message received by AppDeligate: \( userInfo) " )
8685
86+ println ( )
87+ switch ( application. applicationState) {
88+ case UIApplicationState . Active:
89+ println ( " notification received by AppDeligate whilst active " )
90+ case UIApplicationState . Inactive:
91+ println ( " notification received by AppDeligate whilst inactive " )
92+ case UIApplicationState . Background:
93+ println ( " notification received by AppDeligate whilst in background " )
94+ default :
95+ println ( " notification received by AppDeligate with unknown application state " )
96+ }
97+ println ( userInfo)
98+
99+ // decode mesage and create new object
87100 var t1 : AnyObject ! = userInfo. objectForKey ( " aps " )
88101 var alert = t1. objectForKey ( " alert " ) as String
89102 var payload = userInfo. objectForKey ( " payload " ) as String
90103 // var timeStamp = userInfo.objectForKey("timestamp") as String
91104 var messageID = userInfo. objectForKey ( " messageID " ) as Int
92105
93- // println(timeStamp)
94-
95106 var item = NotificationData ( )
96107 item. alert = alert
97108 item. payload = payload
98109 // item.timeStamp = NSDate(timeIntervalSince1970: timeStamp)
99110 item. messageID = messageID
111+
100112
101- notifications. insert ( item, atIndex: 0 )
102- if ( notifications. count > maxNotifications) {
103- notifications. removeLast ( )
113+ // Is this a new message? Check for exisitance of messageID in array
114+ var newItem = true ;
115+ for n in notifications {
116+ if ( n. messageID == item. messageID) {
117+ newItem = false
118+ }
104119 }
105120
106- // notify tableview to refresh
107- let center = NSNotificationCenter . defaultCenter ( )
108- center. postNotificationName ( " dataChanged " , object: self )
109-
110- switch ( application. applicationState) {
111- case UIApplicationState . Active:
112- println ( " notification received whilst active " )
113- case UIApplicationState . Inactive:
114- println ( " notification received whilst inactive " )
115- case UIApplicationState . Background:
116- println ( " notification received whilst in background " )
117- default :
118- println ( " notification received with unknown application state " )
121+ if ( newItem == true ) {
122+ println ( " adding notification " )
123+ notifications. insert ( item, atIndex: 0 )
124+ if ( notifications. count > maxNotifications) {
125+ notifications. removeLast ( )
126+ }
127+ // notify tableview to refresh
128+ let center = NSNotificationCenter . defaultCenter ( )
129+ center. postNotificationName ( " dataChanged " , object: self )
130+ } else {
131+ println ( " duplicate notification - ignoring " )
119132 }
133+
134+ // finished
120135 completionHandler ( UIBackgroundFetchResult . NewData)
121136
122137 }
0 commit comments