Reputation: 139
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 ?
Upvotes: 3
Views: 2181
Reputation: 17710
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": "",
}
Upvotes: 0
Reputation: 635
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)
}
Upvotes: 3