Cannot find type 'OSHandleNotificationActionBlock' in scope

Viewed 475

I am using onesignal with flutter, and I implemented onesignal notification service. Now I want to handle notification action buttons without opening the app, so I added these lines inside notificationService didReceive function.

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

but I am getting this error.

Cannot find type 'OSHandleNotificationActionBlock' in scope

I am not sure what I'm doing wrong, am I not supposed to use this code in this class? Or did I miss importing a package or something like this?

1 Answers

iOS requires that iOS API callbacks are setup in didFinishLaunchingWithOptions. Otherwise, if you tap on a notification that causes the app to be cold started the SDK will miss the event. I am not sure how Capacitor hooks into this but since it is an iOS API limitation so you will need to handle this in the same way

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
  }
Related