iOS Question Daily notification at exact time

Alvsky

Member
Licensed User
Longtime User
Hi

I need local notification to start every day 1 minute after midnight (at 00:01).
The notification is meant to read the data out of database which is relevant for that day.
I managed to schedule a notification for a next day but only once.
I put the Schedule code in Application_start so if application goes to background and is not restarted, but only awakened, notification will not be scheduled for next day.

I tried to use repeatInterval but with no success (or I am using it in a wrong way) - I tried to test it with one minute interval hoping that notification will be raised once more after scheduled time.


Here is the code I use:

B4X:
Private Sub Application_Start (Nav As NavigationController)

...
   
   ScheduleLocalNotification(0,1)

   Dim noNot As NativeObject = ln
   noNot.SetField("repeatInterval", 64) '16 - daily, 32 - every hour, 64 - every minute   

End Sub

Sub ScheduleLocalNotification(Hours As Int, Minutes As Int)

   Dim CurrentTime, Today, HourMinTicks As Long
'   Dim NextRunTime  As Long

   DateTime.DateFormat = "d.M.yyyy"

   'calculate ticks for hours and minutes schedule
   HourMinTicks = Hours * DateTime.TicksPerHour + Minutes * DateTime.TicksPerMinute

   'calc ticks up to start of today ... 00:00am
   Today = DateTime.DateParse(DateTime.Date(DateTime.Now))
   'calc ticks up to current/present time
   CurrentTime = DateTime.Now - Today

   If HourMinTicks > CurrentTime Then
       'Time has Not passed, schedule For Today
       NextRunTime = Today + HourMinTicks
   Else 'Schedule for tomorrow
       NextRunTime = Today + DateTime.TicksPerDay + HourMinTicks 'Schedule for tomorrow
   End If

    GetDBResult_2 ' reads the Database and sets ln.AlertBody

    Dim ln As Notification
    ln.Initialize(NextRunTime) 'at scheduled time
    ln.IconBadgeNumber = 1
    ln.AlertBody = jedan&CRLF&dva
    ln.PlaySound = True
    ln.Register
   
End Sub

Would someone be so kind help me with this since it is important for my app and I am stuck with this and also I am not so experienced with programming...

Thanks
 

Alvsky

Member
Licensed User
Longtime User
Thanks for suggestion. I implemented it and notifications work.

Just to make sure, will now notifications be repeated daily regardless in which state application is?
 
Upvote 0

Alvsky

Member
Licensed User
Longtime User
and how can I make it repeatable daily? I need it to be repeated once a day at midnight.

I tried this but i doesn't work from some reason:
B4X:
   Dim noNot As NativeObject = ln
   noNot.SetField("repeatInterval", 64) '16 - daily, 32 - every hour, 64 - every minute

For testing purposes I set the interval to 1 minute

I also have put the notification start in App_Background which resulted in 2 notifications at once.
 
Upvote 0

Alvsky

Member
Licensed User
Longtime User
Hi
Here I am after testing and the result is that, from some reason, Native object solution did not work for me so I solved it in another way:

B4X:
Private Sub Application_Start (Nav As NavigationController)

...

t_sched = 0 'scheduled time
t = FindNextTime(Array As Double(t_sched))

    GetDBResult_2 'gets notification text

    ln.Initialize(t) 'at scheduled time
    ln.IconBadgeNumber = 1
    ln.AlertBody = "Notification text"
    ln.Register

End Sub

...

Private Sub Application_Background
    
t = FindNextTime(Array As Double(t_sched))

    ln.Initialize(t) 'at scheduled time
    ln.IconBadgeNumber = 1
    ln.Register

End Sub

I tested this solution for few days ant it works without mistake: I get one notification at midnight every day.
Of course this solution presumes that app is opened at least once a day but for now I can live with that until I find some other solution.

Thanks again for help
 
Upvote 0
Top