firebase Swift didReceiveRemoteNotification()未被调用

hmmo2u0o  于 2023-01-14  发布在  Swift
关注(0)|答案(1)|浏览(136)

在广泛阅读了这方面的内容后,我找不到解决方案。我正在从Firebase向我的iOS应用程序发送云消息,但方法DidReceiveRemoteNotification()根本没有被调用,应用在前台和后台都没有。我试过从firebase控制台发送,也试过从postman using topics发送,都不行。我已经在应用中集成了Firebase,我把证书上传到Firebase,控制台没有任何错误,只是通知没有来?我还增加了云消息的功能。
这是我在appDelegate中所做的(来自Firebase教程):

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        FirebaseApp.configure()


        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()

        Messaging.messaging().delegate = self

        return true
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
      // If you are receiving a notification message while your app is in the background,
      // this callback will not be fired till the user taps on the notification launching the application.
      // TODO: Handle data of notification

      // With swizzling disabled you must let Messaging know about the message, for Analytics
      // Messaging.messaging().appDidReceiveMessage(userInfo)

      // Print message ID.
      if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
      }

      // Print full message.
      print(userInfo)
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
      // If you are receiving a notification message while your app is in the background,
      // this callback will not be fired till the user taps on the notification launching the application.
      // TODO: Handle data of notification

      // With swizzling disabled you must let Messaging know about the message, for Analytics
      // Messaging.messaging().appDidReceiveMessage(userInfo)

      // Print message ID.
      if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
      }

      // Print full message.
      print(userInfo)

      completionHandler(UIBackgroundFetchResult.newData)
    }
jv4diomz

jv4diomz1#

禁用方法混合,一切都应该正常工作。
根据文档https://firebase.google.com/docs/cloud-messaging/ios/client#token-swizzle-disabled
如果您已禁用方法混合,或者正在构建SwiftUI应用,则需要将APN令牌显式Map到FCM注册令牌。实现application(_:didRegisterForRemoteNotificationsWithDeviceToken:)方法以检索APN令牌,然后设置消息传递的apnsToken属性
这里还提到https://firebase.google.com/docs/cloud-messaging/ios/receive#handle_messages_with_method_swizzling_disabled
默认情况下,如果你将应用的应用委托类分配给UNUserNotificationCenter和Messaging委托属性,FCM将混合你的应用委托类,以自动将FCM令牌与设备的APN令牌关联,并将收到通知的事件传递给Analytics。如果你显式禁用方法混合,如果你正在构建SwiftUI应用,或者如果你为任一委托使用单独的类,您需要手动执行这两项任务。
MapapnToken和deviceToken将在不同的位置完成,因此建议完整阅读文档并应用必要的更改。

相关问题