【发布时间】:2019-05-16 01:35:04
【问题描述】:
我目前正在处理通知,但未能从 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
我已经从Background fetches 激活了Push Notifications 和Remote Notifications,并且已经阅读了stackoverflow 中的另一个主题,该主题目前与here 中的主题相似
我想与您分享我的 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)")
}
该函数在用于设置 apns 令牌的 Firebase 文档中。
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.
如果您能提供任何帮助,我将不胜感激,因为目前很难看出它有什么问题。
【问题讨论】:
-
你是说没有收到通知,还是说收到了通知,但没有数据?
-
感谢您的关注,由于没有收到日志,所以没有收到通知
-
通知和消息是有区别的。消息是基于数据的。通知出现在屏幕上。它们仅在应用程序处于后台或关闭时出现。当应用在前台时,它们不会出现。
-
是的,但我尝试了两种方式来查看当前发生的情况,即使在前台测试中,也没有书面消息。
-
@AakashDave 感谢您的回复,它现在运行良好。我将再次阅读文档。
标签: swift firebase apple-push-notifications firebase-cloud-messaging