3

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 ?

Steven tan
  • 139
  • 1
  • 1
  • 12

2 Answers2

3

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)
}
Soohwan Park
  • 625
  • 4
  • 11
  • where should i add custom information to ? and if i add custom information, will my data be replace by this data ? example like : My web JSON is "Steven", and it will be replace by "test" ?? – Steven tan Dec 29 '15 at 08:31
  • I've edited my answer. You can add your custom information like that. @Steventan – Soohwan Park Dec 29 '15 at 08:42
  • ok but i have a question, i don't have "name" and "age", i can just don't use that data right ? ( According to my JSON ) @Soohwan Park – Steven tan Dec 29 '15 at 08:46
  • You don't have to add "name" and "age". You can add your custom data what ever you need and You can remove it from the JSON or you can simply ignore them from ios. @Steventan – Soohwan Park Dec 29 '15 at 08:52
  • yes i've done that, thanks to you now i can retrieve the Json data. however i still missing the notification showed up. i've update my page @Soohwan Park – Steven tan Dec 29 '15 at 08:53
  • change "sounds" to "sound" in "aps". And IOS notification is not shows up when the application is in foreground. @Steventan – Soohwan Park Dec 29 '15 at 08:55
  • Ok gonna try it, 1 question This function i have to put inside `if application.applicationState == UIApplicationState.Inactive {}` or i can just leave it like that ? (on my top post) – Steven tan Dec 29 '15 at 08:58
  • I don't know what is `PFPush` and `PFAnalytics`. but I think it wants to check if the app is opened by touching notification or not. I think if you want check push notification touch rate on `PARSE website`, leave it. @Steventan – Soohwan Park Dec 29 '15 at 09:02
  • i've updated some information. please look at it, also thanks it works fine. @Soohwan Park – Steven tan Dec 29 '15 at 09:28
  • When the app is in foreground, `didReceiveRemoteNotification` is called immediately. When the app is in background, Notification is shows up on the notification center and If the user clicks the notification from the notification center, `didReceiveRemoteNotification` is called and the app become foreground. When the app is terminated by `double home->swipe the app`, the notification is shows up on the notification center and If user clicks the notification, the application is launched and the the notification data comes to `didFinishLaunchingWithOptions`. @Steventan – Soohwan Park Dec 29 '15 at 09:48
  • So basically i have to put the code inside ApplicationState too ? – Steven tan Dec 29 '15 at 09:49
  • i got it, it only works for opening the application, how if my data contain link to website ? does it still work normally ? like when i press the notification it will auto-direct to the website : Https://example.com. ?? – Steven tan Dec 29 '15 at 09:55
  • No. that's not possible. You have to do it on your own in your application. @Steventan – Soohwan Park Dec 29 '15 at 09:56
  • Gonna try it, Thanks for your help ! – Steven tan Dec 29 '15 at 09:58
0

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": "",
}
jcaron
  • 17,302
  • 6
  • 32
  • 46
  • but the JSON i received is dynamic depend of what i send through my website. if i set at the top of my playload, it will become static right ? – Steven tan Dec 29 '15 at 08:20
  • @Steventan how could it become static? It's part of your payload. – jcaron Dec 29 '15 at 09:06
  • Updated answer to illustrate the structure of the payload. – jcaron Dec 29 '15 at 09:08
  • if i put my "links" inside "aps" will it be wrong ? Or its still working but with wrong structure ? – Steven tan Dec 29 '15 at 09:13
  • I would have thought it would not cause any issues, but since you're apparently having some with this incorrect structure, just stick to having your extra information in the right place, outside of `aps`. `aps` should be reserved for data that is used internally by iOS. – jcaron Dec 29 '15 at 09:18
  • Thanks for the information ,will be noted if something error occur to me later. Thanks! – Steven tan Dec 29 '15 at 09:24