Flutter-FCM远程/静音通知(后台应用程序)

时间:2020-10-18 19:58:21

标签: ios firebase flutter firebase-cloud-messaging

我有一个Flutter应用程序,需要接收FCM推送消息(静默/后台数据消息)。我没有将Flutter的插件用于Firebase Cloud Messaging,因为它当前不支持后台模式。因此,根据IOS的Firebase文档,我将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)")
    }
}

有什么想法为什么不向代表发送数据消息?

P.S:当然,我正在真实的IOS设备上进行测试

1 个答案:

答案 0 :(得分:0)

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