Skip to content

Commit

Permalink
Improved grouping of notifications
Browse files Browse the repository at this point in the history
This is to avoid notification spamming on connection errors when we try to reconnect repeatedly.
  • Loading branch information
N-Pex committed Jun 28, 2018
1 parent 183f01d commit 9b6f56b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
31 changes: 9 additions & 22 deletions ChatSecure/Classes/Categories/UIApplication+ChatSecure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public extension UIApplication {
let chatString = WANTS_TO_CHAT_STRING()
let text = "\(name) \(chatString)"
let unreadCount = self.applicationIconBadgeNumber + 1
self.showLocalNotificationWith(identifier: nil, body: text, badge: unreadCount, userInfo: [kOTRNotificationType:kOTRNotificationTypeSubscriptionRequest], recurring: false)
self.showLocalNotificationWith(groupingIdentifier: nil, body: text, badge: unreadCount, userInfo: [kOTRNotificationType:kOTRNotificationTypeSubscriptionRequest], recurring: false)
}
}

Expand All @@ -158,7 +158,7 @@ public extension UIApplication {
let userInfo:[AnyHashable:Any] = [kOTRNotificationThreadKey:identifier,
kOTRNotificationThreadCollection:thread.threadCollection,
kOTRNotificationType: kOTRNotificationTypeApprovedBuddy]
self.showLocalNotificationWith(identifier: identifier, body: message, badge: unreadCount, userInfo: userInfo, recurring: false)
self.showLocalNotificationWith(groupingIdentifier: nil, body: message, badge: unreadCount, userInfo: userInfo, recurring: false)
}
}

Expand All @@ -173,13 +173,13 @@ public extension UIApplication {
kOTRNotificationThreadCollection:t.threadCollection,
kOTRNotificationType: kOTRNotificationTypeChatMessage]
}
self.showLocalNotificationWith(identifier: identifier, body: text, badge: unreadCount, userInfo: userInfo, recurring: false)
self.showLocalNotificationWith(groupingIdentifier: nil, body: text, badge: unreadCount, userInfo: userInfo, recurring: false)
}
}

@objc public func showLocalNotificationWith(identifier:String?, body:String, badge:Int, userInfo:[AnyHashable:Any]?, recurring:Bool) {
@objc public func showLocalNotificationWith(groupingIdentifier:String?, body:String, badge:Int, userInfo:[AnyHashable:Any]?, recurring:Bool) {
DispatchQueue.main.async {
if recurring, self.hasRecurringLocalNotificationWith(identifier: identifier) {
if recurring, self.hasRecurringLocalNotificationWith(identifier: groupingIdentifier) {
return // Already pending
}
// Use the new UserNotifications.framework on iOS 10+
Expand All @@ -188,8 +188,8 @@ public extension UIApplication {
localNotification.body = body
localNotification.badge = NSNumber(integerLiteral: badge)
localNotification.sound = UNNotificationSound.default()
if let identifier = identifier {
localNotification.threadIdentifier = identifier
if let threadKey = userInfo?[kOTRNotificationThreadKey] as? String {
localNotification.threadIdentifier = threadKey
}
if let userInfo = userInfo {
localNotification.userInfo = userInfo
Expand All @@ -201,7 +201,7 @@ public extension UIApplication {
date.minute = 0
trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
}
let request = UNNotificationRequest(identifier: UUID().uuidString, content: localNotification, trigger: trigger) // Schedule the notification.
let request = UNNotificationRequest(identifier: groupingIdentifier ?? UUID().uuidString, content: localNotification, trigger: trigger) // Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: { (error: Error?) in
if let error = error as NSError? {
Expand Down Expand Up @@ -325,19 +325,6 @@ public extension UIApplication {
let userInfo = [kOTRNotificationType: kOTRNotificationTypeConnectionError,
kOTRNotificationAccountKey: accountKey]

if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().getDeliveredNotifications(completionHandler: { (notifications) in
// FIXME: this deduplication code doesn't seem to work
// if we are already showing a notification, let's not spam the user too much with more of them
for notification in notifications {
if notification.request.identifier == accountKey {
return
}
}
self.showLocalNotificationWith(identifier: accountKey, body: body, badge: badge, userInfo: userInfo, recurring: false)
})
} else {
showLocalNotificationWith(identifier: accountKey, body: body, badge: badge, userInfo: userInfo, recurring: false)
}
self.showLocalNotificationWith(groupingIdentifier: accountKey, body: body, badge: badge, userInfo: userInfo, recurring: false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ - (void) continueOnboarding:(BOOL)hasAccounts {
notificationBody = [NSString stringWithFormat:MIGRATION_NOTIFICATION_WITH_DATE_STRING(), days];
}

[[UIApplication sharedApplication] showLocalNotificationWithIdentifier:@"Migration" body:notificationBody badge:1 userInfo:[[NSDictionary alloc] initWithObjectsAndKeys:kOTRNotificationTypeNone, kOTRNotificationType, @"Migration", kOTRNotificationThreadKey, nil] recurring:YES];
[[UIApplication sharedApplication] showLocalNotificationWithGroupingIdentifier:@"Migration" body:notificationBody badge:1 userInfo:[[NSDictionary alloc] initWithObjectsAndKeys:kOTRNotificationTypeNone, kOTRNotificationType, @"Migration", kOTRNotificationThreadKey, nil] recurring:YES];
}
} else {
[[UIApplication sharedApplication] cancelRecurringLocalNotificationWithIdentifier:@"Migration"];
Expand Down

0 comments on commit 9b6f56b

Please sign in to comment.