ios 在 Swift 中注册远程通知时未收到设备令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30012420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Device Token not received when registering for remote notifications in Swift
提问by Samuel B.
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.
注册远程通知时,我以某种方式无法接收设备令牌。我收到模态说“您是否希望允许 App X 向您发送通知”,但是当我接受它时,不会调用 didRegisterForRemoteNotifications 函数。
When I register for remote notifications in iOS 8/Swift using this code:
当我使用以下代码在 iOS 8/Swift 中注册远程通知时:
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!)
and
和
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError!)
however when I log this
但是当我记录这个
println("current settings \(UIApplication.sharedApplication().currentUserNotificationSettings()) and \(UIApplication.sharedApplication().isRegisteredForRemoteNotifications())")
I receive
我收到
"current settings <UIUserNotificationSettings: 0x170437120; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);> and true"
My provisioning profile and certificates ar all in order.
我的配置文件和证书都井井有条。
Has someone else had this problem?
有其他人遇到过这个问题吗?
回答by Mohammad Nurdin
You can try this
你可以试试这个
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)
}
EDIT: Update for Swift 2.x
编辑:更新 Swift 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
}
EDIT: Update for Swift 3.x
编辑:更新 Swift 3.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
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let characterSet = CharacterSet(charactersIn: "<>")
let deviceTokenString = deviceToken.description.trimmingCharacters(in: characterSet).replacingOccurrences(of: " ", with: "");
print(deviceTokenString)
}
回答by vedrano
For Swift 3.0 you can use:
对于 Swift 3.0,您可以使用:
let deviceTokenString = message.reduce("", { let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
+ String(format: "%02X", )})
回答by Priti Kanauziya
For swift 4.0 please follow some steps :
对于 swift 4.0,请按照以下步骤操作:
1).On push notification in capabilities(TARGET)
1).关于功能中的推送通知(TARGET)
2).Use this code in didFinishLaunchingWithOptions
2). 在 didFinishLaunchingWithOptions 中使用此代码
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data -> String in
return String(format: "%02.2hhx", data)
}
let token = tokenParts.joined()
print("Device Token: \(token)")
}
3).Add this also
3).也添加这个
UIApplication.sharedApplication().registerForRemoteNotifications()
回答by phnmnn
Do this:
做这个:
var notify : UIUserNotificationSettings = UIUserNotificationSettings(forTypes:UIUserNotificationType.Alert|UIUserNotificationType.Sound, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notify)
UIApplication.sharedApplication().registerForRemoteNotifications()
回答by Princes
Please make sure your certificates are valid and app is registered successfully,Do some Googling and u will sure find right way.This code worked for me.
请确保您的证书有效并且应用程序已成功注册,进行一些谷歌搜索,您一定会找到正确的方法。此代码对我有用。
(in AppDelegate.swift didFinishLaunchingWithOptions method )
(在 AppDelegate.swift didFinishLaunchingWithOptions 方法中)
func application(application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData){
//send this device token to server
println("\(deviceToken)")
}
// Add Delegate methods of UIApplication
// 添加 UIApplication 的委托方法
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
println(error)
}
//Called if unable to register for APNS.
//无法注册APNS时调用。
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
回答by Lucas Huang
If you get this message, then you have already registered and the method will get called only when the application gets launched or the token gets changed. It has slight chance that the token will be updated.
如果您收到此消息,则您已经注册,并且只有在启动应用程序或更改令牌时才会调用该方法。令牌更新的可能性很小。
Probably, you can try to delete the application from you test device and try to clear it from Xcode. And do it over again.
也许,您可以尝试从测试设备中删除该应用程序并尝试从 Xcode 中清除它。然后再做一遍。
回答by Sazzad Hissain Khan
Swift 5
斯威夫特 5
For Swift 5, I have used like this,
对于 Swift 5,我是这样使用的,
##代码##回答by Ely
When didRegisterForRemoteNotificationsWithDeviceToken
doesn't get called, you should always first reconnect to another Wi-Fi network, or toggle Flight modus twice, and restart your app, before you try anything else. Otherwise restarting your device may also help.
当didRegisterForRemoteNotificationsWithDeviceToken
没有被调用时,您应该始终首先重新连接到另一个 Wi-Fi 网络,或切换 Flight 模式两次,然后重新启动您的应用程序,然后再尝试其他任何操作。否则重新启动您的设备也可能有所帮助。