我是一位有用的助手,可以为您进行翻译。以下是需要翻译的内容:
我目前正在处理通知问题,但是我无法从Firebase FCM获得任何通知。
Podfile配置如下:
target 'Project' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for Project
pod 'Firebase/Core'
pod 'Firebase/Messaging'
end
我已经启用了来自“后台获取”中的“推送通知”和“远程通知”,并且已经阅读了类似的另一个主题,位于stackoverflow中的这里。我想与您分享我的AppDelegate代码,但首先我必须说Google关于推送通知的文档似乎有点混乱,因为这里有很多重写的方法,而每个教程都有不同的方式来接收通知。 我已经导入了这些。
import Firebase
import FirebaseInstanceID
import UserNotifications
import FirebaseMessaging
接下来是代理
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate{
...
}
下面是willFinishLaunchWithOptions方法:
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self
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 })
// For iOS 10 data message (sent via FCM
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return true
}
以下是 Messaging
的委托函数。
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("MEssage -> \(remoteMessage.appData)")
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}
那个函数在Firebase文档中用于设置APNS令牌。
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken as Data
}
我已经从 FCM 发送了数百个通知,它在服务器端成功发送,但是当我记录收到的消息时,没有任何收入数据。
如果你问我为什么要在 willFinish
函数中实现配置,那是因为文档中有这样的说明。
For devices running iOS 10 and above, you must assign the
UNUserNotificationCenter's delegate property and FIRMessaging's
delegate property. For example, in an iOS app, assign it in the
applicationWillFinishLaunchingWithOptions: or
applicationDidFinishLaunchingWithOptions: method of the app delegate.
我希望你能帮我,因为目前很难看出问题所在。这涉及到IT技术方面的内容。