调用didRegisterForRemoteNotificationsWithDeviceToken但未发送推送通知

时间:2017-07-11 02:40:35

标签: ios firebase swift3 apple-push-notifications firebase-cloud-messaging

我想在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在全新安装期间被调用两次,一次是在我授予权限之前,一次是在授予权限之后。

1 个答案:

答案 0 :(得分:0)

也许是因为设备令牌现在被解析为'32BYTES'。

在此处查看更多信息:

Swift 3 - device tokens are now being parsed as '32BYTES'

相关问题