Android Question strange issue with Notification object

giggetto71

Active Member
Licensed User
Longtime User
Hi guys,
I am starting using the notification object but I am facing a probably trivial issue.
I have started from some examples posted in the forum and the notifications appear fine but form some reasons the vibration and the sound won't work..

B4X:
Private Sub UpdateNotification(sound As Boolean)
    Dim icon As String
    Dim content As String
    If ConnectedStatus Then
        icon = "connected"
        content = "connected"
    Else
        icon = "disconnected"
        content = "disconnected"
    End If
    
    Notification1.Initialize2(Notification1.IMPORTANCE_LOW)
    Notification1.Sound = sound
    Notification1.Vibrate = sound
    Notification1.Icon = icon
    Notification1.SetInfo("test1:" & InternalNum, content, Main)
    Notification1.Notify(1)   
End Sub
the sub is called by a timer and every 10 seconds it is called with "soud" = True and I would expect it to sound and vibrate but it does not.
I made sure the Setinfo is called just before the Notify...any idea?

Any idea?
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
B4X:
Notification1.Initialize2(Notification1.IMPORTANCE_LOW)

IMPORTANCE_LOW
Added in API level 24
public static final int IMPORTANCE_LOW
Low notification importance: Shows in the shade, and potentially in the status bar (see shouldHideSilentStatusBarIcons()), but is not audibly intrusive.

"is not audibly intrusive" would imply to me that there will be no sound or vibration. Change the importance level & see what happens.

Also if you haven't already, you should probably look at this thread -> https://www.b4x.com/android/forum/threads/custom-notifications-channel.91634/

- Colin.
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
ok. clear. thanks, low priority prevents sound, I did not notice that before. thanks.

after your reply, I searched in the forum and found


which apparently should work for me but for some reasons using the same function:

B4X:
Private Sub CreateNotification(Title As String, Content As String, Icon As String, TargetActivity As Object, _
    Sound As Boolean, Vibrate As Boolean) As Notification
   Dim p As Phone
   If p.SdkVersion >= 21 Then
       Dim nb As NotificationBuilder
      
       nb.Initialize
       nb.DefaultSound = Sound
       nb.DefaultVibrate = Vibrate
       nb.ContentTitle = Title
       nb.ContentText = Content
       nb.setActivity(TargetActivity)
       nb.SmallIcon = Icon
       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
       Return  nb.GetNotification
   Else
       Dim n As Notification
       n.Initialize
       n.Sound = Sound
       n.Vibrate = Vibrate
       n.Icon = Icon
       n.SetInfo(Title, Content, TargetActivity)
       Return n
   End If
End Sub

and calling it using:
B4X:
    Notification1 = CreateNotification("test1:" & InternalNum, content , icon , Main, sound , sound)
    Notification1.Notify(1)

but still when sound = true it does not sound nor vibrate,
thanks for your help!
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
Hi
For testing, uninstall the application. Rename the package and reinstall it.
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
ok. thank for your reply. At the end I decided to use the NB6 class which works very well. thanks!
 
Upvote 0
Top