edamov / pushok

PHP client for Apple Push Notification Service (APNs) - Send push notifications to iOS using the new APNs HTTP/2 protocol with token-based (JWT with p8 private key)
MIT License
380 stars 122 forks source link

How do you handle the response in Swift 3 it gives? #14

Closed TheDagger closed 7 years ago

TheDagger commented 7 years ago

I have this code when I get the push notification, push notifications work by the way.

// Push notification received
    func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
        // Print notification payload data
        print("Push notification received: \(data)")

        let aps = data[AnyHashable("aps")]!

        print(aps)
    }

Here is the console output...

Push notification received: [AnyHashable("samplekey"): samplevalue, AnyHashable("aps"): {
    alert =     {
        body = hey;
        title = "Hello!";
    };
    sound = default;
}]
{
    alert =     {
        body = hey;
        title = "Hello!";
    };
    sound = default;
}

How do I get access to the variables in the alert object in Swift3?

TheDagger commented 7 years ago

Finally found answer, hope this is how you intended it

// Push notification received
    func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
        // Print notification payload data
        print("Push notification received: \(data)")

        let aps = data[AnyHashable("aps")]! as! NSDictionary

        let alert = aps["alert"]! as! NSDictionary

        let body = alert["body"] as! String
        let title = alert["title"] as! String

        print("body=\(body)")
        print("title=\(title)")

    }
edwardmp commented 7 years ago

This question is not really PushOK related, more Swift/iOS/APNS related. You should close this issue.

AnkiReddyPolu commented 7 years ago

// Push notification received func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) { // Print notification payload data print("Push notification received: (data)")

    let aps = data[AnyHashable("aps")]! as! NSDictionary

    let alert = aps["alert"]! as AnyObject as! NSDictionary
    print("alert=\(alert)")

     let body = alert["body"] as AnyObject as! NSDictionary

    if let content_type = body["content_type"] as? String {
        print("content_type is : \(content_type)")
    }
    if let information = body["information"] as? String {
        print("information is : \(information)")
    }

    if let title = alert["title"] as? String {
        print("title is : \(title)")
    }
}