我正在努力在iOS中实现推送通知。我已经成功地让它工作了一次。但现在我面临着一个奇怪的问题。我已经退出XCode并重新打开它,没有更改任何代码,我仍然在获取deviceToken,但现在我的设备上没有收到任何推送通知。我也在使用Firebase来获取通知,它也工作正常,但现在它给了我空白字符串作为设备令牌。
在AppDelegate中,我使用以下代码:
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
//This is triggered and I am getting the push notification token but using this instead of firebase token to send directly via APNS is also not working.
public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().setAPNSToken(deviceToken, type: .sandbox)
}
//This is triggered but the token here is a blank string
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
Messaging.messaging().subscribe(toTopic: "ios_topic")
}
这一直有效,直到我退出 XCode,但突然它停止工作,没有任何代码更改。我尝试了所有方法,包括系统重新启动。有时,didRegisterForRemoteNotificationsWithDeviceToken
在全新安装期间被调用两次,一次是在我授予权限之前,一次是在授予权限之后。
也许是因为设备令牌现在被解析为"32BYTES"。
在此处查看更多内容:
Swift 3 - 设备令牌现在被解析为"32BYTES">