Android Question NotificationBuilder light - finally solution ?

peacemaker

Expert
Licensed User
Longtime User
Hi, All

I read all, and tried all, but no light at the notifications. Android 7 on Samsung.
Screen is off, setCustomLight and DefaultLight were tried... PartialLock is used also.
But messengers and SMS during notification switch LED on.

But how to activate the LED?
 
Last edited:

wes58

Active Member
Licensed User
Longtime User
Hi, All

I read all, and tried all, but no light at the notifications. Android 7 on Samsung.
Screen is off, setCustomLight and DefaultLight were tried...
But messengers and SMS during notification switch LED on.

But how to activate the LED?
I had the same problem (it was a while ago), and I think, the solution was that I had to use vibration in the notification as well to get LED come on. Because it was so long ago, I think that this fixed the problem - at least it is still working in my application. I have Galaxy S7 with Android 7.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
I always use the vibration, and it's working always. But not this f...ng backlight, that is required for the project.
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
I always use the vibration, and it's working always. But not this f...ng backlight, that is required for the project.
I just tested on my app. If I turn vibration off (in my app notification setting!) - no LED light comes on. After I enable vibration I get LED flashing again on notification.
But you are talking now about backlight? So I am not sure what is the issue.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Yes, LED i meant. (Screen backlight is also interesting, but it's another topic, i guess).
May you share your notification code ?
Is wakelock used also ? What kind ?
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
Yes, LED i meant. (Screen backlight is also interesting, but it's another topic, i guess).
May you share your notification code ?
Is wakelock used also ? What kind ?
I can show you the code. I had to delete some parts, but it still looks a bit complex (or messy), but notification part should be there. I am using NotificationBuilder library by Barx
I am using it in the NotificationService for repeating notifications received (intercepted) from different applications defined in app settings. Each app can have defined different LED color, different LED on/off times, vibration on/off (has to be on for LED to work - thanks to Google), different sound.

B4X:
Sub SendNotification(nt As NOTIFY1 , show As Boolean, sound As Boolean)
    Dim nb As NotificationBuilder
    Dim nbIn As NotificationInboxStyle
    Dim v() As Long    Dim sb As StatusBarNotification
    Dim r As Reflector
    Dim p As Phone
    Dim myIntent As Intent

'        myIntent.Initialize("", "")
'        myIntent.SetComponent("com.NotifyMe/.notificationservice")   
'        myIntent.PutExtra("Notify_Tag", "ClearLast")

        nb.Initialize
'        nb.setServiceIntent(myIntent)
        nb.Priority = 10
        nb.AutoCancel = True
        nb.Ticker = sb.TickerText
        nb.Number = nt.repeat            'notification repeat number
        nb.When = nt.ntime                'notification time
        nb.DefaultSound = False       
        nb.CustomSound = aa.sound        'custom sound from app settings
        nb.ShowTime = True
        nb.ContentTitle = nt.title
        nb.ContentText = "(" & nt.repeat & ") " & aa.name & " - " & sb.TickerText   
        If msgList.Size > 1 Then
            nb.SubText = "Swipe down for previous notifications..."
            nbIn.Initialize
            nbIn.BigContentTitle = "Previous Notifications"               
            nbIn.SummaryText = actProfile.msg
            'some code removed for adding text to the notificationInboxStyle
            nb.SetStyle(nbIn)
        End If
        nb.SmallIcon = actProfile.icon
        nb.LargeIcon = LoadBitmap(File.DirAssets, actProfile.icon & ".png")
        nb.DefaultLight = False
        nb.DefaultVibrate = False
        If aa.vibrate = True Then                    'vibration on/off defined in app setting
            v = Array As Long(0, 100, 300, 500, 300)
            nb.CustomVibrate = v
        End If
        nb.setCustomLight(aa.rgb, aa.lOn, aa.lOff) 'LED color, time ON and time OFF defined in setting
    
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "power", "java.lang.String")
    Dim scnon As Boolean
    scnon = r.RunMethod("isScreenOn")
    If Not(scnon) Then
        If Not(nFlag.Proximity) Then
            nFlag.Proximity = phSensor.StartListening("ps")    'proximity sensor events
            If nFlag.Proximity = False Then
                ToastMessageShow("Proximity Sensor not supported", False)
                sensorTmr.Enabled = False
            Else
                sensorTmr.Enabled = True
            End If
        End If
    End If
    'removed code to show toast message
    pws.PartialLock
    nb.Notify(notifyID1)
End Sub
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Thanks, my code is:
B4X:
'Starter service
Sub Process_Globals
 Dim Notif1 As Notification
...
End sub

Sub Service_Create
    Notif1.Initialize
  
End Sub

'others.bas code module for shared subs
Sub Notif(Content As String, Insistent As Boolean, Sound As Boolean, TargetActivity As Object)
    If Starter.Notif1.IsInitialized = False Then
        Starter.Notif1.Initialize
    End If

    Dim p As Phone
    If p.SdkVersion >= 21 Then
        Dim nb As NotificationBuilder
        nb.Initialize
        nb.DefaultSound = Sound
        nb.DefaultVibrate = Sound
        nb.ContentTitle = ""
        nb.ContentText = Content
        nb.setActivity(TargetActivity)
        If Content <> "" Then
            nb.SmallIcon = "icon"
        Else
            nb.SmallIcon = ""
        End If
        nb.Priority = 4    'IMPORTANCE_HIGH
        nb.AutoCancel = Sound
        nb.setCustomLight(0xff00ff00, 300, 1000)
        'nb.DefaultLight = True
        If p.SdkVersion >= 26 Then
            Dim ctxt As JavaObject
            ctxt.InitializeContext
            Dim manager As JavaObject
            manager.InitializeStatic("android.app.NotificationManager")
            Dim Channel As JavaObject
            Dim importance As String
            If Sound Then importance = "IMPORTANCE_DEFAULT" Else importance = "IMPORTANCE_LOW"
            Dim ChannelVisibleName As String = Application.LabelName
            Channel.InitializeNewInstance("android.app.NotificationChannel", _
                   Array("MyChannelId1", ChannelVisibleName, manager.GetField(importance)))
            manager = ctxt.RunMethod("getSystemService", Array("notification"))
            manager.RunMethod("createNotificationChannel", Array(Channel))
            Dim jo As JavaObject = nb
            jo.RunMethod("setChannelId", Array("MyChannelId1"))
        End If
        Starter.Notif1 = nb.GetNotification
    Else
        Dim n As Notification
        n.Initialize
        n.Sound = Sound
        n.Vibrate = Sound
        n.Insistent = Insistent
        If Content <> "" Then
            n.Icon = "icon"
        Else
            n.Icon = ""
        End If
        n.SetInfo(Application.LabelName, Content, TargetActivity)
        n.AutoCancel = Sound
        n.Light = Sound
        Starter.Notif1 = n
    End If
    Starter.Notif1.Notify(10)
End Sub

I see the important difference for the PartialLock.
And strange that here i did not use Service.StartForeground(10, Notif1)....
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
Maybe you could also try to use NotificationBuilder library? Not sure if that would make a difference.
I also have default vibrate false and use custom vibrate.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
:) wes58, NotificationBuilder is exactly used in my code, if you did not see.
Aha, you also used "priority"...
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
:) wes58, NotificationBuilder is exactly used in my code, if you did not see.
Aha, you also used "priority"...
One other thing that I use is, when specifing led color i use colors.red, colors.blue, etc. not like you 0xff00ff00. Does it make a difference? I can't check what value i get when using colors.blue, for example, because i am writing it on my Android tablet. Maybe it is not a problem. You can check it
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

