简体   繁体   中英

Receive Custom Notification iOS swift 2.0

I tried to receive my custom notification from my web through parse website.

Code didReceiveRemoteNotification :

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        PFPush.handlePush(userInfo)
        if application.applicationState == UIApplicationState.Inactive {
         PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)}

Here is the JSON i received from parse website :

 [aps: {
     alert = "test adirax";
     sound = default; }]

It works well for me and the notification shows up. However when i tried to push data from my website, the notification can't shows/ pop up.

Here is my JSON looks like :

{"aps": {"alerts" : "test", 
         "links": "",
         "sounds": "default"}}

I tried to print(userInfo) and the result is i get all the data like above, but there is no notif.

I guess I'm missing some code to convert the data ?

INFORMATION

For the specific information, i tried receive notification from parse.com through subscribe "channels". so it's not a local notification or else.

ADD Code Added (according to my JSON type)

  if let launchOptions = launchOptions {
            let userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject: AnyObject]
            let aps = userInfo!["aps"] as? [NSObject: AnyObject]
            let alert1 = aps!["alerts"] as? String
            let link1 = aps!["links"] as? String
        }

And this :

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

        PFPush.handlePush(userInfo)
        if application.applicationState == UIApplicationState.Inactive {
            PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
        }
        let aps = userInfo["aps"] as? [NSObject: AnyObject]
        let alert1 = aps!["alerts"] as? String
        let link1 = aps!["links"] as? String
        print(userInfo)
        print("success")
    }

When i debugged one by one, its success i collect all the data, however i still missing the notification showed up ?

SOLVED PART 1 So far i managed to get the data and push the notification out, but it's only when i open the application. Code :

 func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    PFPush.handlePush(userInfo)
    if application.applicationState == UIApplicationState.Inactive {
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
    }

    let notifiAlert = UIAlertView()

    let aps = userInfo["aps"] as? [NSObject: AnyObject]
        let alert1 = aps!["alerts"] as? String
        let link1 = aps!["links"] as? String

        notifiAlert.title = alert1!
        notifiAlert.message = link1
        notifiAlert.addButtonWithTitle("OK")
        notifiAlert.show()

        print(userInfo)
        print("success")

}

I use a local notification trick, but how to pop up the notification when i'm not using the app ?

You have to add custom informations like this:

{"aps": {"alerts" : "test", 
         "sound": "default"},
 "name": "Steven",
 "age": "32"
}

And parse it like this:

let aps = userInfo["aps"] as? [NSObject: AnyObject]
let msg = aps!["alert"] as? String
let name = userInfo["name"] as? String
print(name)

EDIT: You have to check push notification on two functions of UIApplicationDelegate

First.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // check for push message
        if let launchOptions = launchOptions {
            let userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject: AnyObject]
            let aps = userInfo["aps"] as? [NSObject: AnyObject]
            let msg = aps!["alert"] as? String
            let name = userInfo["name"] as? String
            print(name)
        }
}

Second.

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    let aps = userInfo["aps"] as? [NSObject: AnyObject]
    let msg = aps!["alert"] as? String
    let name = userInfo["name"] as? String
    print(name)
}

You should add your custom data to the top-level of your payload, not to the aps object.

Like this, for instance:

{
    "aps": {
        "alerts" : "test", 
        "sounds": "default"
    },
    "links": "",
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM