0

I'm integrating Parse Push notifications into an app, and have got caught up in Swift 2.0 conversions. Code is:

if application.respondsToSelector("registerUserNotificationSettings:") {
            let userNotificationTypes: UIUserNotificationType = [.Alert, .Badge, .Sound]
            let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
            application.registerUserNotificationSettings(settings)
            application.registerForRemoteNotifications()
        } else {
            let types: UIUserNotificationType = [.Badge, .Alert, .Sound]
            application.registerForRemoteNotificationTypes(types)
        }

Xcode complains that "Cannot convert value of type 'UIUserNotificationType' to expected argument type 'UIRemoteNotificationType'

3 Answers 3

6

Try the following code, since UIRemoteNotification is deprecated, use registerUserNotificationSettigns to set the notifications settings. But remember to configure push notifications at apple's member center and at parse so it can work. You can follow this tutorial from step 1 to step 4, it's great. https://www.parse.com/tutorials/ios-push-notifications

if application.respondsToSelector("registerUserNotificationSettings:") {
   let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
   application.registerUserNotificationSettings(settings)
   application.registerForRemoteNotifications()
}
Sign up to request clarification or add additional context in comments.

Comments

3

Try the following code, though Xcode still shows warnings that UIRemoteNotificationType was deprecated in iOS 8.0

let types : UIRemoteNotificationType = [.Badge, .Alert, .Sound]
application.registerForRemoteNotificationTypes(types)

Comments

2

try this code ...

app delegate.swift

import UIKit
import Parse

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

//--------------------------------------
// MARK: - UIApplicationDelegate
//--------------------------------------

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    Parse.setApplicationId("APIKEYHERE", clientKey: "CLIENTKEYHERE")

    let types:UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
    let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)

    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()

    return true
}

//--------------------------------------
// MARK: Push Notifications
//--------------------------------------

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let installation = PFInstallation.currentInstallation()
    installation.setDeviceTokenFromData(deviceToken)
    installation.saveInBackground()

    PFPush.subscribeToChannelInBackground("") { (succeeded: Bool, error: NSError?) in
        if succeeded {
            print("ParseStarterProject successfully subscribed to push notifications on the broadcast channel.\n");
        } else {
            print("ParseStarterProject failed to subscribe to push notifications on the broadcast channel with error = %@.\n", error)
        }
    }
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    if error.code == 3010 {
        print("Push notifications are not supported in the iOS Simulator.\n")
    } else {
        print("application:didFailToRegisterForRemoteNotificationsWithError: %@\n", error)
    }
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    PFPush.handlePush(userInfo)
    if application.applicationState == UIApplicationState.Inactive {
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
    }
  }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.