NotificationCenter - >额外的论点

时间:2017-05-17 11:50:01

标签: ios swift push-notification notificationcenter

在我的应用程序中,我有复杂的架构,所以一开始就有一点理论。当我收到远程通知时,我致电

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

在另一个文件中,我使用选择器函数制作了UIViewController扩展名,如:

func myFunction(_ notification: Notification) { }

现在我在我的一个视图控制器中做了什么(让我们叫他MyVC)我打电话给

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

MyVC包含带有对象的数组。当应用程序收到推送通知时,我需要在myFunction内处理此数组,但实际上我不知道如何传递它。

我尝试的是在选择器功能中添加额外的参数但没有成功。我能以某种方式实现吗?

编辑那么将我的数组传递到object函数中的.addObserver参数怎么样?我可以通过致电myFunction来获取notification.object吗?

2 个答案:

答案 0 :(得分:0)

您可以使用

将通知传递给myFunction
NotificationCenter.default.addObserver(self, selector: #selector(myFunction(_:)), name: Notification.Name("myName"), object: nil)

然后在myFunction处理您的通知,以从通知对象的userInfo中提取数据。

答案 1 :(得分:0)

在AppDelegate中接收远程通知(APN):

//---------------------------------------------------------
// 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)
}

RemoteNotificationProcessor的实现(传递信息):

// 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])
}

处理#selector中的数据

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

    }
}