Home > Enterprise >  Send notifications when the app is not used?
Send notifications when the app is not used?

Time:04-26

A client of mine had the question of whether it is possible to send a push notification in the employee app if they have not entered their hours by a certain time.

Is it possible to create a service for Android and iOS apps that checks every hour if the hours of the day have been entered in the database, and if not then send a push notification?

I don't know where to start, and if it is possible. But if other apps can do it, it should be possible with this app as well.

CodePudding user response:

As @SilverWarior Suggests I have created two pieces of code one makes the notifications when the app goes to the background then when the app goes to the foreground I clear Tnotificationcenter. I've used the appevent function to trigger the enterdbackground and willbecomeforground.

The Appevent code looks like this:

function TForm1.AppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
begin
 case AAppEvent of
     TApplicationEvent.EnteredBackground:
      begin


         Gethourregistration(FEmployeeNr,NOW);
          if dm.MthourregistrationControl.RecordCount<1 then
            begin
           var Notification := NotificationCenter1.CreateNotification;
            try
            Notification.Name := 'message1';
            Notification.AlertBody := 'You have not yet entered todays hours!';
                Notification.FireDate :=Date encodetime(19,0,0,0);

            { Send notification in Notification Center }
            NotificationCenter1.ScheduleNotification(Notification);


              Notification.Name := 'message2';
            Notification.AlertBody := 'You have not yet entered todays hours!';
                Notification.FireDate :=Date encodetime(21,0,0,0);

            { Send notification in Notification Center }
            NotificationCenter1.ScheduleNotification(Notification);   

            finally
              Notification.Free;
            end;


          end;

      end;
      TApplicationEvent.WillBecomeForeground:
        Notificationcenter1.CancelAll;

    end;
end;

I use the OnCreate event for asking permission for sending notifications and trigger the AppEvent.

procedure TForm1.FormCreate(Sender: TObject);
var
AppEventSvc: IFMXApplicationEventService;

begin
  if NotificationCenter1.AuthorizationStatus <> TAuthorizationStatus.Authorized then
  begin
    FPendingAction := Action;
    NotificationCenter1.RequestPermission;
  end;
    if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(AppEventSvc)) then
    begin
        AppEventSvc.SetApplicationEventHandler(AppEvent);
    end;
end;

I have tested this code and for me, this works perfectly.

  • Related