Search code examples
androidnotificationsfiremonkeydelphi-10-seattle

How to fire notification using TimeEdit


im trying to fire a notification in specific time using TimeEdit, it didnt work! the code i used..

try
MyNot.Name := Edit1.Text;
MyNot.AlertBody := Edit2.Text;
MyNot.FireDate := Now + TimeEdit1.Time;
NotificationCenter1.ScheduleNotification(MyNot);
Finally
MyNot.DisposeOf;

im using Delphi10 Seattle Update1.


Solution

  • Now() returns a TDateTime that represents the current clock date/time. You are then adding the user's entered time relative to the current date/time. For example, if the user enters 00:05:00, you will be adding 5 minutes to the current date/time.

    If you want the notification to fire at a specific time of the current date, use the Date() function instead of the Now() function so that you are adding the entered time relative to midnight (00:00:00am):

    MyNot.FireDate := Date + TimeEdit1.Time;
    

    Alternatively, you can use the SysUtils.ReplaceTime() function instead:

    var
      dt: TDateTime;
    
    dt := Date;
    ReplaceTime(dt, TimeEdit1.Time);
    MyNot.FireDate := dt;
    

    This has the additional benefit that you can then configure whatever date you want, such as from the SysUtils.EncodeDate() function:

    var
      wYear, wMonth, wDay: Word;
      dt: TDateTime;
    
    wYear := ...;
    wMonth := ...;
    wDay := ...;
    dt := EncodeDate(wYear, wMonth, wDay) + TimeEdit1.Time;
    MyNot.FireDate := dt;
    

    Or:

    var
      wYear, wMonth, wDay: Word;
      dt: TDateTime;
    
    wYear := ...;
    wMonth := ...;
    wDay := ...;
    dt := EncodeDate(wYear, wMonth, wDay);
    ReplaceTime(dt, TimeEdit1.Time);
    MyNot.FireDate := dt;