Android Question Local notification Issue with startserviceat

holdemadvantage

Active Member
Licensed User
Longtime User
Hi all,

my target is to send local notificationin certain days (Wed and Fri) and at certain hours (in my example at 15.37 and 15.39)

A Main Module

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
Dim NowDay As Long

End Sub

Sub Activity_Create(FirstTime As Boolean)


NowDay = DateTime.DateParse(DateTime.Date(DateTime.Now))

StartServiceAt(Notif,NowDay,True)
End Sub

And a Notif Service


B4X:
#Region  Service Attributes
    #StartAtBoot: True
#End Region

Sub Process_Globals
End Sub

Sub Service_Create
End Sub

Sub Service_Start (StartingIntent As Intent)
Log(StartingIntent.Action)
Dim p As Period
p.Days = 1
StartServiceAt(Me, Min(NextTimeInstance(15, 31), NextTimeInstance(15, 34)) ,True)
Dim tomorrow As Long = DateUtils.AddPeriod(DateTime.Now, p)

Dim wed As Int = 4
Dim fri As Int = 6
Dim today As Int = DateTime.GetDayOfWeek(DateTime.Now)

If today <> wed AND today <> fri Then

Else
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo("Title", "Message", Main)
    n.Notify(1)
End If
End Sub
Sub NextTimeInstance (hours2 As Int, minutes2 As Int) As Long
   Dim today As Long = DateTime.Now
   today = DateUtils.SetDateAndTime(DateTime.GetYear(today), DateTime.GetMonth(today), _
     DateTime.GetDayOfMonth(today), hours2, minutes2, 0)
   If today < DateTime.Now Then
     Dim p As Period
     p.Days = 1
     Dim tomorrow As Long = DateUtils.AddPeriod(today, p)
     Return tomorrow
   Else
     Return today
   End If
End Sub

I attach the code.

The issue is that when i reboot the device the notification is sent without control any time or day, could you please give me an help to solve this problem?

Thanks in advance!
 

Attachments

  • Not1.zip
    11.9 KB · Views: 116

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

holdemadvantage

Active Member
Licensed User
Longtime User
Well i try to delete startserviceat.... an activity.create and modify service this way

B4X:
Sub Service_Start (StartingIntent As Intent)
StartServiceAt(Me, Min(NextTimeInstance(15, 31), NextTimeInstance(15, 34)) ,True)
Dim passed As Boolean
Log("SERVIZIO NOTIF STARTED")
Log("Servizio in pausa: " & IsPaused("Notif"))


If StartingIntent.Action= "android.Intent.action.BOOT_COMPLETED" Then
Log("STARTED AFTER BOOT")
Else
Log("NOT STARTED AFTER BOOT")
End If
If IsPaused("Notif") Then'note that IsPaused("Notif") give TRUE even if service was not started
Log("NOTIF IS IN PAUSE")

Dim p As Period
p.Days = 1



Dim Tue As Int = 3

Dim today As Int = DateTime.GetDayOfWeek(DateTime.Now)
Log(today)
If today = Tue  Then

    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo("Title2", "Today is tuesday", Main)
    n.Notify(1)

End If

Else
Log("NOTIF IS IN NOT PAUSE")


End If


End Sub

But when i reboot smartphone i receive notification and i have this log, it seemes that notif start 2 times , but i can't figure out why (and also it seems that start at boot of the service is not tracked)

B4X:
LogCat connected to: 9fadbe8d
--------- beginning of /dev/log/system


--------- beginning of /dev/log/main


** Service (notif) Create **


** Service (notif) Start **


(Intent) Intent { act=android.intent.action.BOOT_COMPLETED flg=0x8000010 cmp=b4a.example/.notif$notif_BR (has extras) }
ParseApplication.onCreate xml Id = 2130903040


data null
data1 JTCxivX1U0cyqRwbasKJ8lOBUPIGWYOafAZOVWgo


data2 llWAKDRnubTZLek5Sg7ROkMQDmiDi0YQQVJkAoBH
ParseApplication.onCreate xml Id = 2130903040


data null


data1 uHhQryoU7zwzerMuH984SA26IdprQtvKZfBnP2tW
data2 NMQnz6Ov4qdXoMZQSF4R6FDVzqKfvnlbD7ZPniJp
ParseApplication.onCreate xml Id = 2130903040


data null


data1 RulSGqbdx4nw0eVe3TPTM8pAUW25zl3iIrqlpMZY
data2 tMVXaXZoZfVrnbpcsE1UBOI7w1rT9yLFD9vEGRAh
ParseApplication.onCreate xml Id = 2130968577


data null


data1 u3JIWhzfdRzU15kDKecJQgHQaladVGI3hOT1Jpze
data2 E3tMyqL3hLCQaYsBpxuiokTLm8FmnP94udeu4DGL
ParseApplication.onCreate xml Id = 2130903040


data null
data1 XGbUNXlkyGldoGYaYJzzXgIfNuC4f1wiphlj7WD2
data2 7uexsnBCn7ldy025o7anqxzZt62HZr0R8MxKWGTp


