![](/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.