gpt4 book ai didi

ios - Firebase 云消息传递 AppDelegate 错误

转载 作者:IT王子 更新时间:2023-10-29 05:12:44 38 4
gpt4 key购买 nike

这是 Firebase 文档中的代码。

if #available(iOS 10.0, *) {
let authOptions : UNAuthorizationOptions = [.Alert, .Badge, .Sound]
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
authOptions,
completionHandler: {_,_ in })

// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.currentNotificationCenter().delegate = self
// For iOS 10 data message (sent via FCM)
FIRMessaging.messaging().remoteMessageDelegate = self

} else {
let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
}

我没有修改任何代码行。但是 Xcode 说“使用未声明的类型 UNAuthorizationOptionsUNUserNotificationCenterFIRMessagingDelegate

我还有一行。

NotificationCenter.defaultCenter.addObserver(self,selector: #selector(self.tokenRefreshNotification),name: kFIRInstanceIDTokenRefreshNotification,object: nil)

它说“类型 AppDelegate 的值没有成员 tokenRefreshNotification

我刚刚从 firebase 文档中复制并粘贴了我的代码,但出现错误!

最佳答案

Firebase 文档中的示例已过时。这是最新的 Xcode 9 和 Swift 3 的代码:

import Firebase
import UserNotifications

class AppDelegate: UIResponder, UIApplicationDelegate {


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


FIRApp.configure()

if #available(iOS 10.0, *) {
let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_,_ in })

// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// For iOS 10 data message (sent via FCM)
Messaging.messaging().delegate = self
}

application.registerForRemoteNotifications()

return true
}
}


@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
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")

// Print full message.
print("%@", userInfo)

}

}

extension AppDelegate : MessagingDelegate {
// Receive data message on iOS 10 devices.
func applicationReceivedRemoteMessage(_ remoteMessage: MessagingRemoteMessage) {
print("%@", remoteMessage.appData)
}
}

关于ios - Firebase 云消息传递 AppDelegate 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39854929/

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