【问题标题】:Cannot find type 'OSHandleNotificationActionBlock' in scope在范围内找不到类型“OSHandleNotificationActionBlock”
【发布时间】:2021-09-21 10:34:30
【问题描述】:

我正在使用带有flutter的onesignal,并且我实现了onesignal通知服务。现在我想在不打开应用程序的情况下处理通知操作按钮,所以我在 notificationService didReceive 函数中添加了这些行。

let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
          if let actionID = result?.action.actionID {
            print("actionID =", actionID)
          }
        }

但我收到此错误。

Cannot find type 'OSHandleNotificationActionBlock' in scope

我不确定我做错了什么,我不应该在这个类中使用这个代码吗?还是我错过了导入包或类似的东西?

【问题讨论】:

    标签: android ios swift flutter onesignal


    【解决方案1】:

    iOS 要求在 didFinishLaunchingWithOptions 中设置 iOS API 回调。否则,如果您点击导致应用程序冷启动的通知,SDK 将错过该事件。我不确定 Capacitor 如何与此挂钩,但由于它是 iOS API 限制,因此您需要以相同的方式处理此问题

    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"])"
                  }
               }
            }
            ....
        //START OneSignal initialization code
        let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false]
    
        // Replace 'YOUR_APP_ID' with your OneSignal App ID.
        OneSignal.initWithLaunchOptions(
          launchOptions,
          appId: "xxxxxxx",
          handleNotificationAction: notificationOpenedBlock,
          settings: onesignalInitSettings
        )
    ....
        return true
      }
    

    【讨论】:

      猜你喜欢
      • 2021-09-24
      • 2021-08-01
      • 2022-01-16
      • 2021-08-03
      • 2021-11-29
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 2023-01-26
      相关资源
      最近更新 更多