Reputation: 704
I implemented parse push notifications in my project and I tested it out with a test code. In viewController:
var push = PFPush()
push.setMessage("This is a test")
push.sendPushInBackgroundWithBlock({
(isSuccesful: Bool!, error: NSError!) -> Void in
println(isSuccesful)
})
and in the appDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Here is the code for parse Push notifications.
Parse.setApplicationId("oIqRHQ8SBqLiuFzU5fIXRKgMVTHrH4ft6Gat7BW7", clientKey: "zPJ5SpDRFg9IqgiWZmW0N3FumzEwSDK1YvPxsipl")
var pushSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
application.registerUserNotificationSettings(pushSettings)
application.registerForRemoteNotifications()
// Override point for customization after application launch.
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
println("succesful")
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
println("failed :(")
}
When I launch the app, it prints successful and true. Also, on the parse.com website I can see the notifications were send. However, why can't I send a notification to my phone using the parse website? When I try to do that, it says there are no registered devices. But I did register my device (with the .p12 certificate).
What could solve this?
Upvotes: 0
Views: 385
Reputation: 389
I guess what you forgot is to save the deviceToken for your installation on parse. Try this:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
PFInstallation.currentInstallation().setDeviceTokenFromData(deviceToken)
PFInstallation.currentInstallation().saveInBackgroundWithBlock() { (success, error) in
if error != nil {
println("Saving failed")
}
if success {
println("Saved the new device push token to parse successfully")
}
}
}
Upvotes: 2