** Service (notif) Create **


** Service (notif) Start **


SERVIZIO NOTIF STARTED
Servizio in pausa: false
NOT STARTED AFTER BOOT
NOTIF IS IN NOT PAUSE
ParseApplication.onCreate xml Id = 2130903040


data null
data1 awXEShWYKG7HyNY1OZaDLP4MSJyDiiJdWkbIOmnS
data2 htb3OMnQOYAqadeHlaUbm4ZCaSBAQt4zW6exYiZc


ParseApplication.onCreate xml Id = 2130903040


data null


data1 rch759bHXZKFZoi9JSrEd5RdWyZlU8Q0UQckC5ZH
data2 LAaz5J3H3MUPdLNjosrA8jNKQmSCSxaqPGJr34x4
ParseApplication.onCreate xml Id = 2130903041


data null
data1 uHhQryoU7zwzerMuH984SA26IdprQtvKZfBnP2tW
data2 NMQnz6Ov4qdXoMZQSF4R6FDVzqKfvnlbD7ZPniJp


ParseApplication.onCreate xml Id = 2130903041


data null
data1 BsH9rDYAxv6btqib8QmENj26Iq0IjdOnf8bA7f5d
data2 4jHfCngitBFof4Zd174mq255Bk6EDTVdkxQGjEBn


ParseApplication.onCreate xml Id = 2130903040


data null
data1 rch759bHXZKFZoi9JSrEd5RdWyZlU8Q0UQckC5ZH
data2 LAaz5J3H3MUPdLNjosrA8jNKQmSCSxaqPGJr34x4


ParseApplication.onCreate xml Id = 2130903041


data null
data1 3TM7I10jOLxhZiFLU72fRlsWThpHL9TEZ6xAovLm
data2 pucXgffoBGlsTMw6BgcDMnvZVuN7VPflZMAC5mth


ParseApplication.onCreate xml Id = 2130903041


data null


data1 FFVpanuthwPnkqJBQX82u6jk3de6TGSeSDVbdyS8
data2 zw7l2NPbaiQLO7eIwcIUpJwBnVgElzAwTWdxLOrR
ParseApplication.onCreate xml Id = 2130968577


data null


data1 z6QnLDRsDZGSm9TwYZk6y3dxaBrp2XJaEuOG0Twp
data2 haIJzvFTZT3amjsbA0xssiSzgAlLqCMktv0nvMlg
ParseApplication.onCreate xml Id = 2130903040


data null
data1 rch759bHXZKFZoi9JSrEd5RdWyZlU8Q0UQckC5ZH
data2 LAaz5J3H3MUPdLNjosrA8jNKQmSCSxaqPGJr34x4
ParseApplication.onCreate xml Id = 2130903041


data null
data1 RulSGqbdx4nw0eVe3TPTM8pAUW25zl3iIrqlpMZY


data2 tMVXaXZoZfVrnbpcsE1UBOI7w1rT9yLFD9vEGRAh
ParseApplication.onCreate xml Id = 2130903041


data null
data1 AjCbcnWlqeFYGPeSee05PfD0tm2wZ0v6bP4JnCIM
data2 e4LZNzNGeRi3UwtAIbrIW57dzZRYrakEp3AvVMnP


Surely i am making a mistake, i am a little frustrated :(:(
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Try this ..
You will have to workout how to start the service at the different times
A Hint is in full project attached

Main Module ..
B4X:
Sub Activity_Create(FirstTime As Boolean)

   Dim i As Intent
   i.Initialize("Started_From_MAIN", "")
   i.SetComponent("com.preekog.notification1/.notif") 'example "b4a.example/.service1"
   StartService(i)

End Sub

Service Module ..
B4X:
Sub Service_Start (StartingIntent As Intent)

   'schedule next service start regardless
   ScheduleNextServiceStart(15,31) '@@ next Wed or Fri at 15:31

   If StartingIntent.Action = "android.intent.action.BOOT_COMPLETED" OR _
     StartingIntent.Action = "Started_From_MAIN" Then
      'Do nothing ..

   Else
     Dim n As Notification
     n.Initialize
     n.Icon = "icon"
     n.SetInfo("Title", "Message", Main)
     n.Notify(1)
   End If

   StopService("")

End Sub

*** !!! NOTE .. Whilst this did work previously ..playing with the above code / Zip file the notification display on a timed service start now
seems to jam up my devices ??? only uninstalling the app frees up the devices. investigating .....
 

Attachments

  • notif2.zip
    12.2 KB · Views: 117
Last edited:
Upvote 0

holdemadvantage

Active Member
Licensed User
Longtime User
Many thanks Mango and all

Great community as usual, i attach the project, this send a local notification in different languages every saturday at 14:15, hope this helps someone
 

Attachments

  • mynotification.zip
    12.7 KB · Views: 127
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Great community as usual, i attach the project, this send a local notification in different languages every saturday at 14:15, hope this helps someone
Are you sure this works as expected ?? .. I am unable to run either version without jamming up devices on notification display :confused:
 
Upvote 0
Top