amplify-android icon indicating copy to clipboard operation
amplify-android copied to clipboard

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

Open iamAliRaza opened this issue 3 years ago • 5 comments

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, {},{},{},{})

iamAliRaza avatar Sep 07 '21 20:09 iamAliRaza

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.

raphkim avatar Sep 09 '21 20:09 raphkim

@raphkim Yeah actually i am talking about to observe specific type of model like i mentioned - Messages::java.class - and above this code Amplify.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...

iamAliRaza avatar Sep 10 '21 06:09 iamAliRaza

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.

raphkim avatar Sep 10 '21 20:09 raphkim

@raphkim ok thanks

iamAliRaza avatar Sep 10 '21 20:09 iamAliRaza

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

poojamat avatar Feb 11 '22 17:02 poojamat