Android Question [solved}Notification with custom sound.

Gavin

Member
Licensed User
Longtime User
I am trying to update a five year old project to use a Notification with Custom Sound.
I down loaded the example from this forum, changed the custom sound from "Shotgun.mp3", to an mp3 of my choosing, compiled and Success.

I then copied the subs "Notification_WithCustomSound" and "CreateFileProviderUri" to a Service Module in my application I named "Tracker" , as in the code below. I renaming the sub "Notification_WithCustomSound" to Simple_Notification_3.

B4X:
Sub Simple_Notification_2
    Dim n As NB6
    n.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(Smiley)
    n.OnlyAlertOnce(True)
    n.Build("Arriving at", Starter.strBody, "", Main).Notify(2) 'It will be Main (or any other activity) instead of Me if called from a service.
End Sub

Sub Simple_Notification_3
    Dim rp As RuntimePermissions
    Dim folder As String = rp.GetSafeDirDefaultExternal("shared")
    Dim FileName As String = "NOA.mp3"
    'copy the file to the shared folder
    File.Copy(File.DirAssets, FileName, folder, FileName)

    Dim n As NB6
    n.Initialize("custom sound", Application.LabelName, "DEFAULT")
    n.SmallIcon(LoadBitmapResize(File.DirAssets, "smiley.png", 32dip, 32dip, True))
 
    'disable the default sound
    n.SetDefaults(False, True, False)
    'set custom sound
    n.CustomSound(CreateFileProviderUri(folder, FileName))
    Dim Notification As Notification = n.Build("Arriving at", Starter.strBody, "", Main)
    Notification.Notify(3)
End Sub

Sub CreateFileProviderUri (Dir As String, FileName As String) As Object
    Dim FileProvider As JavaObject
    Dim context As JavaObject
    context.InitializeContext
    FileProvider.InitializeStatic("android.support.v4.content.FileProvider")
    Dim f As JavaObject
    f.InitializeNewInstance("java.io.File", Array(Dir, FileName))
    Return FileProvider.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))
End Sub

When I try to call Simple_Notification_3, (BTW, I know it is not a "Simple Notification") the Applications crashes on the final line of the CreateFileProviderUri sub.
If I call Simple_Notification_2, the application run as expected.

Originally, I was using the NB6 Module, but the application crashed when I updated to targetSdkVersion="33"
Changing to the Internal NB6 Library solved this problem but now the problem with Custom Sounds has arisen.

Oh, the joys of trying to update an old project.
Once again, any assistance greatly appreciated.

Gavin

PS
I am aware of the new Notification requirements for Sdk Version 33 and am presently trying to learn what is required but, I don't think this is the problem here.

Sorry, I forgot to include the Crash report.
B4X:
** Service (tracker) Start **
tracker_vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1 (java line: 161)
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at b4a.CheckList.tracker._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1(tracker.java:161)
    at b4a.CheckList.tracker._simple_notification_3(tracker.java:346)
    at b4a.CheckList.tracker._gps_locationchanged(tracker.java:197)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.gps.GPS$1.onLocationChanged(GPS.java:65)
    at android.location.LocationListener.onLocationChanged(LocationListener.java:63)
    at android.location.LocationManager$LocationListenerTransport$1.operate(LocationManager.java:3250)
    at android.location.LocationManager$LocationListenerTransport$1.operate(LocationManager.java:3247)
    at com.android.internal.listeners.ListenerExecutor.lambda$executeSafely$0(ListenerExecutor.java:127)
    at com.android.internal.listeners.ListenerExecutor$$ExternalSyntheticLambda0.run(Unknown Source:8)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8762)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
Caused by: java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority b4a.CheckList.provider
    at androidx.core.content.FileProvider.getFileProviderPathsMetaData(FileProvider.java:664)
    at androidx.core.content.FileProvider.parsePathStrategy(FileProvider.java:695)
    at androidx.core.content.FileProvider.getPathStrategy(FileProvider.java:645)
    at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:449)
    ... 22 more
 
Last edited:

Gavin

Member
Licensed User
Longtime User
I simply forgot to update the Manifest.
I have read this post before


but did not read it carefully enough.

Gavin
 
Upvote 0
Top