无法为推送通知注册设备 (iOS8)

Posted

技术标签:

【中文标题】无法为推送通知注册设备 (iOS8)【英文标题】:Cannot register device for push notifications (iOS8) 【发布时间】:2015-02-11 03:07:39 【问题描述】:

我正在按照 Parse 教程为我的设备配置推送通知。

https://www.parse.com/tutorials/ios-push-notifications

我的苹果开发者屏幕显示它们已启用:

我已经安装了推送证书:

我已将 .p12 证书添加到 Parse 的设置部分:

我已将请求的代码添加到我的 appdelegate:

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) 
    PFPush.handlePush(userInfo)


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


/** Get the availability to use the GPS at any given time */
func availabile() -> Bool 
    return !(locationServices.getGlobalManager().location == nil)


  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
    // Override point for customization after application launch.
    Parse.setApplicationId("<redacted>",  clientKey:"<redacted>")


    let userNotificationTypes = (UIUserNotificationType.Alert |
        UIUserNotificationType.Badge |
        UIUserNotificationType.Sound);

    let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()

    GMSServices.provideAPIKey(GoogleAPIKey.apiKey());

    locationServices.resolveLocationServices 
        success in
        if success 
            println("Location services success - \(self)")
        
        else 
            println("Locations services not allowed - \(self)")
        
    

    return true

当我尝试发送测试推送通知(我的应用程序在我的 iPhone 6+ 上运行)时,我收到没有设备注册的消息。我相信我已经正确地遵循了每一步,但我在这里不知所措。

【问题讨论】:

【参考方案1】:
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()

问题是,您尝试注册远程通知的时间过早,即您应该在用户允许您的应用接收通知后注册通知。你可以在 app delegate 的 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings; 方法中了解到这一点。只需在 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings 方法中执行 application.registerForRemoteNotifications() 即可,一切正常。祝你好运!

【讨论】:

我没有 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings 方法。你能告诉我这个方法应该是什么样子吗? 看看UIApplicationDelegate 协议文档,它就在那里。请注意,它仅适用于 iOS8 及更高版本。

以上是关于无法为推送通知注册设备 (iOS8)的主要内容,如果未能解决你的问题,请参考以下文章

推送通知声音在 iOS8 上不起作用

Parse 不为推送通知注册设备

如何向 Azure 通知中心注册的每个设备发送不同的推送通知?

使用 Amazon SNS 时无法接收到 iOS 设备的推送通知

由于应用程序/无效凭据错误,无法发送设备推送通知

Phonegap 在设备上启用推送通知