wes58

Active Member
Licensed User
Longtime User
Why not?
B4X:
log(colors.blue)
Because, as I wrote, I am writing it on Android tablet. My laptop is off, and and there is no B4A IDE that works on Android environment, is There?
But, since you replied you could have said yes it is the same or no it isn't, instead of saying why not. This would be more helpful.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
I tried mentioned above difference - no help :(
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
You also can show the value in a label or an Alertbox as text! I don´t see where the problem is.
I thought it was obvious why I couldn't do it at the time. To modify an application to display the log I had to have access to the laptop and B4A and as I wrote before - I DIDN'T HAVE access to the latpop/PC when I was writing the replies.
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
I tried mentioned above difference - no help :(
I have created a simple application to test it.
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
'    #AdditionalRes: ..\resources

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
    Private Button2 As Button
    Private CheckBox2 As CheckBox
    Private CheckBox1 As CheckBox
    Private vibrate, sound As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")
    vibrate = False
    CheckBox1.Text = "Vibration " & vibrate
    sound = False
    CheckBox2.Text = "Sound " & sound
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub notify(b As Int)
    Dim nb As NotificationBuilder

        nb.Initialize
        nb.AutoCancel = True
        nb.SmallIcon = "icon"
        nb.LargeIcon = LoadBitmap(File.DirAssets, "iconb.png")
        nb.Priority = 2
        nb.DefaultVibrate = vibrate    'True 'False
        nb.DefaultSound = sound '    False 'True
        nb.DefaultLight = False   
        If b = 1 Then
            nb.ContentTitle = "Colour RED"
            nb.setCustomLight(Colors.Red, 400, 1000)
            nb.Ticker = "Colour Red"
        Else if b = 2 Then
            nb.ContentTitle = "Colour GREEN"
            nb.setCustomLight(Colors.Green, 400, 1000)
            nb.Ticker = "Colour Green"
        End If
        nb.ShowTime = True
        nb.setActivity(Me)
        nb.Notify(1)   
End Sub

Sub Button2_Click
    notify(2)
End Sub
Sub Button1_Click
    notify(1)
End Sub

Sub CheckBox1_CheckedChange(Checked As Boolean)
    vibrate = Checked
    CheckBox1.Text = "Vibration " & vibrate
End Sub

Sub CheckBox2_CheckedChange(Checked As Boolean)
    sound = Checked
    CheckBox2.Text = "Sound " & sound
End Sub
Create a layout with 2 buttons and 2 checkboxes.
Press the button to trigger notification and wait for the screen to go off. See if you get led flash.
I did different combination with sound and vibration.
When I have both sound and vibration FALSE, I didn't get any light flashing. When either sound or vibration was TRUE light was flashing
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
wes58, thanks very much.
Strange that you could not upload the whole test project. I have uploaded.

And the main :)....
Please, don't laugh much :) and promise you won't kill me :)
I have found finally that the testing phone does .... not .... have LED :)
AT ALL.
Just 2 LED backlighed buttons, but they are backligted together with the screen on, say, when SMS or message notification is received.
But, seems, it's not notification light control.
 

Attachments

  • FlashLED_test.zip
    44.2 KB · Views: 166
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Switching screen on at notification is OK by this.
But buttons backlight is still off. But seems, it's not the target :)
 
Upvote 0
Top