Android Question Notification sound

fishwolf

Well-Known Member
Licensed User
Longtime User
I have problem with app sound when arrived a push and show the notification
The sound is not always played, one time yes, one time no, ecc,ecc.

Why?

Thanks

B4X:
sub Process_Globals
Private fm As FirebaseMessaging
Dim NotifSound As SoundPool
Dim NotifID As Int
End Sub

Sub Service_Create
    fm.Initialize("fm")
    
    NotifSound.Initialize(2)
    NotifID = NotifSound.Load(File.DirAssets, "notification.ogg")
    
End Sub


Sub fm_MessageArrived (Message As RemoteMessage)
Dim n As Notification

    n.Initialize
    
    n.Icon = "notification"
    n.Sound = False
    n.Vibrate = False
    n.Light = False
    n.OnGoingEvent = False
    n.AutoCancel = True
    
    n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Message.GetData.Get("activity"))
        
    n.Notify(1)
    
    NotifSound.Play(NotifID, 1, 1, 1, 0, 1)
    
end sub
 

Albert Kallal

Active Member
Licensed User
I have a less then ideal answer! I find (and other posts confirm) that for some reason "often" the first sound does not play. My current solution is to always fire off a dummy sound (a blank tiny sound file with nothing) before actually having to fire off the real sound I want. I do find that on startup firing off this one time "dummy" sound works quite well. However, if the application is paused, or resume, then it can become "difficult" to nail down when playing the sound fails. So, play a very wee tiny sound right before the required sound - you find that tends to make this work.

I apologize for lack of a better answer - I am not sure if there is a better solution then what I suggest.

Regards,
Albert D. Kallal
Edmonton, Alberta Canada
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
with Androind 9
i guess you need to use fileprovider for the soundfile on Android 7+
I remember i did it some time ago for a customer runng Android 8. Using fileprovider it did work for me.
 
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
i guess you need to use fileprovider for the soundfile on Android 7+
I remember i did it some time ago for a customer runng Android 8. Using fileprovider it did work for me.
but not played also the default sound with simple notification example, is a system sound
 
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
It depends on the your device settings.

Test it with a high importance notification and set the Sound property to True.

Make sure that the device is not muted and not in "do not disturb" mode.

not played, other apps play the sound

B4X:
    n.Initialize("MyDefault", "MyChannel", "HIGH").AutoCancel(True).SmallIcon(smiley)
    n.SetDefaults(True, True, True)
    
    n.Build("Title", "Content", "tag1", Me).Notify(4)
 
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
all conditions are true

B4X:
Public Sub CustomSound (FileProviderUri As Object) As NB6
    If IsOld Then Return Me
    ctxt.RunMethod("grantUriPermission", Array("com.android.systemui", FileProviderUri, 1))
    Log("CustomSound")
    If IsBuilder Then
        Log ("IsBuilder")
        NotificationBuilder.RunMethod("setSound", Array(FileProviderUri, NotificationStatic.GetField("AUDIO_ATTRIBUTES_DEFAULT")))
        If IsChannel Then
            Log ("IsChannel")
            Channel.RunMethod("setSound", Array(FileProviderUri, NotificationStatic.GetField("AUDIO_ATTRIBUTES_DEFAULT")))
        End If
    End If
    Return Me
End Sub
 
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
also with wav the custom sound is not played.
also system sounds are not played.
is played with the old method (first post) with the same problem
 

Attachments

  • NB6.zip
    306.9 KB · Views: 185
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
The sound Is setted for play the sound, old method work fine, but not the 100% of times
I have done a video
The method with classe not play the sound
In the example the sound file Is copied in share Cordero. Why?

View
 
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
It is probably an OS issue.


This is required when sharing files with external programs.

i have resolved with a sleep(1000)

B4X:
    n.Notify(1)
    
    Sleep(1000)

    NotifSound.Play(NotifID, 1, 1, 1, 0, 1)


copy is mandatory? work fine also without
 
Upvote 0
Top