carverauto / flutter

🚨 ChaseApp opensource mobile app - follow live police chases 🚔
https://chaseapp.tv
MIT License
3 stars 0 forks source link

Background message/notification handler never invoked on iOS #137

Open rutvik110 opened 2 years ago

rutvik110 commented 2 years ago

Description : When app is open from background by tapping on notification, the notification is not being recieved within the app.

Logs : 2020-10-01 15:58:24.788526+0100 Runner[972:925222] Warning: Application delegate received call to -application:didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called.

This is something which is occuring just now. I believe prev it was working fine. Something probably related to this

https://github.com/FirebaseExtended/flutterfire/issues/6290

https://github.com/FirebaseExtended/flutterfire/issues/3735

mfreeman451 commented 2 years ago
In our case, we have been able to make onBackgroundMessage work on iOS even if the app is terminated. However, this is not the case if the device is in low power mode / has low battery / is locked.

Our current workflow that works only when the device is NOT in low power mode is:

App is in terminated state.
Our backend sends first a data-only message with 'apns-push-type': 'background', 'apns-priority': '5', contentAvailable: true
Device receives the silent data-only message and "awakes" the app. It still doesn't appear in the current apps opened, so it's in terminated state.
Our backend sends a few seconds later our notification+data message, which is now handled by the onBackgroundMessage handler, since the app was awaken previously.
If the device is in low power mode, the step 4 is not handled even when the app has been awaken. We know that the app has been awaken because when we click on the app icon, the splash screen is not loaded, and instead the home screen is already loaded.

However, if we send another notification AFTER the app has missed the previous notification+data message, this one is correctly handled by the onBackgroundMessage. This is what is really strange to me. The handler probably gets awaken aswell when the first notification+data message arrives to the system notification center? Making the handler to work in next messages?

TLDR; If the iOS device is in low power mode and the app is still in terminated state, the onBackgroundMessage is not called for the first notification+data message even when the app has been previously awaken by a data-only message. However, the second notification+data message will trigger the onBackgroundMessage properly
rutvik110 commented 2 years ago

https://github.com/FirebaseExtended/flutterfire/issues/3395

rutvik110 commented 2 years ago

https://github.com/pusher/push-notifications-flutter/blob/49bf20438d2620709641f12dd2ae728da5a85a61/packages/pusher_beams_ios/ios/Classes/SwiftPusherBeamsPlugin.swift#L134 ![Uploading Screenshot 2022-03-19 at 12.20.26 PM.png…]() Is this issue due the pusher beams? Need to dig a little deeper!

rutvik110 commented 2 years ago

@mfreeman451 Damnn! It's the pusher as I suspected! The handlers from firebase are overridden by the pusher handlers as they should be but they don't have that notification handler implemented yet! :( I tested by removing the pusher beams package and sent notifications through firebase. Those notifications are handled properly in all the app states!

rutvik110 commented 2 years ago

What are the possible things we can do now to get around this?

Some of my opinions :

  1. Move from pusher beams to firebase messaging for sending and handling of notifications. I don't think this would have much big of an impact rn as the migration should be easy to go with.
  2. If for any reason we still want to continue with pusher beams, we can either wait for them to update this which I think won't happen anytime soon seeing the activity on their git repo. Secondly, we could help update that in but that would take our time and efforts and we would have to maintain that specific part.

Wdyt?

mfreeman451 commented 2 years ago

Can you explain what is happening or missing / broken on the pusher side please?

mfreeman451 commented 2 years ago

    public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        // Handle the user interaction with the notification
        // Not Implemented yet
    }

is this it?

rutvik110 commented 2 years ago

yah, I think that's it.

rutvik110 commented 2 years ago

I didn't look into how it's being communicated to the top level but I'm sure that's the missing bit!

rutvik110 commented 2 years ago

Filed an issue in pusher beams for this. https://github.com/pusher/push-notifications-flutter/issues/23#issue-1176707952