Skip to content

Commit

Permalink
Clear excess notifications in more places on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
nkalupahana committed Jul 3, 2024
1 parent c4d24b0 commit e607dac
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions ios/App/App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
self.handleAppRefresh(task: task as! BGAppRefreshTask)
}

self.clearExcessNotifications(resolve: {})
return true
}

Expand Down Expand Up @@ -94,7 +95,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

NotificationCenter.default.post(name: Notification.Name.init("didReceiveRemoteNotification"), object: completionHandler, userInfo: userInfo)

completionHandler(.newData)
clearExcessNotifications(resolve: {
completionHandler(.newData)
})

return
}

Expand All @@ -116,7 +120,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func handleAppRefresh(task: BGAppRefreshTask) {
// Schedule next refresh
scheduleAppRefresh()

clearExcessNotifications(resolve: {
task.setTaskCompleted(success: true)
})
}

func clearExcessNotifications(resolve: @escaping () -> Void) {
UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
// Find the latest notification (only one we want to keep)
var latestNotification: Optional<UNNotification> = nil;
Expand All @@ -139,7 +148,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

// Remove all remaining notifications, and complete
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: identifiers)
task.setTaskCompleted(success: true)
resolve()
}
}
}
Expand Down

0 comments on commit e607dac

Please sign in to comment.