简体   繁体   English

注册iOS推送通知时的奇怪行为

[英]Strange behavior when registering for iOS Push Notifications

This is the code I've put in AppDelegate to register for push notifications : 这是我放入AppDelegate中以注册推送通知的代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {  
    let notificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
    let settings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    return true
}

func application(application:UIApplication!, didRegisterForUserNotificationSettings notificationSettings:UIUserNotificationSettings) {
    println("[AppDelegate][didRegisterForUserNotificationSettings]")
    UIApplication.sharedApplication().registerForRemoteNotifications()
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    println("[AppDelegate][didFailToRegisterForRemoteNotificationsWithError]")
    println(error)
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    println("[AppDelegate][didRegisterForRemoteNotificationsWithDeviceToken]")
    println(deviceToken)
}

Absolutely nothing happens when I launch the app on my iPhone through Xcode 6.1.1, I don't see my println output but when I leave the app and go to Settings -> Notifications I find that Push Notifications are enabled for my app. 当我通过Xcode 6.1.1在iPhone上启动应用程序时,绝对没有任何反应,但是看不到我的println输出,但是当我离开应用程序并转到“设置”->“通知”时,发现为我的应用程序启用了“推送通知”。

Since didRegisterForRemoteNotificationsWithDeviceToken is not called, I don't get the deviceToken 由于未调用didRegisterForRemoteNotificationsWithDeviceToken ,因此无法获取deviceToken

What am I doing wrong ? 我究竟做错了什么 ?

Maybe the problem is that you are calling registerForRemoteNotifications() after returning true from the app delegate method called when app did finish launching. 可能的问题是,当app完成启动时,从调用的app委托方法返回true后,您正在调用registerForRemoteNotifications() Try this: 尝试这个:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {  
    let notificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
    let settings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)

    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications() 

    return true
}

I went to the settings of phone -> go to general -> notification -> choose your app -> center of notification -> change it to 10! 我去了电话的设置->转到常规->通知->选择您的应用程序->通知中心->将其更改为10! after changing it i started to get device token data! 更改后,我开始获取设备令牌数据!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM