0

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!

user1046037
  • 16,755
  • 12
  • 92
  • 138
Marco
  • 1,051
  • 1
  • 17
  • 40
  • It is best to use MVC (Model, View, Controller) Architecture, Model will store all the data / business logic. In your case Model will store the data related to number of notifications. Create this model in the AppDelegate and pass the same instance of Model to the different view controllers – user1046037 Sep 18 '17 at 11:55
  • @user1046037 you mean to create a class just to store the number of delivered notification? – Marco Sep 18 '17 at 11:56
  • 1
    Yes, it would be a separate class, something that would make logical sense to your app logic. And this class would be independent of any view controller and is the golden source of data. So if any view controller wants this data, they can access it. Every ViewController should be assigned the reference to the same class (during creation or segue) so that they all are pointing to the same instance. – user1046037 Sep 18 '17 at 11:59
  • I am sure there is more related data you would like to store across view controllers. So design your class to be a Model. If you are using CoreData then the `NSManagedObject` could serve as a model too. – user1046037 Sep 18 '17 at 12:06
  • All i need to do is to check if UIApplication.shared.applicationIconBadgeNumber changed...i do not need to store data actually.. if the badenumber changed its value then do smt... – Marco Sep 18 '17 at 12:08
  • How about creating a static property `HomeVC.delivered`? – algrid Sep 18 '17 at 12:22
  • @algrid all i need to do is to check (if HomeVC is running) if UIApplication.shared.applicationIconBadgeNumber changed. i cannot call it on viewWill appear because if the view is open... well it won't works... – Marco Sep 18 '17 at 12:25
  • Declare the property in HomeVC: `static Int delivered = 0` and then you can use it at any time even when your view controller is not instantiated. – algrid Sep 18 '17 at 12:31
  • @algrid ok... but if HomeVC is running and delivered changed... it would not show the change until reload the controller right? how can I display that change immediately the property changes without user interaction? – Marco Sep 18 '17 at 12:33
  • @Marco you can use notifications for that - see NSNotificationCenter – algrid Sep 18 '17 at 12:39
  • https://medium.com/@JoyceMatos/using-nsnotificationcenter-in-swift-eb70cf0b60fc – algrid Sep 18 '17 at 12:40
  • @sorry but i cannot figure it out... where should i put the observer? I need to observe IconBadgeNumber change when the notif is delivered. If i have it in viewWillAppear it will works as long as i reload the controller. But i don't how i intercept the change in IconBadgeNumber? – Marco Sep 18 '17 at 13:17

1 Answers1

0

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.
Vishwas Singh
  • 1,497
  • 1
  • 18
  • 16
  • can you check here please? https://stackoverflow.com/questions/46285135/swift-change-in-unnotification-badge-number – Marco Sep 18 '17 at 17:36