Matt Ellison
Matt Ellison

Reputation: 332

Parse Push Notifications - Swift Installation Not Working

I am trying to get Parse push notifications working on my app (all swift) but while trying to implement, I get the error 'PFInstallation' does not have a member named 'saveInBackground'

Here is my code.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    Parse.setApplicationId("APP ID HIDDEN", clientKey: "CLIENT ID HIDDEN")

   // let notificationTypes:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
    //let notificationSettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
    var notificationType: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound

    var settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationType, categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    UIApplication.sharedApplication().registerForRemoteNotifications()

    //UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
    // Override point for customization after application launch.
    return true
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings!) {
   UIApplication.sharedApplication().registerForRemoteNotifications()

}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

    var currentInstallation: PFInstallation = PFInstallation()
    currentInstallation.setDeviceTokenFromData(deviceToken)
    currentInstallation.saveInBackground()

    println("got device id! \(deviceToken)")

}


func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    println(error.localizedDescription)
    println("could not register: \(error)")
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    PFPush.handlePush(userInfo)
}

When I change the currentInstallation.saveInBackground to currentInstallation.saveEvenutally() , the code compiles fine..

But when trying to successfully sign up for push notifications, an error pops up in the console saying Error: deviceType must be specified in this operation (Code: 135, Version: 1.4.2)

I have spent hours trying to figure this out, no dice, any help is appreciate.

Upvotes: 8

Views: 4462

Answers (4)

kevcol
kevcol

Reputation: 1243

In addition to importing Bolts, I fixed the same error in my app by changing the function to

 func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    // Store the deviceToken in the current Installation and save it to Parse
    let installation = PFInstallation.currentInstallation()
    installation.setDeviceTokenFromData(deviceToken)
    installation.saveInBackground()
}

From the Parse guide on Push Notifications (as opposed to quickstart guide): https://parse.com/docs/ios/guide#push-notifications

Upvotes: 2

Kaptain
Kaptain

Reputation: 1348

Just write import Bolts in you AppDelegate.swift file

Upvotes: 4

Andrew
Andrew

Reputation: 2468

A valid PFInstallation can only be instantiated via [PFInstallation currentInstallation] because the required identifier fields are readonly. (source)

So instead of:

var currentInstallation: PFInstallation = PFInstallation()

Try:

var currentInstallation = PFInstallation.currentInstallation()

Upvotes: 7

Matt Ellison
Matt Ellison

Reputation: 332

To anyone else who has this error, make sure you import the Bolts framework into your Bridging Header file

Which isn't outlined in their crap docs.

That fixes the issue.

Below is the code.

#import <Parse/Parse.h>
#import <Bolts/Bolts.h>

Just add that to your bridging header then you are good to go. Thanks

Upvotes: 22

Related Questions