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

Code:
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
 
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
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
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
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

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

Code:
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
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

Upvote 0
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
It is probably an OS issue.


This is required when sharing files with external programs.

i have resolved with a sleep(1000)

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

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


copy is mandatory? work fine also without
 
Upvote 0
Back
Top