简体   繁体   中英

Removing Remote Notifications From Notification Center

I have a requirement to aggregate remote notifications of the same type.

example : if a user received a push notification saying:"user 1 commented on your post", and then received "user 2 commented on your post", when receiving the second push I should remove the first notification and create a custom notification saying "2 users have commented on your post".


I'm getting the counter in the userInfo dictionary and I'm using NotificationService Extension in order to modify the notification's content.

the problem is I'm presenting 2 notifications:

  1. "user 1 commented on your post"
  2. "user 3 and 2 others have commented on your post"

instead of only the 2nd notification.

I've tried initializing UNNotificationRequest with custom identifier but still I'm getting double notifications (the original one and then the custom one).

    UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["push.comment"])
    if let username = bestAttemptContent.userInfo["sender"] as? String,
       let count = bestAttemptContent.userInfo["views_counter"] as? Int {
            bestAttemptContent.title = "\(username) and \(count) others  have commented on your post"
    }
    bestAttemptContent.body = "tap to see"
    let request = UNNotificationRequest(identifier: "push.comment", content: bestAttemptContent, trigger: nil)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

I tried using available-content : 1 in the notification's payload, but I'm not able to modify the notification when the app is terminated (not in background/foreground).

Basically I want a similar behaviour to facebook's messenger app.

Any Suggestions?

Push Notifications grouping is a feature that is provided in Android applications, but it is impossible to achieve the same on iOS. Because it should be handled by operation system(in other case it will be not possible to achieve when app is closed or minimized) and iOS doesn't provide this support.

So I read in Apple's documentation that when setting content-available : 1 in the aps object it launches the app in a background mode and its possible to handle the received silent push. its important to avoid setting the mutable-content : 1 and add background modes with remote notifications on in the apps configurations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM