推送通知与firebase和swift ui不工作

brtdzjyr  于 2023-11-21  发布在  Swift
关注(0)|答案(1)|浏览(158)

所以我经历了所有的步骤,以实现推送通知使用FCM,但由于某种原因,我不知道为什么它不会工作。我发誓它是工作昨天,但由于某种原因,今天的通知不会通过。此外,我有我的APN认证密钥上传到firebase以及。这是我使用的代码用途:

import SwiftUI
import FirebaseCore
import FirebaseAuth
import Firebase 
import FirebaseMessagingInterop
import FirebaseMessaging
import UserNotifications


class AppDelegate: NSObject, UIApplicationDelegate {
  let gcmMessageIDKey = "gcm.message_id"

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
      FirebaseApp.configure()

      Messaging.messaging().delegate = self

      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()
      return true
  }

  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                   fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

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

    print(userInfo)

    completionHandler(UIBackgroundFetchResult.newData)
  }
}

@main

struct YourApp: App {
// register app delegate for Firebase setup
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

var body: some Scene {
  WindowGroup {
    NavigationView {
        
       ContentView()
            
    
    }
  }
}
}


extension AppDelegate: MessagingDelegate {
  func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {

    let deviceToken:[String: String] = ["token": fcmToken ?? ""]
      print("Device token: ", deviceToken) // This token can be used for testing notifications on FCM
  }
}

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
  withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  let userInfo = notification.request.content.userInfo

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

  print(userInfo)

  // Change this to your preferred presentation option
  completionHandler([[.banner, .badge, .sound]])
}

  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

  }

  func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {

  }

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
  let userInfo = response.notification.request.content.userInfo

  if let messageID = userInfo[gcmMessageIDKey] {
    print("Message ID from userNotificationCenter didReceive: \(messageID)")
  }

  print(userInfo)

  completionHandler()
}
}

字符串

c0vxltue

c0vxltue1#

在你的Info.plist中,将FirebaseAppDelegateProxyEnabled设置为“NO”。我相信这个调整会对你有所帮助。

相关问题