![](/img/trans.png)
[英]How can I go about setting up push notifications using firebase cloud messaging and cloud functions?
[英]Setting up firebase notifications
因此,我一直致力於使用雲消息傳遞設置 firebase 通知,但遇到了問題。 我一直在學習本教程,所以如果我做錯了什么(希望我沒有做錯),請告訴我。 我也一直在看這個。
這是我的 AppDelegate 代碼,這是他們告訴我放置所有東西的地方。 我也做了證書的事情,希望它能正常工作:
import UIKit
import Firebase
import FirebaseMessaging
class AppDelegate: NSObject, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions
launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
FirebaseApp.configure()
// 2
FirebaseConfiguration.shared.setLoggerLevel(.min)
UNUserNotificationCenter.current().delegate = self
// 2
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions) { _, _ in }
// 3
application.registerForRemoteNotifications()
Messaging.messaging().delegate = self //Here is one of the problems
return true
}
}
// 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 application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler:
@escaping (UNNotificationPresentationOptions) -> Void
) {
completionHandler([[.banner, .sound]])
}
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
completionHandler()
}
}
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
Messaging.messaging().apnsToken = deviceToken //Here's the other problem
}
extension AppDelegate: MessagingDelegate {
func messaging(
_ messaging: Messaging,
didReceiveRegistrationToken fcmToken: String?
) {
let tokenDict = ["token": fcmToken ?? ""]
NotificationCenter.default.post(
name: Notification.Name("FCMToken"),
object: nil,
userInfo: tokenDict)
}
}
問題:
Type 'Messaging' has no member 'messaging'
您可能正在使用舊版本的 pod 或依賴項。
FIRMessaging.messaging().delegate = self
上面的代碼將為您工作。
選擇
如果您使用的是 pod,則可以使用以下命令進行更新。
pod update Firebase/Messaging
要不就
pod update
上面的命令將更新您的所有 pod,請小心。
好極了。 我找到了解決方案。 其實很簡單。 我不小心在我的名為 Messaging 的應用程序中有一個 swift 視圖,它丟棄了語法。
如果你遇到這個問題。 確保項目中沒有任何名稱名為 Messaging。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.