iPhone 8 未收到 FCM 推送通知

时间:2021-02-05 12:42:50

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

我已经在我的 iOS SwiftUI 应用程序中实现了 FCM Cloud Messaging

我正在测试 2 台设备:iPhone 8 和 iPhone 7 Plus(均运行 iOS 14.3)

出于某种原因,只有 iPhone 7 Plus 会收到通知。我不知道为什么 iPhone 8 没有,因为所有条件都相同。我复制 didReceiveRegistrationToken 中的令牌并使用 FCM Cloud 消息传递测试仪表板对其进行测试:

New token: cXzJ3bpRm**********************************************************************

enter image description here

class AppDelegate: NSObject, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {
    

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        FirebaseConfiguration.shared.setLoggerLevel(.min)
        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()
        UNUserNotificationCenter.current().delegate = self
        Messaging.messaging().delegate = self
        Messaging.messaging().token { token, error in
           // Check for error. Otherwise do what you will with token here
            print("Remote token: \(token)")
        }
        print("Finished App Delegate initialisation")
        return true
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("Successfully registered for notifications!")
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Failed to register for notifications: \(error.localizedDescription)")
    }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
      print(userInfo)

      completionHandler(UIBackgroundFetchResult.newData)
    }
    
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
      let dataDict:[String: String] = ["token": fcmToken ?? ""]
      NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
      // TODO: If necessary send token to application server.
      // Note: This callback is fired at each app startup and whenever a new token is generated.
        guard let fcmToken = fcmToken else {
            return
        }
        print("New token: \(fcmToken)")
        // When fcmToken changes, save new token in user profile
        Database().updateUserProfile(fcmToken: fcmToken)
    }

}

@main
struct Quiz: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    @Environment(\.scenePhase) private var scenePhase
    @ObservedObject var sessionManager = SessionManager()
    @ObservedObject var gameManager = GameManager()
    @AppStorage("country") var country = ""

    init(){
        print("Finished Swift App initialisation")
    }
    
    var body: some Scene {
        WindowGroup {
            switch sessionManager.authState {
            case .session:
                Session()
                    .environmentObject(sessionManager)
                    .environmentObject(gameManager)
            default:
                Login()
                    .environmentObject(sessionManager)
                    .onAppear {
                        // If email verified and exists, proceed to login
                        let verified = Auth.auth().currentUser?.isEmailVerified ?? false
                        if verified && Auth.auth().currentUser?.email != nil && self.country.count > 1 {
                            sessionManager.authState = .session
                        }
                    }
            }
        }
        .onChange(of: scenePhase) { (newScenePhase) in
            switch newScenePhase {
            case .active:
                // Load game if gameId exists in UserDefaults
                if self.gameManager.gameId.count > 0 {
                    self.gameManager.loadGame(gameId: self.gameManager.gameId)
                }
            case .inactive:
                print("")
            case .background:
                print("")
            @unknown default:
                print("")
            }
        }
    }
    
}

签名和功能: enter image description here

有人能解释为什么我的 iPhone 8 没有收到通知吗?

编辑:是的,iPhone 8 已接受来自应用程序的通知。

0 个答案:

没有答案