Skip to content

Commit 6e87350

Browse files
committed
Prevent display of duplicate messages based on MessageID.
1 parent 85dab78 commit 6e87350

File tree

5 files changed

+38
-23
lines changed

5 files changed

+38
-23
lines changed

Swift Push 3.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@
317317
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
318318
GCC_WARN_UNUSED_FUNCTION = YES;
319319
GCC_WARN_UNUSED_VARIABLE = YES;
320-
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
320+
IPHONEOS_DEPLOYMENT_TARGET = 8.1;
321321
MTL_ENABLE_DEBUG_INFO = YES;
322322
ONLY_ACTIVE_ARCH = YES;
323323
SDKROOT = iphoneos;
@@ -355,7 +355,7 @@
355355
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
356356
GCC_WARN_UNUSED_FUNCTION = YES;
357357
GCC_WARN_UNUSED_VARIABLE = YES;
358-
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
358+
IPHONEOS_DEPLOYMENT_TARGET = 8.1;
359359
MTL_ENABLE_DEBUG_INFO = NO;
360360
SDKROOT = iphoneos;
361361
TARGETED_DEVICE_FAMILY = "1,2";

Swift Push 3/AppDelegate.swift

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Swift Push 3/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>
22-
<string>167</string>
22+
<string>185</string>
2323
<key>LSRequiresIPhoneOS</key>
2424
<true/>
2525
<key>UIBackgroundModes</key>

Swift Push 3/Settings.bundle/Root.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<array>
77
<dict>
88
<key>DefaultValue</key>
9-
<string>167</string>
9+
<string>185</string>
1010
<key>Key</key>
1111
<string>CurrentBuildNumber</string>
1212
<key>Title</key>

0 commit comments

Comments
 (0)