ios - 在 App Delegate 之外的 ViewController 中注册解析远程通知

标签 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”没有被触发。为什么不触发“didRegisterForRemoteNotificationsWithDeviceToken”?

最佳答案

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

在我的 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 中,我使用它来触发对用户的请求以授予我们发送远程通知的权限:

类 PermissionModalViewController: UIViewController {

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

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

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

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

关于ios - 在 App Delegate 之外的 ViewController 中注册解析远程通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30689275/

相关文章:

ios - 如何设置iOS 13深色模式大标题颜色?

ios - UITableView 意外发现 Nil 不一致发生

swift - 使用 Swift 函数计数进行黑客攻击不可用,没有普遍好的答案

ios - Swift 2.1 Core Data - 使用 setValue() 预填充数据只保留最后一个

ios - Swift 4.0 的 CryptoSwift Lib 安装失败

ios - iphone 6 的自定义导航栏图像

ios - iOS 应用中的 Vize.ai 图像识别

ios - 在 Facebook 应用程序中打开帖子的 URL Scheme

ios - 如何让用户在具有多个 UIButton 的 View 中只选择一个自定义 UIButton? swift 3

ios - 处理 textField 中的无文本(swift 3)