Skip to content

Commit

Permalink
app icon badge
Browse files Browse the repository at this point in the history
fixed repeating notifications
  • Loading branch information
RealBonus committed Mar 9, 2018
1 parent dfc6e93 commit dd8e4b8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 13 deletions.
34 changes: 32 additions & 2 deletions Adamant/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var repeater: RepeaterService!

weak var accountService: AccountService?
weak var notificationService: NotificationsService?

// MARK: - Lifecycle

Expand All @@ -28,6 +31,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
container.registerAdamantChatsStory()
container.registerAdamantSettingsStory()

accountService = container.resolve(AccountService.self)
notificationService = container.resolve(NotificationsService.self)


// MARK: 2. Prepare UI
self.window = UIWindow(frame: UIScreen.main.bounds)
Expand Down Expand Up @@ -109,6 +115,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func applicationDidBecomeActive(_ application: UIApplication) {
repeater.resumeAll()

if accountService?.account != nil {
notificationService?.removeAllDeliveredNotifications()
}
}

// MARK: Background fetch
Expand All @@ -131,18 +141,38 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return
}

let lastHeight: Int64?
var lastHeight: Int64?
if let raw = securedStore.get(StoreKey.chatProvider.receivedLastHeight) {
lastHeight = Int64(raw)
} else {
lastHeight = nil
}

var notifiedCount = 0
if let raw = securedStore.get(StoreKey.chatProvider.notifiedLastHeight), let notifiedHeight = Int64(raw), let h = lastHeight {
if h < notifiedHeight {
lastHeight = notifiedHeight

if let raw = securedStore.get(StoreKey.chatProvider.notifiedMessagesCount), let count = Int(raw) {
notifiedCount = count
}
}
}


apiService.getChatTransactions(address: address, height: lastHeight, offset: nil) { result in
switch result {
case .success(let transactions):
if transactions.count > 0 {
notificationsService.showNotification(title: String.adamantLocalized.notifications.newMessageTitle, body: String.localizedStringWithFormat(String.adamantLocalized.notifications.newMessageBody, transactions.count), type: .newMessages)
let total = transactions.count + notifiedCount
securedStore.set(String(total), for: StoreKey.chatProvider.notifiedMessagesCount)

if let newLastHeight = transactions.map({$0.height}).sorted().last {
securedStore.set(String(newLastHeight), for: StoreKey.chatProvider.notifiedLastHeight)
}


notificationsService.showNotification(title: String.adamantLocalized.notifications.newMessageTitle, body: String.localizedStringWithFormat(String.adamantLocalized.notifications.newMessageBody, total), type: .newMessages(count: total))
completionHandler(.newData)
} else {
completionHandler(.noData)
Expand Down
2 changes: 2 additions & 0 deletions Adamant/ServiceProtocols/DataProviders/ChatsProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ extension StoreKey {
static let address = "chatProvider.address"
static let receivedLastHeight = "chatProvider.receivedLastHeight"
static let readedLastHeight = "chatProvider.readedLastHeight"
static let notifiedLastHeight = "chatProvider.notifiedLastHeight"
static let notifiedMessagesCount = "chatProvider.notifiedCount"
}
}

Expand Down
29 changes: 22 additions & 7 deletions Adamant/ServiceProtocols/NotificationsService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ extension StoreKey {
/// - transaction: token transaction
/// - custom: other
enum AdamantNotificationType {
case newMessages
case newTransactions
case custom(identifier: String)
case newMessages(count: Int)
case newTransactions(count: Int)
case custom(identifier: String, badge: NSNumber?)

var identifier: String {
switch self {
Expand All @@ -45,10 +45,23 @@ enum AdamantNotificationType {
case .newTransactions:
return "newTransactions"

case .custom(let identifier):
case .custom(let identifier, _):
return identifier
}
}

var badge: NSNumber? {
switch self {
case .newMessages(let count):
return NSNumber(integerLiteral: count)

case .newTransactions(let count):
return NSNumber(integerLiteral: count)

case .custom(_, let badge):
return badge
}
}
}

// MARK: - Notifications
Expand All @@ -64,13 +77,15 @@ enum NotificationsServiceResult {
case denied(error: Error?)
}

protocol NotificationsService {
protocol NotificationsService: class {
var notificationsEnabled: Bool { get }

func setNotificationsEnabled(_ enabled: Bool, completion: @escaping (NotificationsServiceResult) -> Void)

func showNotification(title: String, body: String, type: AdamantNotificationType)

func removeAllPendingNotificationRequests(ofType type: AdamantNotificationType)
func removeAllDeliveredNotifications(ofType type: AdamantNotificationType)
func removeAllPendingNotificationRequests()
func removeAllDeliveredNotifications()
// func removeAllPendingNotificationRequests(ofType type: AdamantNotificationType)
// func removeAllDeliveredNotifications(ofType type: AdamantNotificationType)
}
16 changes: 12 additions & 4 deletions Adamant/Services/AdamantNotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import UIKit
import UserNotifications

class AdamantNotificationsService: NotificationsService {
Expand Down Expand Up @@ -43,6 +44,11 @@ class AdamantNotificationsService: NotificationsService {

// MARK: Lifecycle
init() {
NotificationCenter.default.addObserver(forName: Notification.Name.adamantUserLoggedIn, object: nil, queue: OperationQueue.main) { _ in
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
UIApplication.shared.applicationIconBadgeNumber = 0
}

NotificationCenter.default.addObserver(forName: Notification.Name.adamantUserLoggedOut, object: nil, queue: nil) { [weak self] _ in
self?.notificationsEnabled = false
self?.securedStore.remove(StoreKey.notificationsService.notificationsEnabled)
Expand Down Expand Up @@ -99,6 +105,7 @@ class AdamantNotificationsService: NotificationsService {
content.title = title
content.body = body
content.sound = UNNotificationSound.default()
content.badge = type.badge

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: type.identifier, content: content, trigger: trigger)
Expand All @@ -110,11 +117,12 @@ class AdamantNotificationsService: NotificationsService {
}
}

func removeAllPendingNotificationRequests(ofType type: AdamantNotificationType) {
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [type.identifier])
func removeAllPendingNotificationRequests() {
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
}

func removeAllDeliveredNotifications(ofType type: AdamantNotificationType) {
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [type.identifier])
func removeAllDeliveredNotifications() {
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
UIApplication.shared.applicationIconBadgeNumber = 0
}
}

0 comments on commit dd8e4b8

Please sign in to comment.