Home > Software design >  How to receive data from an api without any user interaction
How to receive data from an api without any user interaction

Time:09-29

I'm trying to receive some data from an API call and do some task based on the received data. Let's say I have my flutter app opened and in that opened state without any click or any interaction I get some data or do any function when I send something through the API.

I used Firebase FCM to do such task - When I send something through the API the firebase notification captures that data and doing the tasks I want on the flutter app.

But I want to do without Firebase. How can I do that. Any solution/suggestions would be great. Thanks!

CodePudding user response:

Your question is little bit unclear. Whatever if you want to do some network call even without any interaction by user you can call network in your onInit method. This method runs at the very beginning for individual screen. Then do your task based on the response

CodePudding user response:

You need stream API for this,look into socket programming

CodePudding user response:

You can use webs socket or pusher for this. When web socket is connected with your server that time you received some data and you can do your task.

CodePudding user response:

At first, let's put Flutter aside. Your problem is to pull data from API and do a specific task OR your API should notify the app through API.

For the first problem, a simple data polling should work: when the app is opened, you just start some kind of timer and periodically pull data from the API and do something. However, this method is not perfect, it drains the battery and in general, there are better ways to achieve the same thing, for instance using sockets or push notifications (as some of the answers already state that).

For your second problem - "or do any function when I send something through the API" - you need some kind of mechanism to notify your app for it to react and process the data. This one could be handled using the same mechanism as for the first problem.

So, probably you need to use some kind of push notification service (for instance OneSignal or Pusher, or any other similar service) or web sockets (https://flutter.dev/docs/cookbook/networking/web-sockets).

  • Related