Zhan Zhan - 1 year ago 114
Swift Question

How to display a specific ViewController in OneSignal Swift

I do app with

Swift 3
. I got push. How to display a specific
when notification is clicked. In push in additional data with field
I got link, but can't display this link in my

I try use global variable
to put url from
additional data
. But it not work.

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

OneSignal.initWithLaunchOptions(launchOptions, appId: "MYID", handleNotificationAction: { (result) in
let payload = result?.notification.payload
print("This is Payload \(payload)")
var fullMessage = payload?.title
let messageTitle = "OneSignal Example"

if (result?.action.actionID) != nil {
let additionalData = payload?.additionalData
let url = additionalData?["link"] as! String?
tempURL = url!
fullMessage = fullMessage! + "\nPressed ButtonId:\(url)"


let alertController = UIAlertController(title: messageTitle, message: fullMessage, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default)
alertController.show(alertController, sender: nil)

return true

Try to show:

func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {

let aViewController = ViewController()
aViewController.loadAddressURL(url: tempURL)
UIApplication.shared.keyWindow?.rootViewController?.present(aViewController, animated: true, completion:nil)


I have error:

fatal error: unexpectedly found nil while unwrapping an Optional value

Answer Source

first of all you can't pass data in your way. You should instantiate ViewController with StoryboardID. I explained how to use it with this link.

If you are pretty sure, your additionalData is not nil, you can pass your data with StoryboardID.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download