ios - 无法使用 swift 在 iPhone 6 中注册远程通知

标签 ios swift notifications

我正在使用以下代码为我的应用注册远程通知

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

    let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: categories)
    application.registerUserNotificationSettings(notificationSettings)
    application.registerForRemoteNotifications()
    return true
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    print("token: \(deviceToken)")
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print("Error : Fail to register to APNS : \(error)")
}

对于 iPhone 6,它不会调用方法 didRegisterForRemoteNotificationsWithDeviceTokendidFailToRegisterForRemoteNotificationsWithError,对于其他设备,如 5c/4s,它正在注册并打印 deviceToken

最初我认为这是操作系统问题,但后来,我检查了具有相同版本 9.3.2 的其他设备,它有效。

有人遇到同样的问题吗?

最佳答案

把这段代码写在 didFinishLaunchingWithOptions 中用于远程通知。我认为它会工作正常

if #available(iOS 8.0, *) {

    let userNotificationTypes:UIUserNotificationType = ([UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound])

    if UIApplication.sharedApplication().respondsToSelector("isRegisteredForRemoteNotifications") {


        // iOS 8 Notifications
        let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(settings)
        UIApplication.sharedApplication().registerForRemoteNotifications()


    } else {

        // iOS < 8 Notifications

        UIApplication.sharedApplication().registerForRemoteNotificationTypes([.Badge, .Sound, .Alert])

    }
    print("Push notifications setup complete")

}

快乐编码。

关于ios - 无法使用 swift 在 iPhone 6 中注册远程通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36886069/

相关文章:

ios - NSOperationQueue的多个分配使我的应用程序崩溃

ios - fatal error : could not demangle keypath type

android - 在现有的iOS和Android项目中集成React Native,无需修改现有的目录结构

ios - 在 Swift 中运行 Core Data Manager 的类初始值设定项

android - 检索 StatusbarNotification 详细信息(标题、通知文本)的可靠方法

ios - 如何使用 UIButton 单击事件移动下一页和上一页?

swift - 同时接收scrollViewDidScroll和UIGestureRecognizer触摸事件

ios - 为什么在相似的代码片段中初始化的顺序不同。 swift 4

ios - UNCalendarNotificationTrigger 不适用于完整的日期组件

vim - 在 MacVim 中禁用铃声