0

In my app I have complicated architecture, so a little bit of theory at start. When I receive remote notification I call

NotificationCenter.default.post(Notification.Name("myName"), 
                                object: nil,
                                userInfo: userInfo)

in AppDelegate.

In another file I made an UIViewController extension with selector function like:

func myFunction(_ notification: Notification) { }

Now what I do in one of my view controllers (let's call him MyVC) I call

override viewWillAppear(_ animated: Bool) {
    NotificationCenter.default.addObserver(self,
                                           selector:#selector(myFunction),
                                           name: Notification.Name("myName"),
                                           object: nil)
}

MyVC contains array with objects. When application receives a push notification I need to process this array inside myFunction but in fact I don't know how to pass it.

What I tried was adding extra argument into selector function but unsuccessfully. Can I achieve it somehow?

Edit: And what about passing my array into the object parameter inside .addObserver function? Will I be able to get it in myFunction by calling notification.object?

Caleb
  • 124,013
  • 19
  • 183
  • 272
mcgtrt
  • 657
  • 6
  • 22

2 Answers2

0

You can pass the notification to the myFunction by using

NotificationCenter.default.addObserver(self, selector: #selector(myFunction(_:)), name: Notification.Name("myName"), object: nil)

And then process your notification in myFunction to extract the data from the notification object's userInfo.

Frederik
  • 384
  • 3
  • 7
0

Receiving a Remote Notification (APN) in AppDelegate :

//---------------------------------------------------------
// Notification event received when APN comes in ... pre 10.0+
func application(_ application: UIApplication,
                 didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
{
    RemoteNotificationProcessor.handleRemoteNotification(userInfo, handler : completionHandler)
}

//-------------------------------------------------------------------------
// UNUserNotificationDelegate implementation 10.0+
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
    // Received a Local or Remote Notification (ios 10.0+), we can do what we want
    RemoteNotificationProcessor.handleRemoteNotification(notification, handler : completionHandler)
}

Implementation of RemoteNotificationProcessor (passing on the info):

// NON 10.0+ version....
class func handleRemoteNotification(_ notif : [AnyHashable : Any],
                                    handler : @escaping (UIBackgroundFetchResult) -> Void)
{
    NotificationCenter.default.post(name: Notification.Name("myName"),
                                                              object: nil,
                                                              userInfo: notif)

    handler(UIBackgroundFetchResult.newData)
}

@available(iOS 10.0, *)
class func handleRemoteNotification(_ notif : UNNotification,
                                    handler : @escaping (UNNotificationPresentationOptions)->Void)
{
    guard let trigger = notif.request.trigger else {
        print("Don't know origin of notification trigger")
        handler([.alert, .badge, .sound])
        return
    }
    if trigger is UNPushNotificationTrigger {
        if let json = notif.request.content.userInfo as? [String : Any]
        {
           NotificationCenter.default.post(name: Notification.Name("myName"),
                                                              object: nil,
                                                              userInfo: info)
    }
    else {
        print("Notified other than APN")
    }
    handler([.alert, .badge, .sound])
}

Handling data in your #selector

@objc
func myFunction(_ notification : Notification)
{
    if let info = (notification as NSNotification).userInfo {

    }
}
GNewc
  • 445
  • 4
  • 4