【发布时间】:2021-02-01 15:55:23
【问题描述】:
我有一个需要接收 FCM 推送消息(静默/后台数据消息)的 Flutter 应用。我没有使用 Flutter 的 Firebase Cloud Messaging 插件,因为它目前不支持后台模式。因此,我已根据 IOS 的 Firebase 文档将 FCM IOS sdk 集成到项目中。
我可以很好地接收“通知”消息(当推送消息到达时向用户显示通知)。所以我相信我的设置(keys/fcm)很好。
现在的问题是,当我发送数据消息(不希望应用向用户显示通知)时,什么也没有发生。
我的消息看起来像
{
"to": "fcm-token",
"content_available": true,
"apns-priority": 5,
"data": {
"some_key": "some_value"
}
}
我希望上面的数据消息会触发下面的委托方法(但它不会)。
override func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
我已尝试启用或禁用方法调配,但这也无济于事。
以下是我为 FCM 消息传递设置委托的方式
extension AppDelegate : MessagingDelegate {
override func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
print("didReceiveRemoteNotification")
completionHandler(.newData)
}
func initFirebase(_ application: UIApplication) {
FirebaseApp.configure()
Messaging.messaging().delegate = self
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
}
@available(iOS 10.0, *)
override func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
completionHandler([[.alert, .sound]])
}
@available(iOS 10.0, *)
override func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
print("withCompletionHandler")
completionHandler()
}
override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("didFailToRegisterForRemoteNotificationsWithError")
}
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
print("didRegisterForRemoteNotificationsWithDeviceToken")
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("didReceiveRegistrationToken")
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}
}
知道为什么没有为数据消息调用代表吗???
P.S: 当然我是在真正的 IOS 设备上测试
【问题讨论】:
-
嗨@N0000B,您的问题解决了吗?
-
嗨@N0000B 你解决了吗?我也是。
标签: ios firebase flutter firebase-cloud-messaging