ably / ably-flutter

A wrapper around our Cocoa and Java client library SDKs, providing iOS and Android support for those using Flutter and Dart.
https://ably.com/download
Apache License 2.0
60 stars 16 forks source link

setOnBackgroundMessage not getting along with async/await #496

Closed asoap closed 11 months ago

asoap commented 1 year ago

When getting a background notification while the app is in a dead state. I can see Ably launching the app and I see the background notification execute. But I'm having an odd issue where I want to do a few things. Like updating some counters in stored local storage and updating the app's badge icon. All of these which use asyn/await to know when to continue. I investigate the code execution on Android using logcat I see the app dying before the function completes.

I see the app waking, I see it login via our webserver getting it's ably token, connecting to ably, and then getting the background message. It then starts executing the background message only for it to stop when it gets to an await. Under my normal code it was kind of a mix on which await would cause it to stop. Some awaits would execute, others would end the function. So I created a test to confirm it wasn't me.

ably.Push.notificationEvents.setOnBackgroundMessage(StateTestNotification.backgroundMessageHandler);

class StateTestNotification {
  static Future<void> backgroundMessageHandler(ably.RemoteMessage message) async {
    print('---------------- Just received a background message, with:');
    print('RemoteMessage.Data: ${message.data.toString()}');
    bool delayResult = await Future.delayed(Duration(milliseconds: 1), () {
      return true;
    });
    print('delay result: $delayResult');
    print('at end of backgroundMessageHandler');
  }
}

When I look at the logs using logcat I see:

---------------- Just received a background message, with:
RemoteMessage.Data: {increment: {"mydata": "here"}}

The rest of the prints() are missing. It always dies when it sees a delay of one millisecond.
Is there any way around this? Am I doing something wrong?

sync-by-unito[bot] commented 1 year ago

➤ Automation for Jira commented:

The link to the corresponding Jira issue is https://ably.atlassian.net/browse/SDK-3946

asoap commented 11 months ago

I managed to fix this. I've created a pull request which you can find here: https://github.com/ably/ably-flutter/pull/498

stmoreau commented 11 months ago

Thank you for raising this and for sending over your PR @asoap. We'll get back to you on it very shortly 😄

ttypic commented 11 months ago

Fixed in https://github.com/ably/ably-flutter/pull/499