flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

[firebase_messaging]: Data only message not delivered to firebase onMessage listener in Foreground

Open thiagommohallem opened this issue 3 years ago • 16 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues.

Are you aware of the differences between iOS and Android background message handling?

  • [X] I understand that iOS and Android background messages behave differently, and I've designed my application with that in mind.

Do you have an active Apple Developer account?

  • [X] I have an active Apple Developer account.

Are you using a physical iOS device to test background messages?

  • [X] I am using a physical iOS device to test background messages.

Have you enabled "Remote Notifications" & "Background Mode" (Checking options for "Background Processing" & "Remote Notifications") in your app's Xcode project?

Captura de Tela 2023-10-11 às 10 22 20

Have you created an APNs key in your Apple Developer account & uploaded this APNs key to your Firebase console?

Captura de Tela 2023-10-11 às 10 23 59

Have you disabled method swizzling for Firebase in your app?

Captura de Tela 2023-10-11 às 10 25 05

Are you sending messages to your app from the Firebase Admin SDK?

admin
  .messaging()
  .send(
    
    {
      token: token,
      data: {
        type:'example',
      }
    },
  false,
  )
  .then((res) => {
    if (res.failureCount) {
      console.log('Failed', res.results[0].error);
    } else {
      console.log('Success');
    }
  })
  .catch((err) => {
    console.log('Error:', err);
  });

Have you requested permission from the user to receive notifications?

  • [X] I have the relevant permission to receive notifications.

Have you used the 'Console' application on your macOS device to check if the iOS device's system is throttling your background messages?

padrão	10:30:55.722393-0300	SpringBoard	Received incoming message on topic br.com.pushapplication at priority 5
padrão	10:30:55.751090-0300	SpringBoard	[br.com.pushapplication] Received remote notification request B5D0-3548 [ waking: 0, hasAlertContent: 0, hasSound: 0 hasBadge: 0 hasContentAvailable: 1 hasMutableContent: 0 pushType: Background]
padrão	10:30:55.751188-0300	SpringBoard	[br.com.pushapplication] Process delivery of push notification B5D0-3548
padrão	10:30:55.751246-0300	SpringBoard	[br.com.pushapplication] Request DUET delivers content-available push notification to application
padrão	10:30:55.751513-0300	SpringBoard	SUBMITTING: com.apple.pushLaunch.br.com.pushapplication:F3EB4A
padrão	10:30:55.753089-0300	dasd	Submitted Activity: com.apple.pushLaunch.br.com.pushapplication:F3EB4A at priority 5 (Wed Oct 11 10:30:55 2023 - Thu Oct 12 10:30:55 2023)
padrão	10:30:55.761168-0300	dasd	com.apple.pushLaunch.br.com.pushapplication:F3EB4A:[
	{name: ApplicationPolicy, policyWeight: 50.000, response: {Decision: Absolutely Must Proceed, Score: 1.00, Rationale: [{[appIsForeground]: Required:1.00, Observed:1.00},]}}
 ], FinalDecision: Absolutely Must Proceed}

Additional context and comments

I'm using Flutter version 3.10.4 Firebase Messaging version: 14.6.2

iOS device: iPhone 8 iOS version: 16.3.1

I'm sending a data only message to my Flutter app running in foreground using the admin script above. However, It's not being processed by onMessage listener, on android everything is fine. The only thing that appears on my log is this:

Warning: Application delegate received call to -application:didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called.

The message works well if the 'notification' part is present, but I need the push to be silent. Any help?

Here's my AppDelegate.swift: Captura de Tela 2023-10-11 às 10 38 47

thiagommohallem avatar Oct 11 '23 13:10 thiagommohallem

Updates: Adding this lines to my AppDelegate.swift resolves the warning "Warning: Application delegate received call to -application:didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called.".

My data only message is delivered to this method. But the message is still not fowarded to firebase listeners, i feel like I'm missing something in the swift code. Captura de Tela 2023-10-11 às 14 43 02

thiagommohallem avatar Oct 11 '23 17:10 thiagommohallem

Thanks for the report @thiagommohallem Check this issue and see if it resembles your case and helps to resolve your case or not.

darshankawar avatar Oct 12 '23 10:10 darshankawar

Thank you for the reply @darshankawar, but I'm afraid not. I only see the problem with data-only notifications exclusively on my iOS device. I'm able to send a 'normal' notification and trigger the onMessage method as shown below: Captura de Tela 2023-10-12 às 08 08 14

Only the silent ones does not make through the listener, the only thing that prints out in the console is this: Captura de Tela 2023-10-12 às 08 08 56

On Android everything works perfectly.

thiagommohallem avatar Oct 12 '23 11:10 thiagommohallem

Thanks for the update @thiagommohallem You may check the plugin example and see its implementation and compare it with yours to check if any config is missing at your end or not.

darshankawar avatar Oct 13 '23 10:10 darshankawar

@darshankawar I've replaced my app code with the example you sent me. Same results. Data-only messages are not captured by the onMessage listener with my app on the foreground on an iOS device. The only differences that I saw in native config files was that my AppDelegate is in Swift, the example is in Objective-c and that in the info.plist, processing is enabled for the UIBackrgoundModes, tried that as well.

