在斯威夫特远程通知注册时未收到设备令牌 [英] Device Token not received when registering for remote notifications in Swift
问题描述
我莫名其妙地为远程通知注册时无法接收设备令牌。我得到的语气说:你要允许X程序,以便能够向您发送notificaitons,但是当我接受它,则didRegisterForRemoteNotifications函数没有被调用。
I somehow can not receive the device token when registering for remote notifications. I get the modal saying "Do you want to allow App X to be able to send you notificaitons", but when I accept it, the didRegisterForRemoteNotifications function is not called.
当我注册使用这个code iOS中8 /斯威夫特远程通知:
When I register for remote notifications in iOS 8/Swift using this code:
UIApplication.sharedApplication().registerForRemoteNotifications()
let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
这些功能不会被触发在所有的:
These functions are not triggered at all:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData!)
和
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError!)
但是当我登录这个
however when I log this
println("current settings \(UIApplication.sharedApplication().currentUserNotificationSettings()) and \(UIApplication.sharedApplication().isRegisteredForRemoteNotifications())")
我接受
"current settings <UIUserNotificationSettings: 0x170437120; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);> and true"
我的provisioning profile和证书AR一切为了。
My provisioning profile and certificates ar all in order.
已经有人人都有这个问题?
Has someone else had this problem?
推荐答案
您可以试试这个
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
var types: UIUserNotificationType = UIUserNotificationType.Badge |
UIUserNotificationType.Alert |
UIUserNotificationType.Sound
var settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
var characterSet: NSCharacterSet = NSCharacterSet(charactersInString: "<>")
var deviceTokenString: String = (deviceToken.description as NSString)
.stringByTrimmingCharactersInSet(characterSet)
.stringByReplacingOccurrencesOfString( " ", withString: "") as String
println(deviceTokenString)
}
编辑:更新斯威夫特2.X
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
return true
}
这篇关于在斯威夫特远程通知注册时未收到设备令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!