通过解析将推送通知作为测试发送到您的iphone

时间:2015-03-26 19:41:20

标签: xcode swift parse-platform push-notification

我在我的项目中实现了解析推送通知,并使用测试代码对其进行了测试。在viewController中:

var push = PFPush()
       push.setMessage("This is a test")
        push.sendPushInBackgroundWithBlock({
            (isSuccesful: Bool!, error: NSError!) -> Void in

            println(isSuccesful)

        })

并在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 :(")
}

当我启动应用程序时,它打印成功且真实。此外,在parse.com网站上,我可以看到通知已发送。但是,为什么我不能使用解析网站向我的手机发送通知?当我尝试这样做时,它说没有注册设备。但我确实注册了我的设备(使用.p12证书)。

有什么可以解决这个问题?

1 个答案:

答案 0 :(得分:2)

我猜你忘记的是在解析时为你的安装保存deviceToken。试试这个:

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")
        }
    }
}