Android Question NB6 sound / vibrate for LOW notifications

giggetto71

Active Member
Licensed User
Longtime User
Hi guys,
I am using the NB6 class for notifications and I have one doubt. I am coding the app to let the user to choose if some notified conditions do sound and or vibration (both configurable).
the sub I call to create the notification is:


B4X:
Sub Simple_Notification2(sound As Boolean,vibrate As Boolean,NotificationText As String)
    Dim n As NB6
    n.Initialize("default", Application.LabelName, "LOW").SmallIcon(EventPic)
    Dim cs As CSBuilder
    n.SetDefaults(sound,False,vibrate)
    n.BigTextStyle("Alert", cs.Initialize.BackgroundColor(Colors.White).Append("Active alerts...").PopAll, NotificationText)
    n.Build("Alert", "New event(s). Click for details", "EventNotification", Main).Notify(1)
    'Notification1.Notify(1)
End Sub

the notification itself works fine and but regardless what I set with the SetDefaults method, I can only influence the sound and the vibration by setting the notification as "LOW" (no sound, no vibration) or "DEFAULT" (in that case I get sound AND vibration) again regardless the SetDefaults.
I am sure I am doing something wrong.
Can you help?
thanks!!!
 

giggetto71

Active Member
Licensed User
Longtime User
I kept testing and testing even with Erel's NB6 original example but I cannot find a way to control vibration and sound individually. I can only get (sound AND vibrate with "default" OR (no sound, no vibration) with LOW). Do you guys think it is possible? if it is not, then I will stop trying and live with that..but I am pretty sure there must be a way as there are apps that can do that..thanks for your help.
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
ok. and what about the default one? Can I customize those to have/not have sound and/or vibration?
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
ok but I think this is what I am trying to do here:

B4X:
Sub DefPriority_Notification
    Dim n As NB6
    
    n.Initialize("default", Application.LabelName, "DEFAULT").SmallIcon(smiley)
    n.SetDefaults(True, False, False)
    n.Build("Not important...", "Content", "tag", Me).Notify(10)
End Sub

I set the channel importance in the initialize to "default" and then I call the SetDefaults, to change the vibration to "false" but the notification still vibrates.
Did I misundestand your reply?
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
Which clearly is something I am not able to do..can you provide an example of a notification that only vibrates and not sound? Or one that sound and not vibrate?
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
I haven't touched b4a much in some time and haven't looked at the NB6 class. I also suffered a major data loss and lost all of my code about 12 months ago.
If you are targetting a reletively new sdk, then channels come into play.
If I remember correctly you have to set a channel imprtance and set the lights / sound / vibration against this.
BUT, again, if my memory serves me right, once this channel is created, you cannot change it. So pushing an 'update' to your device won't update the channel. Try uninstalling the older version first before pushing from b4a.

As I say, I'm not fully up to speed with it, so I could be wrong.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
I am that far out of touch that I don't even have my own library or any additional library/class installed in b4a. I only installed b4a again recently when it became free (Thank you Erel), but have been too busy with personal issues to get back into it
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
ok..thank you all for the inputs. I have uninstalled (after that actually I could get the sound only..) the app and simplified it.
The attached code has 3 buttons. The first fires a "sound only" (here I set the SetDefaults(True,False,False))notification and works. the second is a default (no SetDefaults called) so it actually issues "sound and vib" but the third button (for which I set the SetDefaults (False, False, True) does not work (despite the false on the sound parameter, it keeps sounding..)
Can you help to understand what's wrong with function "Simple_Notification_Vib"? How do I change 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

Sub Process_Globals
    Private smiley As Bitmap
    Private xui As XUI
    Private OldIntent As Intent
End Sub

Sub Globals
    Private CLV As CustomListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        smiley = LoadBitmapResize(File.DirAssets, "smiley.png", 24dip, 24dip, False)
    End If
    Activity.LoadLayout("1")
    CLV.DefaultTextBackgroundColor = Colors.Black
    CLV.AddTextItem("sound only", "Simple_Notification_Sound")
    CLV.AddTextItem("Sound & Vib", "Simple_Notification_Sound_Vib")
    CLV.AddTextItem("only vib", "Simple_Notification_Vib")
End Sub

Sub CLV_ItemClick (Index As Int, Value As Object)
    CallSub(Me, Value)   
End Sub

Sub Activity_Resume
    'Code to get the notification tag after the user clicks on a notification.
    Dim in As Intent = Activity.GetStartingIntent
    If in.IsInitialized And in <> OldIntent Then
        OldIntent = in
        If in.HasExtra("Notification_Tag") Then
            Log("Activity started from notification. Tag: " & in.GetExtra("Notification_Tag"))
        End If
    End If
End Sub

Sub Simple_Notification_Sound
    Dim n As NB6
    n.Initialize("default1", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(smiley)
    n.SetDefaults(True,False,False)   
    n.Build("sound only", "sound only", "tag1", Me).Notify(1) 'It will be Main (or any other activity) instead of Me if called from a service.
End Sub

Sub Simple_Notification_Sound_Vib
    Dim n As NB6   
    n.Initialize("default2", Application.LabelName,"DEFAULT").AutoCancel(True).SmallIcon(smiley)
    'n.SetDefaults(True,False,True)
    n.Build("sound and vib", "sound and vib", "tag2", Me).Notify(2)   
End Sub


Sub Simple_Notification_Vib
    Dim n As NB6   
    n.Initialize("default3", Application.LabelName,"DEFAULT").AutoCancel(True).SmallIcon(smiley)
    n.SetDefaults(False, False, True)
    n.Build("vib", "vib", "tag3", Me).Notify(3)
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
Thanks for your reply. I am testing on a android 9 device. I have also made a test on a ver. 8.0.0 and it behaves the same. It really seems that it is possible to get sound only by setting Setdefaults(true,false,false) but NOT vibration only (I.e Setdefaults(false,false,true) seems to be ignored). Any workaround you may think of?
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
just tested. the "silent.mp3" customsound workaround worked! I have used this collection of blank mp3 and chosen the 250ms of silence.
now it works as I intended too. not ideal but a reasonable workaround.
just in case in the future someone finds the way to make "vib only" notifications please update this case..
thanks to all.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
It is a work around.
I think you should have been looking more at creating multiple channels with different settings for each and issue the relevant notifications against those channel based on desired effects.
I am not sure if the NB6 class allows this though.
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
It is a work around.
I think you should have been looking more at creating multiple channels with different settings for each and issue the relevant notifications against those channel based on desired effects.
I am not sure if the NB6 class allows this though.
You can do it with NB6, but that is not what Google expects from the channel notifications - i.e. Google expects that the end user of the application controls if they want the sound (what sound they want), vibration and what notification importance is, after the notification channel is created. They can change it in Application Notification Settings.
So, having an application creating different notification effects is not what they would want - although end user can still change the setting for each channel in the Notification Settings. They can select "silent" sound with the sound picker, so there is no need for adding the blank.mp3

Google Description of the new notification with Channels:
Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel. For each channel, you can set the visual and auditory behavior that is applied to all notifications in that channel.
Then, users can change these settings and decide which notification channels from your app should be intrusive or visible at all.
After you create a notification channel, you cannot change the notification behaviors—the user has complete control at that point. Though you can still change a channel's name and description.
 
Upvote 0
Top