【发布时间】:2021-04-15 16:01:15
【问题描述】:
我正在尝试按照 firebase 文档将推送通知合并到我的项目中。但是,我遇到了一个无法解决的错误。 “类型‘通知’没有成员‘名称’”。提前致谢!
import UIKit
import Firebase
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
registerForPushNotifications(application)
Messaging.messaging().delegate = self
Messaging.messaging().subscribe(toTopic: "relationship") { (error) in
print("subscribed to relationship topic..")
}
return true
}
func registerForPushNotifications(_ application: UIApplication){
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()
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
print("Firebase registration token: \(String(describing: fcmToken))")
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.
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("DEBUG: registered for notifications with device token: \(deviceToken)")
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.banner)
}
}
我在项目中安装的 pod:
- pod 'Firebase/Core'
- pod 'Firebase/Functions'
- pod 'Firebase/Analytics'
- pod 'Firebase/Auth'
- pod 'Firebase/Firestore'
- pod 'Firebase/Storage'
- pod 'Firebase/Messaging'
【问题讨论】:
-
签入您的项目。您已经创建了自己的通知类或结构。如果您发现了这个,请替换名称。
标签: ios swift firebase push-notification