1

I want to open a URL with actionable notification button, my code works when my device is not locked, but when the device is locked and I want to click the action button, the app crashes and this error message appears

NSLocalizedFailureReason=The request was denied by service delegate (SBMainWorkspace) for reason: Locked ("com.apple.mobilesafari cannot be launched because the device is locked") 

my code looks like this :

func userNotificationCenter(_ center: UNUserNotificationCenter,
                              didReceive response: UNNotificationResponse,
                              withCompletionHandler completionHandler: @escaping () -> Void) {
    
    let defaultsSettings = UserDefaults.standard
    let customToken =  defaultsSettings.value(forKey: "customToken")
    
    let userInfo = response.notification.request.content.userInfo
    
    // Perform the task associated with the action.
    switch response.actionIdentifier {
    
    case "open":
        if (userInfo["type"] != nil){
            if(userInfo["type"] as! String == "campfireOpened"){
                let title = userInfo["title"] as! String
                if let url = URL(string: "https://www.reloadium.com/news?search=\(title.replacingOccurrences(of: "#", with: "."))&customToken=\(customToken ?? "No Token")"){
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
                break
            }
            else if (userInfo["type"] as! String == "conversationOpened"){
                let title = userInfo["title"] as! String
                if let url = URL(string: "https://www.reloadium.com/news?search=\(title.replacingOccurrences(of: "#", with: "."))&customToken=\(customToken ?? "No Token")"){
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
                break
            }
            else if (userInfo["type"] as! String == "message"){
                let conversationId = userInfo["conversationId"] as! String
                if let url = URL(string: "https://www.reloadium.com/conversations?setActiveConversation=\(conversationId)&customToken=\(customToken ?? "No Token")"){
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
                break
            }
        }
        else {
            print("Type is null")
            print("User Info", userInfo)
            break
        }
    case "cancel":
        print("cancel button")
        break
            
    default:
        break
        
    }
    print(userInfo)
    // Always call the completion handler when done.
    completionHandler()
  }
}

Do you have any idea to open a URL with actionable notifications, when the device is locked?

Manatea Vagner
  • 111
  • 1
  • 9

1 Answers1

1

For open a URL with actionable notifications button, when the device is locked, you need to set .foreground options when you declare your UNNotificationAction like this :

   let open = UNNotificationAction(
        identifier: "open",
        title: "Open",
        options: .foreground
    )
Manatea Vagner
  • 111
  • 1
  • 9