这是我在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)
}当我通过Xcode6.1.1在我的iPhone上启动应用程序时,绝对没有任何反应,我看不到我的println输出,但当我离开应用程序并进入设置->通知时,我发现我的应用程序启用了推送通知。
因为没有调用didRegisterForRemoteNotificationsWithDeviceToken,所以我得不到deviceToken
我做错了什么?
发布于 2015-03-03 03:00:51
也许问题是,在应用程序完成启动时调用的应用程序委托方法返回true之后,您调用了registerForRemoteNotifications()。试试这个:
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
}发布于 2015-04-19 01:23:17
我去了手机的设置,进入一般的->通知->,选择你的应用程序,->通知中心,把它改成10!更改后,我开始获取设备令牌数据!
https://stackoverflow.com/questions/28817070
复制相似问题