NotificationCenter发布请求从未收到

问题描述 投票:0回答:1

我正在尝试将消息发布到我的应用中的可观察对象,但由于某种原因它根本无法正常工作,我唯一能归因于的是安装https://bugfender.com/,谁能告诉我此代码有什么问题或如何找出根本原因,因为根本没有错误消息

两个片段均已移至AppDelegate

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {


     NotificationCenter.default.post(name: Notification.Name("TestPost"), object: nil)
     NotificationCenter.default.addObserver(self, selector: #selector(onDidReceiveData(_:)), name: Notification.Name("TestPost"), object: nil)

    return true
  }

并且接收器功能为

    @objc func onDidReceiveData(_ notification:Notification) {
        // Do something now
        print("XXXXXX received")
    }

=====用我的自定义电容器插件的实际代码更新========

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    ... code for receiving notification starts here

     NotificationCenter.default.post(name: Notification.Name("TestPost"), object: nil)

    ... code for receiving notification ends here


    return true
  }
import Foundation
import Capacitor

@objc(myHelpers)
public class myHelpers: CAPPlugin {

    public override func load() {
        print("PluginLoaded")

       let nc = NotificationCenter.default
            nc.addObserver(self, selector: #selector(handleSignal), name: Notification.Name("TestPost"), object: nil)

    }

    @objc func handleSignal()
    {
    print("XX WE RECEIVED AN EVENT AT handleSignal")
        notifyListeners(
            "myPluginEvent",
            data: [:],
            retainUntilConsumed: true
        )
    }    


}
swift swift3
1个回答
0
投票

发布通知之前,您需要先注册观察员。

© www.soinside.com 2019 - 2024. All rights reserved.