Notify user of upcoming dates

I am currently working on an Android application that uses several Firebase features. In the Realtime database, I have a date (the date when the book is required), and I need to notify the user when this date is close (say, 1 day before the date in the database). The Firebase cloud features don't seem to have a specific trigger for this, since nothing in the database changes.

I saw this thread that can set an alarm / notification for a future date, but I do not know how to stop it after setting it; I need to cancel the notification if they return the book. This causes a change in the database, so I would have an event to undo it, if that were possible.

Is this the best way to do this, or is there a way to implement it with Firebase? And if that's the way, how do I cancel a scheduled notification?

+5
source share
4 answers

Based on the message delivered by Reddit, use the Job Scheduling tool, for example JobScheduler (API 21+), Firebase JobDispatcher (API 9+) or Evernote Android Job (API 14+ but Google Play is not required).

Since your Firebase Realtime DB is available locally, you have the date (and possibly the time) when you want to display the notification. One of the task planners can simply run a task that displays a notification that no internet or server is required.

In addition, it is recommended to plan the work, because with Android Oreo and higher (26+) background tasks have more restrictions when executed.

+8
source

If you are comfortable using Python, read this post →

How to Schedule (Cron) Cloud Functions Jobs for Firebase to Create Firebase Cloud Functions

My suggestion was to use the daily-tick function to scan your Firebase real-time database to retrieve all books that are "due to the date of the next day" and give them notifications.

It should meet your requirement in a fairly simple way. Alternatively, you can try another method suggested in the Firebase video here . Temporary cloud features for Firebase using HTTP Trigger and Cron - Firecasts

Let me know if none of them meets your requirements. And what I miss. I will be happy to recommend the best alternative based on your feedback.

+1
source

You can easily cancel the alarm using alarmManager.cancel(pendingIntent) . Note that you must provide the same PendingIntent . For more information, see How to Cancel an Alarm from AlarmManager . I think this will solve your problem.

0
source

I have a similar use case, and in my code all triggers are executed on the device only after updating the data snapshot. I solved it as follows:

  • I have a listener for a database snapshot .addSnapshotListener(new EventListener<QuerySnapshot>()
  • After the records were restored, I set an alarm for the required dates.
  • If the recording was changed (returned in your case), the same snapshot receiver starts again, iterates through the recordings and cancels the alarms.

According to your core business or service, there might be something like this:

 db.collection("items").addSnapshotListener(new EventListener<QuerySnapshot>() { @Override public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) { if(e==null) { ArrayList<Item> itemsList = new ArrayList<>(); itemsList.addAll(documentSnapshots.toObjects(Item.class)); for(Item item:itemsList){ if(item.isReturned()){ cancelAlarm(item); } else { setAlarm(item); } } } else { // error handling } } }); Intent intent; PendingIntent pendingIntent; final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); public void setAlarm(Item item){ intent = new Intent(context, ItemBroadcastReceiver.class).putExtra("ID", item.getUuid()); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, item.getUniqueIntegerCode(), intent, 0); am.set(AlarmManager.RTC_WAKEUP, item.getDateTimeDue().minusDays(1), pendingIntent); } public void cancelAlarm(Item item) { if(intent != null && pendingIntent != null){ am.cancel(pendingIntent); /// if item needs to be saved, add your firestore set() routine } } 

And your broadcast receiver that responds to the trigger:

 public class ItemBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { context.startActivity(intent); // example, add necessary reaction } } 
0
source

Source: https://habr.com/ru/post/1273300/


All Articles