Swift - UNNotification center

190 Views Asked by At

I have multiple controllers:

  • Home VC
  • MyNotifVC

In myNotifVC I have a list of all the pending notification.

I am trying to update the number of notification in HomeVC as the notifications are delivered.

So basically I would like to update a var (var deliveredNotif: Int) in HomeVC as soon as a notification is delivered (independently from which controller is active).

So far in myNotifVC i have:

     func getDeliveredNotif(){
    UNUserNotificationCenter.current().getDeliveredNotifications(completionHandler: {deliveredNotifications -> () in
        if let notifVC = self.presentingViewController as? HomeVC {
            notifVC.delivered = deliveredNotifications.count
        }
    })
}

How can I store in my var delivered (which is declared in my HomeVC) the number of delivered notification either if HomeVC is running or as soon as it will be running?

Thank you!

1

There are 1 best solutions below

1
On

Do this-

  1. Add observer in HomeVC for a notification "NewNotificationRecieved" (also make sure to remove it in deinit method)
  2. Send the notification "NewNotificationRecieved" with the delivered notifcations count in userinfo in MyNotifVC whenever new notification arrives
  3. Whenever new notification will be recieved in MyNotifVC, it will be broadcasted to all observing view controllers and accordingly they can update the UI.