Flutter - FCM 远程/静默通知(应用在后台)

3
我有一个Flutter应用程序,需要接收FCM推送消息(静默/后台数据消息)。我没有使用Flutter的Firebase Cloud Messaging插件,因为它目前不支持后台模式。所以我按照Firebase针对IOS的文档将FCM IOS sdk集成到项目中。
我能够正常接收“通知”消息(即推送消息到达时向用户显示通知)。所以我认为我的设置(密钥/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)")
    }
}

有没有想法为什么代理无法接收数据消息???
附言:当然,我正在真实的IOS设备上进行测试。

1
嗨 @N0000B,你解决了问题吗? - undefined
嗨 @N0000B 你解决了吗?我也遇到同样的问题。 - undefined
1个回答

0
也许你没有在“Singing & Capabilities”中的后台模式中启用“远程通知”。enter image description here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接