【问题标题】:Send push notification via parse to your iphone as a test通过解析向您的 iPhone 发送推送通知作为测试
【发布时间】:2015-03-26 19:41:19
【问题描述】:

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

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

什么可以解决这个问题?

【问题讨论】:

    标签: xcode swift parse-platform push-notification


    【解决方案1】:

    我猜你忘记的是在解析时保存 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")
            }
        }
    }
    

    【讨论】:

    • 我想把它添加到 AppDelegate 中?我正在尝试! :)
    • 尝试用我的方法替换你的方法:p
    • :) 你不需要每次都设置和保存令牌,否则你会浪费 api 调用。就我而言,它只会在用户重新安装时发生变化
    • 哦,但是我可以在我要上传到应用商店的应用中使用此代码吗?
    • 当然,我的意思是当您没有在每次应用程序注册远程通知时设置此 API 调用时,您将在解析时保存 API 调用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多