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

标签 xcode swift parse-platform push-notification

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

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 证书)。

什么可以解决这个问题?

最佳答案

我想您忘记了在解析时保存用于安装的 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")
        }
    }
}

关于xcode - 通过解析将推送通知发送到您的 iPhone 作为测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29287428/

相关文章:

ios - 保存图像以进行解析

iphone - 对 Xcode 的愤怒担忧

ios - Swift 中的初始化和作用域

javascript - 快速且开放的声音控制 (OSC) 通信

javascript - Parse.com后台作业云代码无法调用未定义的方法 'then'

java - 解析 -Android 获取多个推送通知

iphone - UIImagePickerControllerEditedImage 问题

ios - xcode项目中的预编译头文件是什么以及在哪里?

ios - xcode Interface Builder 未更新 IBDesignable 类

ios - Swift - 如果语句不能正常工作