0

I am trying to get pending notification request on local notification. It throws me error: "Invalid conversion from throwing function of type '(_) throws -> Void' to non-throwing function type '([UNNotificationRequest]) -> Void' "

My code is:

var notificationTitle = "\(String(describing: notificationData!["title"]))"
var notificationInterval: Int = notificationData!["interval"] as! Int
let center  =  UNUserNotificationCenter.current()
center.getPendingNotificationRequests(completionHandler: {(requests) -> Void in
    var notificationExist:Bool = false
    for notificationRequest in requests {
        try{
            var notificationContent:UNNotificationContent = notificationRequest.content
        }
    }
Arasuvel
  • 2,971
  • 1
  • 25
  • 40
K.K
  • 79
  • 2
  • 8

3 Answers3

3

You might want to do it this way ,

    center.getPendingNotificationRequests(completionHandler: {requests -> () in
        var notificationExist:Bool = false
        for notificationRequest in requests {
            do {
                var notificationContent:UNNotificationContent = try notificationRequest.content
            }
            catch {
                print(error)
            }
        }
    }
AjinkyaSharma
  • 1,870
  • 1
  • 16
  • 26
1

I'm not sure which part of your code is throwing but this line of your code is not true:

try{
    var notificationContent:UNNotificationContent = notificationRequest.content
    }

The correct way of it is this:

do {
    var notificationContent:UNNotificationContent = try notificationRequest.content
}
catch {
print(error)
}
0

The problem is in your try block. So, you can replace it as shown below.

guard let notificationContent:UNNotificationContent = try? notificationRequest.content else {
    print("There was an error!")
}
Khushbu
  • 2,342
  • 15
  • 18