Android Question Notifications Builder and SDK 26

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello,

I'm trying to run an app with sdk 26 and Android V 7. The same app that runs in android V6 with the sdk 26 is showing the error:

android.os.FileUriExposedException: file:///storage/emulated/0/notification/msgting.mpeg exposed beyond app through Notification.sound

I use notificationbuilder lib from @barx . The code is:
B4X:
                                    Dim SoundFile As String = "file:///sdcard/notification/msgting.mpeg"

         Nb.SmallIcon = "notification_whatpro"
                                   
          If File.Exists(File.DirInternal,UserImage) Then
                                        Nb.LargeIcon = NativeMe.RunMethod("getRoundBitmap",Array(LoadBitmap(File.DirInternal,UserImage)))
                                    Else
                                        Nb.LargeIcon = NativeMe.RunMethod("getRoundBitmap",Array(LoadBitmap(File.DirAssets,"ic_person_black_48dp.png")))
                                    End If
           
                                    Nb.ContentTitle = "Nova Mensagem"
           
                                    Nb.ContentText =  NotifyText
                                    Nb.SubText = "Clique aqui para visualizar "
           
                                    Nb.CustomSound = SoundFile
                                    Nb.setActivity(chats)
                                    Nb.DefaultVibrate = True
                                    Nb.OnGoingEvent = False
                                    Nb.AutoCancel = True
                                    Nb.Priority = 100
                                   
                                    Nb.Notify(20)

The error occurs in Nb.Notify(20), #ONLY# with Android 7 AND Sdk 26 (Android 6.x and SDK 26 OR Android 7 and SDK 21 runs normally).

How can I fix this?
 

DonManfred

Expert
Licensed User
Longtime User
Check tsrget sdk thread from erel
Setup you app to use fileprovider
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
It is indeed to do with the uri, try passing a uri created with fileprovider as suggested. Hopefully it will just work ;)
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
It is indeed to do with the uri, try passing a uri created with fileprovider as suggested. Hopefully it will just work ;)
Hi @barx , thanks for answering ! I have a set of files created by app came from dirassets . Then, I copy to an external folder (with the corrected and previously granted rights) and play the correct for the event that I want to notify user.
Then, I don't have the uri. I have the file name and path. I'm trying some solutions to get the URI from filename but without success up to now. Could u help me?
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
It is indeed to do with the uri, try passing a uri created with fileprovider as suggested. Hopefully it will just work ;)
Hello @barx . I did a small code to test the uri filesource with Notifications Builder... this is not working... see:

B4X:
#Region  Project Attributes
    #ApplicationLabel: File Provider Test
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #AdditionalJar: com.android.support:support-v4
   
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Rp As RuntimePermissions
    Dim SharedFolder As String

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
    Private Label1 As Label
    Dim I As Intent
    Dim Nb As NotificationBuilder
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("layout1")

    SharedFolder = Rp.GetSafeDirDefaultExternal("notification")

    File.Copy(File.DirAssets,"msgting.mpeg",SharedFolder,"msgting.mpeg")
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
   
    Dim Uri As String
    Dim Dir As String = File.DirRootExternal
    Uri = CreateFileProviderUri(SharedFolder ,"msgting.mpeg")
    Label1.Text= Uri
   
    Nb.Initialize
    Nb.CustomSound = Uri
    'Nb.DefaultSound = True
    Nb.Notify(10)
   
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

The manifest editor is correctly configured. The method FileProvider.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f)) is returning:

content://com.fileprovidertest.provider/notification/msgting.mpeg (correct!)

The file msting.mpeg is played normally with notifications builder in external dir reference file://sdcard/notification/msgting.mpeg (which doesn't work anymore with Android 7 and sdk 26)...

I mean, all the the code seems to be running fine, but the lib NotificationsBuilder (version 3.0) doesn't play the customnotification sound. Can you help me?
 
Last edited:
Upvote 0
Top