ios - 在 iOS 中检索到推送通知,但在 Firebase 中检索不到

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

我在申请中遇到了这个奇怪的问题。我正在尝试使用 firebase 在我的应用程序中集成推送通知。我使用下载的 SDK(不使用 pod)将 Firebase SDK 包含在项目中。我在应用程序中包含了下载的 zip 文件中的以下框架:

enter image description here

在 AppDelegate 中

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    return true
    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 called and the access token is received.
func application(_ application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    var token = ""
    for i in 0..<deviceToken.count {
        token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
    }
    print(token)
    Messaging.messaging().setAPNSToken(deviceToken, type: .sandbox)
}

// This is not getting called
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
    Messaging.messaging().subscribe(toTopic: "ios_topic")
}

我在 didRegisterForRemoteNotificationsWithDeviceToken 中获得的访问 token 是有效的,我使用它成功地向设备发送了推送。但是 FireBase 的 didRefreshRegistrationToken 没有被调用。我在 linkerflags 中添加了 -ObjC,在 info.plist 中添加了 FirebaseAppDelegateProxyEnabled 作为 NO。我在项目根目录中添加了 GoogleService-Info.plist 文件,并且该项目已在 FireBase 控制台中正确配置。

这里可能的问题是什么,如何解决?

最佳答案

这是遵循教程时犯的最愚蠢的错误。只需要将 Messaging.messaging() 的委托(delegate)设置为 AppDelegate。在 FirebaseApp.configure() 下添加以下代码,问题就解决了。

FirebaseApp.configure()
Messaging.messaging().delegate = self

关于ios - 在 iOS 中检索到推送通知,但在 Firebase 中检索不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45015929/

相关文章:

ios - Swift 将字符串转换为日期输出错误的日期

ios - OpenWeatherMap 中不同大小的天气图标

iOS Swift 3 "Login"应用程序 : Best Method for storing/passing a User

ios - 如何收到用户使用 ShareKit 分享内容的通知?

javascript - 错误: deadline-exceeded when working with firebase cloud functions

ios - 遍历Firebase中节点中的所有节点

swift3 - Swift 3 NSFileManager Xcode 8 错误 : Has been renamed FileManager

ios - 迦太基建立伞形框架

firebase - 是否可以获取添加前的ID?

android - Firebase 9.0.2 身份验证错误代码