NotificationCenter Post 请求从未收到

NotificationCenter Post request never gets received

我正在尝试 post 向我的应用程序中的 observable 发送一条消息,但由于某种原因它根本不起作用,我唯一可以归因于安装 https://bugfender.com/ can谁能告诉我这段代码有什么问题或如何找到根本原因,因为根本没有错误消息

两个片段都已移至 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
        )
    }    


}

您需要先注册您的观察员,然后才能post通知。