Android Question [RESOLVED] Question about New Notification Methods

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hello

I remember using the old notification method and it was quiet simple to use. Most of my applications have more then 1 background service running. I generally have a primary server running, svc_service, and it has the notification object defined in it. All the other services that need to run as background tasks would have used Service.StartForeground(1,svc_service.n1) in there service_create. This all worked lovely.

Now, the new method I find a bit confusing in respect to being able to change the icon, and the text. For example when the app of offline, I want to who an offline icon and the test says offline, and similarly when the app is on line. This I cannot seem to do correctly.

This is my current method - does not really work properly.

svc_service - (relevant functions only)
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
 
   Dim tmrServiceHook As Timer 
   Dim br As BroadCastReceiver
   Dim pw As PhoneWakeState
   Dim PE As PhoneEvents
   Dim PhoneId As PhoneId
   Dim nID As Int = 1
 
 
End Sub

Sub Service_Create
 
   br.Initialize("br")
   mod_functions.writelog("svc_service(), Service_Create")
 
   Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
   show_notify_offline

   pw.PartialLock

   ' // phone events
   PE.InitializeWithPhoneState("PE",PhoneId)   
   ' // set the serve hook to be called every 1 seconds
   tmrServiceHook.Initialize("tmrServiceHook",1000)
 
End Sub

Sub Service_Start (StartingIntent As Intent)
 
 
   mod_functions.writelog("svc_service(), Service_Start")
 
   br.addAction("android.intent.action.SCREEN_OFF")
   br.addAction("android.intent.action.SCREEN_ON")
   br.SetPriority(2147483647)
   br.registerReceiver( "")
 
   tmrServiceHook.Enabled = True
 
   If Not(Main.APPSET.currentChannel.IsInitialized) Then
       Main.APPSET.currentChannel.Initialize
   End If
 
   ' // update notification
   If Main.APPSET.IsOnline Then
       show_notify_online
   End If
   If Not(Main.APPSET.IsOnline) And Main.APPSET.currentChannel.call_state = mod_core_consts.CS_CLOSED  Then
       show_notify_offline
   End If
   If Not(Main.APPSET.IsOnline) And Main.APPSET.currentChannel.call_state = mod_core_consts.CS_SNOOZING  Then
       show_notify_sleeping
   End If
   If Not(Main.APPSET.IsOnline) And Main.APPSET.currentChannel.call_state = mod_core_consts.CS_OFFLINE  Then
       show_notify_offline
   End If
 
 
 
End Sub

Sub Service_Destroy
 
   br.unregisterReceiver
   tmrServiceHook.Enabled = False
   Service.StopAutomaticForeground
   pw.ReleasePartialLock
   mod_functions.writelog("svc_service(), Service_Destroy")

End Sub

Sub show_notify_online()

   Service.AutomaticForegroundNotification = CreateNotification("Atlas PTT","Online","icon_online",Main,False,False)
 
End Sub

Sub show_notify_offline()

   Service.AutomaticForegroundNotification = CreateNotification("Atlas PTT","Offline","icon_offline",Main,False,False)
 
End Sub

Sub show_notify_sleeping()

   Service.AutomaticForegroundNotification = CreateNotification("Atlas PTT","Sleeping until " & DateTime.Date(Main.APPSET.snooze) & " " & DateTime.Time(Main.APPSET.snooze),"icon_offline",Main,False,False)

End Sub

Sub get_service_notification As Notification
 
   Return Service.AutomaticForegroundNotification
 
End Sub


Private Sub CreateNotification(Title As String, Content As String, Icon As String, TargetActivity As Object, Sound As Boolean, Vibrate As Boolean) As Notification
   
   mod_functions.writelog("svc_service(), CreateNotification - " & Content)
   
   Dim p As Phone
   Dim n As Notification
   
   If p.SdkVersion >= 21 Then
       Dim nb As NotificationBuilder
       Dim Channel As NotificationChannelBuilder
       Dim ChannelID As String = "ies_ptt"
       Dim ChannelName As String = "Atlas Push To Talk"
       
       ' // Build a Channel
   
       If Channel.ChannelsSupported Then
           Channel.Initialize(ChannelID, ChannelName, Channel.IMPORTANCE_LOW)
           Channel.Build
       End If
       
       nb.Initialize(ChannelID)
       nb.AutoCancel = False
       nb.OnGoingEvent = True
       nb.DefaultSound = False ' sound
       nb.DefaultVibrate = False ' vibrate
       nb.DefaultLight = False
       nb.ContentTitle = Title
       nb.ContentText = Content
       nb.setActivity(TargetActivity)
       nb.SmallIcon = Icon
       nb.Number = nID
       ' //set the return object for the notification object (n1) in this service
       Return nb.GetNotification
   Else
       n.Initialize
       n.Number = nID
       n.Light = False
       n.Vibrate = False
       n.Sound = False
       n.OnGoingEvent = True
       n.Icon = Icon
       n.SetInfo(Title, Content, TargetActivity)
       Return n
   End If
   
   
End Sub

Any other service that want to be a background running task does this on se4rvice_create

B4X:
Sub Service_Create
 
   Service.AutomaticForegroundNotification = CallSub(svc_service,"get_service_notification")
   Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
         
End Sub


One of the problems I am havving, which is wany I want to deal with on this post is in svc_service you can call on of the following functions;
show_notify_online(),show_notify_offline(),show_notify_sleeping() which in turn will create/recreate the notification. It stops doing the icon change and text change after about 2 calls to any of these functions.

I am obviously doing something wrong, any ideas. Also when I link the other services to the notification in svc_service, does this link get effected when the icon is changed ?

Regards

John.
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
The automatic notification id is 51042.

You can create a new notification object and show it with Notification.Notify.

Thanks Erel, I am ok with the other services calling svc_service.get_service_notification so they can bind to that notification so they can operate as a long running background task ?
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Don't reuse the notification. If you have a single foreground service then the app is in the foreground state. Other services will not be killed.


Erel, Thanks for the reply. Just to be clear.

If I have a single service, svc_service as my main background task which has a notification icon are the following conditions true.

1. All other services create will adopt the state of svc_service.
Example. I have a another service called svc_data. it currently has the following:

B4X:
Sub Service_Create
 
   Service.AutomaticForegroundNotification = CallSub(svc_service,"get_service_notification")
   Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
 
   pw.PartialLock

End Sub

2. My understanding is when a service loads it will try to create a notification icon. I do not want this, but want the service to remain running.

3. Do I just need to create a partial lock on all other services and how do I not have these other services no attempt to create a notification icon.

Sorry if this seems a bit confusing.

Regards

John.

[UPDATE]

Ignore this post as "Don't reuse the notification. If you have a single foreground service then the app is in the foreground state. Other services will not be killed." is true as I tested it.
 
Last edited:
Upvote 0
Top