【问题标题】:Swift 5 store event until all of the app has initialisedSwift 5 商店事件,直到所有应用程序都已初始化
【发布时间】:2020-06-13 23:27:20
【问题描述】:

我正在使用 Swift 开发 Ionic/Capacitor 原生插件

我正在尝试集成 OneSignal 推送通知服务,如果应用程序在前台并且用户单击通知,我的下面的代码可以正常工作,但是如果应用程序从单击通知开始,我的事件不会触发,我想这是因为 AppDelegate.swift 在 Capacitor/Ionic 之前加载,当我的 NotificationCenter.default.post 被触发时,数据不会到达我的插件。

我想我想知道 Swift 中是否有任何方法可以存储此帖子数据,直到应用程序的所有部分都已加载和初始化,即电容器/离子网络视图

AppDelegate.swift

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


        let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
           // This block gets called when the user reacts to a notification received
           let payload: OSNotificationPayload = result!.notification.payload

           var fullMessage = payload.body

            let nc = NotificationCenter.default
                nc.post(name: Notification.Name("TestingEvents"), object: nil)

           if payload.additionalData != nil {
              if payload.title != nil {
                 let messageTitle = payload.title
                    print("Message Title = \(messageTitle!)")
              }

              let additionalData = payload.additionalData
              if additionalData?["actionSelected"] != nil {
                 fullMessage = fullMessage! + "\nPressed ButtonID: \(additionalData!["actionSelected"])"
              }
           }
        }


    return true
  }

MyPlugin.swift

    public override func load() {
        let nc = NotificationCenter.default
            nc.addObserver(self, selector: #selector(handleSignal), name: Notification.Name("TestingEvents"), object: nil)
    }

    @objc func handleSignal() {
        self.bridge.triggerWindowJSEvent(eventName: "myCustomEvent")
        self.notifyListeners("myPluginEvent", data: [:])
    }

还有我的 app.component.ts

      window.addEventListener('myCustomEvent', () => {
        alert("myCustomEvent ")
      });            
      Plugins.myPlugin.addListener("myPluginEvent", () => {
        alert("myPluginEvent ")
      });

【问题讨论】:

    标签: ios swift swift3 swift4 swift5


    【解决方案1】:

    这是一个逻辑问题。
    您可以在此处将通知数据 (OSNotificationPayload) 保存在您在 didFinishLaunchingWithOptions 中收到的对象/字典/数组中并添加观察者 "HandleNotificaionAfterPluginSetup "appdelegate。 对于此观察者,您可以在插件初始化后触发发布通知,并准备好处理通知。
    一旦你在你的插件中设置好并触发了代码可以在你刚刚设置的通知处理程序中执行的通知,它将执行代码

                  if payload.title != nil {
                     let messageTitle = payload.title
                        print("Message Title = \(messageTitle!)")
                  }
                  let additionalData = payload.additionalData
                  if additionalData?["actionSelected"] != nil {
                     fullMessage = fullMessage! + "\nPressed ButtonID: \(additionalData!["actionSelected"])"
                  }
               }```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-17
      • 2019-09-23
      相关资源
      最近更新 更多