adaptyteam / AdaptySDK-Flutter

SDK for growing mobile in-app purchases
https://docs.adapty.io/docs/quickstart
MIT License
88 stars 15 forks source link

Listening to profile status #96

Closed tiyberius closed 10 months ago

tiyberius commented 10 months ago

I am using Riverpod package and a "subscription status" provider to store the state of a user's subscription for my app. I've got subscriptions working to the extent that after making a purchase I can update the state of the provider, but I'm trying to figure out the best way to keep the state of the user's subscription updated (in particular, when said subscription might have expired).

I tried following the setup instructions https://docs.adapty.io/docs/subscription-status#listening-for-subscription-status-updates, but I could not get the code I put in the listener to fire. I tried calling

Adapty().didUpdateProfileStream.listen((profile) {
  // handle any changes to subscription state
  debugPrint('hello from profile stream listener');
});

in the initialization code for my app, but could not get it to execute, I never saw 'hello from profile stream listener' in the logs.

As a workaround, I could set up this code

try {
  final profile = await Adapty().getProfile();
  // check the access
} on AdaptyError catch (adaptyError) {
  // handle the error
} catch (e) {
}

on a 60 second or 10 minute timer, but that's a hack and I wanted to see if there was a better way, and not send any more traffic to the Adapty API's than you're already getting.

I noticed in my app logs that Adapty is polling for profile updates every 60 seconds. With log level verbose I am seeing the following in my logs:

Adapty v2.6.3] - VERBOSE: LifecycleManager: syncProfile Begin
[Adapty v2.6.3] - VERBOSE: Calling now: getProfile(_:) [<REDACTED>].
[Adapty v2.6.3] - VERBOSE: #API# GET --> /sdk/analytics/profiles/<REDACTED>/ [Oa3fIX]
----------REQUEST START----------
$ curl -v -X GET -H "adapty-sdk-device-id:<REDACTED>" -H "adapty-sdk-previous-response-hash:<REDACTED>" -H "adapty-sdk-profile-id:<REDACTED>" -H "adapty-sdk-crossplatform-version:2.6.2" -H "adapty-sdk-observer-mode-enabled:false" -H "Authorization:<REDACTED>" -H "adapty-sdk-crossplatform-name:flutter" -H "adapty-sdk-sandbox-mode-enabled:true" -H "adapty-sdk-storekit2-enabled:disabled" -H "adapty-app-version:0.1.0" -H "adapty-sdk-version:2.6.3" -H "adapty-sdk-platform:iOS" -H "adapty-sdk-session:<REDACTED>" "https://api.adapty.io/api/v1/sdk/analytics/profiles/<REDACTED>/"
----------REQUEST END------------
[Adapty v2.6.3] - VERBOSE: #API# RESPONSE <-- GET /sdk/analytics/profiles/<REDACTED>/ [<REDACTED>]
----------RESPONSE START----------
HTTP 200 -H "cf-apo-via: origin,host" -H "Vary: Origin, Accept-Encoding" -H "cf-ray: <REDACTED>" -H "request-id: <REDACTED>" -H "referrer-policy: same-origin" -H "x-content-type-options: nosniff" -H "cf-cache-status: DYNAMIC" -H "x-response-hash: <REDACTED>" -H "Date: Thu, 21 Sep 2023 01:36:21 GMT" -H "Content-Length: 11" -H "allow: GET, POST, PATCH, HEAD, OPTIONS" -H "Content-Type: application/vnd.api+json" -H "Server: cloudflare" -d '{"data":{}}'
----------RESPONSE END------------
[Adapty v2.6.3] - VERBOSE: Completed getProfile(_:) [<REDACTED>]  is successful.
[Adapty v2.6.3] - VERBOSE: LifecycleManager: syncProfile Done
[Adapty v2.6.3] - VERBOSE: LifecycleManager: scheduleProfileUpdate after 60.0 sec.

And I'd like to hook into that! The documentation on https://docs.adapty.io/docs/subscription-status#listening-for-subscription-status-updates states that

"Whenever the user's subscription changes, Adapty will fire an event. In order to receive messages from Adapty, you need to make some additional configuration (depends on your platform):"

but makes no mention of what additional configuration is necessary.

x401om commented 10 months ago

Hi @tiyberius!

Adapty SDK is indeed pulling profile updates every 60 seconds, and if there are some changes in the user profile, didUpdateProfileStream should emit an event. It should also emit every time the SDK receives the new profile (successful purchase, restore, etc.), but it won't emit anything until the profile is updated.

I am trying to understand if these scenarios work for you, or if you are not receiving updates even when you change a user's profile by making a purchase or by updating the profile within the dashboard?

tiyberius commented 10 months ago

Thanks for the prompt response, @x401om! I am not receiving updates when a user's profile changes, for instance when they make a purchase. I'm on the free plan at the moment, so I am unable to update the profile through the dashboard, but I guess I could call the API to update the user profile and test it that way. However, like I said I am never seeing the result of "debugPrint('hello from profile stream listener');" in the logs, and so it seems like the listener isn't working for me. I am testing it by making a purchase.

Is there a particular place one needs to put this snippet? Maybe I don't have it in the right place. I am calling it from the main() method.

Adapty().didUpdateProfileStream.listen((profile) {
  // handle any changes to subscription state
});

The documentation makes reference to "additional configuration (depends on your platform)", but doesn't specify any "additional configuration" for a Flutter app or any other type of app for that matter.

x401om commented 10 months ago

@tiyberius, thank you! Now I understand. I will investigate the issue and get back to you soon.

x401om commented 10 months ago

Hey, @tiyberius! I've just checked the behavior of didUpdateProfileStream using our example app, and it looks like everything works as expected. I did receive an event during the startup and after making the purchase as well. Could you please check this behavior on our example app using your API key and bundle ID? Here is the line.

tiyberius commented 10 months ago

Okay! Thanks @x401om for confirming. I will try the example app with my credentials and see if the behavior persists. If I run into trouble I'll re-open this case. Thanks!

IliaKhuzhakhmetov commented 3 months ago

I have the same situation. Updating flutter adapty to 2.10.1 didn't help. Tried updating xCode to 15.3 - didn't help. Also updated some libraries (firebase core) and increased the target IOS version to 12.2.

When I make a purchase, I don't see any profile updates after a successful purchase. @tiyberius Have you solved your problem or not?

I too had this problem on the same version: 2.6.3

tiyberius commented 3 months ago

Hey @IliaKhuzhakhmetov, unfortunately I never got the code snippet working. I'm resorting to polling every few minutes or so to update the subscription status

IliaKhuzhakhmetov commented 3 months ago

Thanks @tiyberius for the reply. What helped me is that I switched to RevenueCat and RevenueCat works well in the same situation. Also, if the user has successfully subscribed, the profile that comes back after the purchase call does not contain any active subscriptions. After another refusal from Apple to publish my app with Adapty, they have appruved a version with RevenueCat