在 Swift 中注册远程通知时未收到设备令牌
Posted
技术标签:
【中文标题】在 Swift 中注册远程通知时未收到设备令牌【英文标题】:Device Token not received when registering for remote notifications in Swift 【发布时间】:2015-05-03 09:46:54 【问题描述】:注册远程通知时不知何故无法接收设备令牌。我收到模式说“你想让 App X 能够向你发送通知”,但是当我接受它时,不会调用 didRegisterForRemoteNotifications 函数。
当我使用此代码在 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()
这些功能根本没有被触发:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData!)
和
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError!)
但是当我记录这个时
println("current settings \(UIApplication.sharedApplication().currentUserNotificationSettings()) and \(UIApplication.sharedApplication().isRegisteredForRemoteNotifications())")
我收到了
"current settings <UIUserNotificationSettings: 0x170437120; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);> and true"
我的配置文件和证书都井井有条。
其他人有这个问题吗?
【问题讨论】:
为什么要给registerForRemoteNotifications()
打两次电话?
1) 创建推送通知并附加您的个人资料 2) 在功能推送通知中 3) 发送设备令牌查看更多信息以了解我的答案
【参考方案1】:
你可以试试这个
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)
编辑: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
编辑: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)
【讨论】:
还是不行!会不会跟证书有关? 是的,你需要做的,从头开始重新创建。 "***.com/questions/33970840/…" 你能找到这个问题的答案吗?我用你的答案试过了,但它不适用于 iPad swift 3 中设备令牌的问题 你在做什么与 OP 的代码不同?这样做有什么问题:UIApplication.sharedApplication()
?【参考方案2】:
对于 Swift 3.0,您可以使用:
let deviceTokenString = message.reduce("", $0 + String(format: "%02X", $1))
【讨论】:
【参考方案3】:对于 swift 4.0,请按照以下步骤操作:
1).关于功能中的推送通知(TARGET)
2). 在 didFinishLaunchingWithOptions 中使用此代码
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
3). 也添加这个
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)")
【讨论】:
【参考方案4】:这样做:
UIApplication.sharedApplication().registerForRemoteNotifications()
【讨论】:
【参考方案5】:请确保您的证书有效并且应用程序注册成功,做一些谷歌搜索,您一定会找到正确的方法。此代码对我有用。
(在 AppDelegate.swift didFinishLaunchingWithOptions 方法中)
var notify : UIUserNotificationSettings = UIUserNotificationSettings(forTypes:UIUserNotificationType.Alert|UIUserNotificationType.Sound, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notify)
UIApplication.sharedApplication().registerForRemoteNotifications()
//添加UIApplication的Delegate方法
func application(application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
//send this device token to server
println("\(deviceToken)")
//无法注册APNS时调用。
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError)
println(error)
【讨论】:
【参考方案6】:如果您收到此消息,则说明您已经注册,并且只有在启动应用程序或更改令牌时才会调用该方法。令牌更新的可能性很小。
也许,您可以尝试从您的测试设备中删除应用程序并尝试从 Xcode 中清除它。再做一遍。
【讨论】:
感谢您的回答,但我已经尝试过,并在多个设备上尝试过。【参考方案7】:斯威夫特 5
对于 Swift 5,我是这样使用的,
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
【讨论】:
【参考方案8】:当didRegisterForRemoteNotificationsWithDeviceToken
未被调用时,您应始终先重新连接到另一个 Wi-Fi 网络,或切换两次飞行模式,然后重新启动您的应用程序,然后再尝试其他任何操作。否则,重新启动您的设备也可能会有所帮助。
【讨论】:
以上是关于在 Swift 中注册远程通知时未收到设备令牌的主要内容,如果未能解决你的问题,请参考以下文章
我应该自己保存firebase令牌还是在注册远程通知时自动完成?