Unable to register for Push Notifications (Xcode 7, iOS9)

前端 未结 2 1063
误落风尘
误落风尘 2021-01-14 11:53

I\'ve upgraded my devices to iOS 9 and my Xcode environment to 7.0 (7A220). My app registers for notifications via:

[[UIApplication sharedApplication] regis         


        
相关标签:
2条回答
  • 2021-01-14 12:03

    Do you use a simulator?

    In a simulator, remote notifications are not supported.

    sample code:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        let pushSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Badge ,UIUserNotificationType.Sound ,UIUserNotificationType.Alert], categories: nil)
        application.registerUserNotificationSettings(pushSettings)
        return true
    }
    
    func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
        application.registerForRemoteNotifications()
    }
    
    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        let token=deviceToken.description
        print(token)
    }
    
    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        print(error)
    }
    

    Xcode says:

    Error Domain=NSCocoaErrorDomain
    Code=3010 "REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED_NSERROR_DESCRIPTION"
    UserInfo={NSLocalizedDescription=REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED_NSERROR_DESCRIPTION}
    
    0 讨论(0)
  • 2021-01-14 12:08

    The reason why it wasn't working was because I wasn't calling:

    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    

    Prior to the call:

    [[UIApplication sharedApplication] registerForRemoteNotifications];
    
    0 讨论(0)
提交回复
热议问题