Flutter app, check daily at x time to see if I need to send local notification to user
Asked Answered
M

2

7

I am a little uncertain as to how I could go about doing this task and was hoping for some clarification.

The APP: It reminds people to water their plants and the user can specify how often they wish to do so.

The problem I am facing is how I can go about sending the local notifications to the user. Instead of setting up an individual notification for each new plant they have with a scheduled time to go off. I was hoping I could specify a time of the day (say 8:00 in the morning) where my app runs through all my plants and checks if any require watering today. If they do, it then tells the user through a local notification saying for instance "You have 5 plants to water today" and when they click on it they go through to the app which shows them what plants they are.

  • Should I be using the Alarm Manager api for a daily alert? But can this run a script to check plant data and then send a local notification.
  • Or is it best to just attach an individual local notification to each plant?

Now I am still a novice at Android/Flutter development and just a little unsure what the best practices are for this? Hope I was clear enough in what I said, happy to answer any further questions. Thanks in advance for any help.

Masha answered 18/12, 2019 at 13:13 Comment(1)
any updates on this?Benildis
N
1

I think that this package will help.

Neely answered 18/12, 2019 at 14:28 Comment(0)
P
0

It really depends if you need a specific reminder for each plant (e.g. plant1 should be watered at 16:30 and plant2 needs watering at 17:30) or the scenario you describe where all notifications arrive together is what you desire.

If you need a reminder per plant - the only way is to set an alarm per plant

If you can live with one reminder for all plants - you can set one alarm that goes through them all

Please see an excellent implementation example here

public class Water extends BroadcastReceiver 
{    
    @Override
    public void onReceive(Context context, Intent intent) 
    {   
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();

        // Check if there are plants need watering
        // Notify user

        wl.release();
    }

    public void setAlarm(Context context)
    {
        AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, Alarm.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute
    }
}

When you all setAlarm, the Android system will schedule a repeating alarm that wakes up your process at the onReceive function (by broadcasting an event to it).

Please keep in mind this is just the core, look at the link I provided for complete set up including required permissions

Phonology answered 18/12, 2019 at 13:20 Comment(5)
Hi Ishay, I am happy with their being only one reminder per day for all the plants. But I don't necessarily need the user to be notified every day as some days there may be 0 plants to water. Reading through that article does the Alarm Manager go off and run in the background without the user knowing its happening?Masha
The alarm manager will 'wake up' your application into the code section you write inside broadcast receiver in the intervals you configure. In other words, the user does not know an alarm is set and in fact your application does not need to be in foreground for this to work. I added what I think may solve your problem to the answerPhonology
The alarm manager works well to run a function. I'm having trouble however figuring out how to use plugins like SharedPreferences in the AlarmManager callback as says the plugin is missing.Masha
SharedPreferences shouldn't be a problem afaik, can you share your code?Phonology
As I am working in Flutter not core Android Java I don't think I can use the example codes you have provided but instead find some Flutter packages.Masha

© 2022 - 2024 — McMap. All rights reserved.