在App Delegate外部的ViewController中注册解析远程通知

时间:2015-06-07 01:45:43

标签: ios swift

我有一个带有@IBAction的ViewController用于提示用户通过调用AppDelegate中的方法来注册通知

class PermissionModalViewController: UIViewController {

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

@IBAction func presentPermissions(sender: AnyObject) {
    appDelegate.askNotificationsPermissions()
}

在我的AppDelegate.swift中,我有" didRegisterForRemoteNotificationsWithDeviceToken"功能

func askNotificationsPermissions() {
        if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))){
            println("i'm asking the do you want notifications question!")
            UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert | .Sound, categories: nil))
        }
    }

       func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
            println("successful register for push")
            let installation = PFInstallation.currentInstallation()
            installation.setDeviceTokenFromData(deviceToken)
            installation.saveInBackground()
        }

根据这个答案,Register for remote notifications outside of app delegate我应该在我的AppDelegate中调用通知注册的方法(在我的情况下为appDelegate.askNotificationPermissions())

然而,我的回调," didRegisterForRemoteNotificationsWithDeviceToken"没有被触发。为什么没有" #RegisterForRemoteNotificationsWithDeviceToken"不被触发?

1 个答案:

答案 0 :(得分:1)

好的,能够解决这个问题。

在我的AppDelegate.swift文件中,我有方法:

func askNotificationsPermissions() {
    if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))){
        println("i'm asking the question!")
        UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Badge | .Alert | .Sound, categories: nil))
        UIApplication.sharedApplication().registerForRemoteNotifications()
    }
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    println("successful register for push")
    let installation = PFInstallation.currentInstallation()
    installation.setDeviceTokenFromData(deviceToken)
    installation.saveInBackground()
}

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

在我的ViewController.swift中,我用来触发向用户发送远程通知的权限请求:

class PermissionModalViewController:UIViewController {

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

@IBAction func presentPermissions(sender: AnyObject) {
    appDelegate.askNotificationsPermissions()
}

简而言之,我调用了来自ViewController的AppDelegate中定义的askNotificationsPermissions()函数。

我在原始问题中遗漏的另一篇文章是registerForRemoteNotifications(),这是我在askNotificationsPermissions()问题中调用的方法。这需要触发didRegisterForRemoteNotificationsWithDeviceToken回调