Is It Not Possible to Have a Code in the Background Who Will Be Called Every 24H

Is it not possible to have a code in the background who will be called every 24h?

Edit April 22, 2019:

Recently, Google Cloud released Cloud Scheduler, which allows you to schedule HTTP requests or Cloud Pub/Sub messages to functions that you deploy.

This new service works also very well with Firebase and for that I recommend you read an excellent article writen by Doug Stevenson on the Firebase blog named Scheduling Cloud Functions for Firebase (cron).


Is it not possible to have a code in the "background" who will be called every 24h?

Yes, it is possible. In this case, you should write a function in Cloud Functions for Firebase and call it whenever you needed. If you want to be triggred every 24 hours, use the follwing service:

  • https://cron-job.org/en/

This means that you can do that particular calculation even if the user has the app closed. For a code example, please see Frank van Puffelen's answer from the following post:

  • Cloud Functions for Firebase trigger on time?

Is it not possible to have a code in the background who will be called every 24h?

Edit April 22, 2019:

Recently, Google Cloud released Cloud Scheduler, which allows you to schedule HTTP requests or Cloud Pub/Sub messages to functions that you deploy.

This new service works also very well with Firebase and for that I recommend you read an excellent article writen by Doug Stevenson on the Firebase blog named Scheduling Cloud Functions for Firebase (cron).


Is it not possible to have a code in the "background" who will be called every 24h?

Yes, it is possible. In this case, you should write a function in Cloud Functions for Firebase and call it whenever you needed. If you want to be triggred every 24 hours, use the follwing service:

  • https://cron-job.org/en/

This means that you can do that particular calculation even if the user has the app closed. For a code example, please see Frank van Puffelen's answer from the following post:

  • Cloud Functions for Firebase trigger on time?

So am building an app and it has this feature that i dont know exactly how to go about it

While @Narendra_Nath's answer might work, please note that is not a bulletproof solution. Why? Because a SharedPreferences doesn't persist across app uninstalls. This means that your user can install and uninstall the app and see the page as much as they want. So if you indeed want a user to see a screen only once, then you should consider storing that data in a database. Please note that SQLite isn't also a solution because when a user uninstalls the app, everything that is stored locally is wiped out. So what's the solution?

The best way to solve this would be to store the data in the cloud, either in Cloud Firestore or in the Realtime Database. So you can set a boolean variable and always check against it.

If you however intend to implement Firebase Authentication, then another solution would be to display the screen when your users are authenticated for the first time. So even if they will try to sign in on another device, install and uninstall the app, they won't be able to see the screen again.

Regarding the second problem, you should consider using Cloud Function for Firebase. It's the most elegant solution. If you want to somehow schedule an operation, then you should consider using Cloud Scheduler, as explained in my answer in the following post:

  • Is it not possible to have a code in the background who will be called every 24h?

Firebase: Delete data after 60 seconds

Start the handler in your onCompleteListener. Then use the code to delete the specific node after 60 Seconds.

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
FirebaseDatabase.getInstance().getReference()
.child("node_name").child(keyval).removeValue()
}
}, 60000);

I want to make Background Service to add item to RecyclerView every 24 hours

Background service can not be used for long running tasks on Android 8.0 onwards. Please use JobScheduler API or Workmanager to achieve the desired results. Consult the following link for details.

https://developer.android.com/topic/libraries/architecture/workmanager



Related Topics



Leave a reply



Submit