Skip to content

Commit

Permalink
remove irrelevant notification on app-start or background-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
r10s committed Mar 20, 2024
1 parent 1449af8 commit 5a0da83
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DcNotificationService/NotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ class NotificationService: UNNotificationServiceExtension {
private func silenceNotification(_ bestAttemptContent: UNMutableNotificationContent) -> UNMutableNotificationContent {
// with `com.apple.developer.usernotifications.filtering` entitlement,
// one can use `contentHandler(UNMutableNotificationContent())` to not display a notifcation
// and remove all "irrelevant" handling
bestAttemptContent.sound = nil
bestAttemptContent.body = "No further relevant"
bestAttemptContent.userInfo["irrelevant"] = true
if #available(iOS 15.0, *) {
bestAttemptContent.interruptionLevel = .passive
bestAttemptContent.relevanceScore = 0.0
Expand Down
6 changes: 6 additions & 0 deletions deltachat-ios/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
increaseDebugCounter("notify-remote-launch")
pushToDebugArray("📡'")
performFetch()
} else {
NotificationManager.removeIrrelevantNotifications()
}

if dcAccounts.getSelected().isConfigured() && !UserDefaults.standard.bool(forKey: "notifications_disabled") {
Expand Down Expand Up @@ -254,6 +256,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
}

AppDelegate.emitMsgsChangedIfShareExtensionWasUsed()

NotificationManager.removeIrrelevantNotifications()
}
}

Expand Down Expand Up @@ -437,6 +441,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
}

private func performFetch(completionHandler: ((UIBackgroundFetchResult) -> Void)? = nil) {
NotificationManager.removeIrrelevantNotifications()

// `didReceiveRemoteNotification` as well as `performFetchWithCompletionHandler` might be called if we're in foreground,
// in this case, there is no need to wait for things or do sth.
if appIsInForeground() || UserDefaults.nseFetching {
Expand Down
14 changes: 14 additions & 0 deletions deltachat-ios/Helper/NotificationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ public class NotificationManager {
}
}

public static func removeIrrelevantNotifications() {
let nc = UNUserNotificationCenter.current()
nc.getDeliveredNotifications { notifications in
var toRemove = [String]()
for notification in notifications {
let irrelevant = notification.request.content.userInfo["irrelevant"] as? Bool ?? false
if irrelevant {
toRemove.append(notification.request.identifier)
}
}
nc.removeDeliveredNotifications(withIdentifiers: toRemove)
}
}

private func initObservers() {
anyIncomingMsgObserver = NotificationCenter.default.addObserver(
forName: eventIncomingMsgAnyAccount,
Expand Down

0 comments on commit 5a0da83

Please sign in to comment.