aws-amplify / amplify-android

The fastest and easiest way to use AWS from your Android app.
https://docs.amplify.aws/lib/q/platform/android/
Apache License 2.0
239 stars 112 forks source link

How to observe Datastore Model Realtime on background or after killing application #1488

Open iamAliRaza opened 2 years ago

iamAliRaza commented 2 years ago

Hey guyz!! I want to observe my model if the application is in background or if user killed the application from recent apps. In that case please let me know the best approach for listening to datastore events.

I Tried some ways but due to background restrictions in android none of the method is working correctly..

Any solution??

Amplify.Datastore.observe(Messages::java.class, {},{},{},{})

raphkim commented 2 years ago

What type of datastore events are you hoping to listen to?

If you wish to subscribe to operations involving a specific type of model (e.g. new Messages was created, or an instance of Messages was edited, etc.), then you are using the correct API. However, you do not seem to be providing any callback for the events, which basically means that your code is completely ignoring any changes being made to Messages. Please refer to our documentation for observing on DataStore models with callbacks here.

If what you wish to do is to subscribe to generic DataStore events (e.g. DataStore is now finished syncing remote models locally), then you can use our Hub category to subscribe to those events and act on them.

iamAliRaza commented 2 years ago

@raphkim Yeah actually i am talking about to observe specific type of model like i mentioned - Messages::java.class - and above this codeAmplify.Datastore.observe(Messages::java.class, {},{},{},{}) i intentionally left it blank. Just for showing that my question is related to Observe Specfic Model. Its working fine if i am observing it in MainActivity or anywhere when i am using the application. My problem is i implemented chat. So if the user is not using the application and he killed the application from recent apps after using it. Then how we can observe model. i want to get new messages in background like Whatsapp etc... Is there any best approach how can i observe model mutations in background. Because of background restrictions in android nothing is correctly working : Services, Background tasks etc...

raphkim commented 2 years ago

It looks like I misunderstood the original question! I apologize for that.

Amplify Android currently does not support realtime observation while the app is in the background. We will track it as a feature request and prioritize it based on how much traction it receives.

iamAliRaza commented 2 years ago

@raphkim ok thanks

poojamat commented 2 years ago

Push notification to achieve your use case will be better strategy.