gpt4 book ai didi

使用 FirebaseApp.configure() 时 iOS 应用崩溃

转载 作者:行者123 更新时间:2023-11-28 13:31:49 27 4
gpt4 key购买 nike

我想在我的框架中使用 Firebase 云消息传递。但是我在我的代码中使用 FirebaseApp.configure 的地方,除了这个错误消息之外什么都没有崩溃:

Message from debugger: Terminated due to signal 9

有人知道发生了什么事吗?

我在 firebase 中创建了项目和应用程序,并将 GoogleService-Info.plist 添加到我的应用程序中。我可以收到推送通知抛出 APN。但我无法在 FCM 中注册。

这是 AppDelegate.swift 中的相关代码:

import MyFramework

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
MyFramework.shared.start()

return true
}

这是我框架中的代码:

import UIKit
import UserNotifications
import FirebaseCore

public class MyFramework: NSObject {
public static let shared = MyFramework()

override private init() {}

public func start() {
UNUserNotificationCenter.current().delegate = self
UIApplication.shared.registerForRemoteNotifications()

let authorizationOptions: UNAuthorizationOptions = [.alert, .sound, .badge]
UNUserNotificationCenter.current().requestAuthorization(options: authorizationOptions, completionHandler: { (granted, error) in
if let _error = error {
print(_error.localizedDescription)
}

guard granted else {
return
}
})

FirebaseApp.configure()
}
}

最佳答案

AppDelegate 中使用此示例代码,如果解决问题则将代码传输到自定义类。

import UIKit
import Firebase
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

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

FirebaseApp.configure()

// [START set_messaging_delegate]
Messaging.messaging().delegate = self
// [END set_messaging_delegate]

registerForPushNotifications()

return true
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

application.applicationIconBadgeNumber = 0
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken as Data
}

func registerForPushNotifications() {

// iOS 10 support
if #available(iOS 10, *) {

UNUserNotificationCenter.current().delegate = self

UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in
// Enable or disable features based on authorization.

print("Permission granted: \(granted)")
guard granted else { return }
}
UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in

switch setttings.soundSetting{
case .enabled:

print("enabled sound setting")

case .disabled:

print("setting has been disabled")

case .notSupported:
print("something vital went wrong here")
}
}

UIApplication.shared.registerForRemoteNotifications()
}
// iOS 9 support
else if #available(iOS 9, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
// iOS 8 support
else if #available(iOS 8, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
// iOS 7 support
else {
UIApplication.shared.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
}
}
}

// [START ios_10_message_handling]
@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

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

// Print full message.
print(userInfo)

// Change this to your preferred presentation option
completionHandler([])
}

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

completionHandler()
}
}
// [END ios_10_message_handling]


extension AppDelegate : MessagingDelegate {
// [START refresh_token]
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")

let dataDict:[String: String] = ["token": fcmToken]

print(dataDict)

// 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.
}
// [END refresh_token]

// [START ios_10_data_message]
// Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
// To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true.
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}
// [END ios_10_data_message]
}

这段代码对我来说是正确的。如果不工作检查其他的东西!

关于使用 FirebaseApp.configure() 时 iOS 应用崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57240332/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com