Michael Johnston
Michael Johnston

Reputation: 858

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

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.

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.

Upvotes: 7

Views: 3060

Answers (2)

Ruben Martirosyan
Ruben Martirosyan

Reputation: 940

I think that this package will help.

Upvotes: 1

Ishay Peled
Ishay Peled

Reputation: 2868

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

Upvotes: 0

Related Questions