Still got the same warning: Warning: Application delegate received call to -application:didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called.

It seems to me that the silent-push method is not being handled natively for iOS

thiagommohallem avatar Oct 13 '23 14:10 thiagommohallem

Updates: I was able to override the didReceiveRemoteNotification method in my AppDelegate.swift adding this code to invoke onMessage for data-only a.k.a silent push's

override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    
        let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
        let channel = FlutterMethodChannel(name: "plugins.flutter.io/firebase_messaging", binaryMessenger: controller.binaryMessenger);
        
        let dictionary: NSDictionary = [
                    "messageId": "1697221363163183",
                    "data": [
                        "type":  "update-customer-wallet"
                    ],
                ]
        channel.invokeMethod("Messaging#onMessage", arguments: dictionary)
    }

Doing that, my push was received by the onMessage method as shown below: 1

I think the firebase_messaging plugin is failing to call the channel method, or the didReceiveRemoteNotification implementation is not being called on the plugin side at all.

thiagommohallem avatar Oct 13 '23 18:10 thiagommohallem

Thanks for the update @thiagommohallem Re: didReceiveRemoteNotification, check this issue with underlying comments for reference and see if they help in your case or not.

darshankawar avatar Oct 16 '23 11:10 darshankawar

@darshankawar I was able to make my own implementation for the didReceiveRemoteNotification method to handle data-only messages in iOS. I've tried the example code and got the same errors results, I'd be great if you or anyone in the team could try it to see if this data-only foreground in iOS is a bug in my project/device or in the plugin.

I've put breakpoints everywhere during the 'investigation' part of this bug. The firebase messaging plugin here was not picking up anything from silent push's in foreground. But was working properly for notifications with alerts.

thiagommohallem avatar Oct 17 '23 12:10 thiagommohallem

@thiagommohallem this works for ios data messaging (silent), the important thing is an option with content available

payload = { 'data': { "click_action": "FLUTTER_NOTIFICATION_CLICK", "sound": "default", "priority": "high", "myData" : "myData" }, fcm.sendToDevice(tokens, payload, {'content_available': true})

yostrokon avatar Oct 17 '23 19:10 yostrokon

@thiagommohallem Check above and see if it helps to resolve your case or not.

darshankawar avatar Oct 19 '23 10:10 darshankawar

@thiagommohallem this works for ios data messaging (silent), the important thing is an option with content available

payload = { 'data': { "click_action": "FLUTTER_NOTIFICATION_CLICK", "sound": "default", "priority": "high", "myData" : "myData" }, fcm.sendToDevice(tokens, payload, {'content_available': true})

Hi @yostrokon Everything is ok, We are sending notification using api post method. I can add click_action, sound, priority in data map but where to add content_available as true?

Is mydata key also needed?

msarkrish avatar Oct 20 '23 07:10 msarkrish

@msarkrish i'm using firebase function for sending push notification, and there is a third parameter for options, content_available true will trigger onMessage listener on IOS when notification missing in payload, mydata is an example key for your data to send with the data message,

yostrokon avatar Oct 20 '23 07:10 yostrokon

Ok @yostrokon . But still, we are facing that warning. listener is not getting called.

msarkrish avatar Oct 20 '23 11:10 msarkrish

Updates: I was able to override the didReceiveRemoteNotification method in my AppDelegate.swift adding this code to invoke onMessage for data-only a.k.a silent push's

override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    
        let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
        let channel = FlutterMethodChannel(name: "plugins.flutter.io/firebase_messaging", binaryMessenger: controller.binaryMessenger);
        
        let dictionary: NSDictionary = [
                    "messageId": "1697221363163183",
                    "data": [
                        "type":  "update-customer-wallet"
                    ],
                ]
        channel.invokeMethod("Messaging#onMessage", arguments: dictionary)
    }

Doing that, my push was received by the onMessage method as shown below: 1

I think the firebase_messaging plugin is failing to call the channel method, or the didReceiveRemoteNotification implementation is not being called on the plugin side at all.

Thank you @thiagommohallem . Now issue got resolved.

msarkrish avatar Oct 21 '23 10:10 msarkrish

I was using the official FCM plugin from NodeJS to send my notifications, tried content available inside aps property and it didn't work. Ended up doing my own implementation overriding the didReceiveRemoteNotification in AppDelegate.swift based on FLTFirebaseMessagingPlugin.m.

It could be something specific to my project @darshankawar. But apparently, @msarkrish was facing the same issue. It is working for me this way, of course it is preferable if Firebase handled it, but due to business demand, I had to make it work first. Don't think I will keep doing tests because it is 'fixed' for me. Any updates on this i will post here. I'll keep coming back to this post to see if there is any updates from others devs or from the firebase team

thiagommohallem avatar Oct 25 '23 12:10 thiagommohallem

Thanks for the updates. Based on above comment / solution, I'll close this as resolved. If we get more traction on such case, will consider reopening it. If you disagree, write in comments and I'll reopen it.

darshankawar avatar Oct 27 '23 07:10 darshankawar