Help with retrieving recorded audio file from system.DocumentsDirectory

Hi there!  I need help with retrieving a recorded audio file from system.DocumentsDirectory in my code.  I record an audio file in corona, then I save it to system.DocumentsDirectory.  I need to access this audio file within a OneSignal push notification Post.  

The audio is recorded and plays back fine.  But when the push notification is called when i press btn3, the notification appears but without the recorded audio file that should play.

All help is appreciated thanks!

code below: 

[lua] 

– Your code here

local OneSignal = require(“plugin.OneSignal”)

– This function gets called when the user opens a notification or one is received when the app is open and active.

– Change the code below to fit your app’s needs.

function DidReceiveRemoteNotification(message, additionalData, isActive)

    if (additionalData) then

        if (additionalData.discount) then

            native.showAlert( “Discount!”, message, { “OK” } )

            – Take user to your app store

        elseif (additionalData.actionSelected) then – Interactive notification button pressed

            native.showAlert(“Button Pressed!”, “ButtonID:” … additionalData.actionSelected, { “OK”} )

        end

    else

        native.showAlert(“OneSignal Message”, message, { “OK” } )

    end

end

OneSignal.Init(“MY ONE SIGNAL ID GOES HERE”, “############”, DidReceiveRemoteNotification)

– Creates a notification to be deliver to this device as a test.

function IdsAvailable(userID, pushToken)

    if (pushToken) then

        local notification = {

            [“contents”] = {[“en”] = “This is a Test notification! HI MAX!!”, [“ios_sound”] = “newRecording.aif”}

    

        }

        

        notification[“include_player_ids”] = {userID}

        

        OneSignal.PostNotification(notification)

        

        

    end

end

local bg = display.newRect(display.contentCenterX, display.contentCenterY, 1000, 1000)

local btn1 = display.newCircle(display.contentCenterX, display.contentCenterY, 50)

btn1:setFillColor(1, 0, 0)

local btn2 = display.newCircle(display.contentCenterX, display.contentCenterY - 150, 50)

btn2:setFillColor(0, 1, 0)

local btn3 = display.newCircle(display.contentCenterX, display.contentCenterY + 150, 50)

btn3:setFillColor(0, 0, 1)

local function startRecording(event)

if (event.phase == “began”) then 

local filePath = system.pathForFile( “newRecording.aif”, system.DocumentsDirectory )

local recording = media.newRecording( filePath )

recording:startRecording()

end

end

local function stopRecording(event)

if (event.phase == “began”) then 

local filePath = system.pathForFile( “newRecording.aif”, system.DocumentsDirectory)

local recording = media.newRecording( filePath )

recording:stopRecording()

local sound = audio.loadSound(“newRecording.aif”, system.DocumentsDirectory )

audio.play(sound)

end

end

local function sendRecording(event)

if (event.phase == “began”) then

OneSignal.IdsAvailableCallback(IdsAvailable)

end

end

btn3:addEventListener(“touch”, sendRecording)

btn2:addEventListener(“touch”, startRecording )

btn1:addEventListener(“touch”, stopRecording ) [/lua]

When a notification is received while your app is active, the code will execute DidReceiveRemoteNotification immediately, so you can manually play a sound there instead of just calling native.showAlert as it does now.

To have the sound play for notifications that are received while the app is not active, it should work if you specify the full path to the sound file. Try using system.pathForFile( “newRecording.aif”, system.DocumentsDirectory) as the value for the ios_sound option.

@george18

I need the sound to play when the notification is received when the app is not active. 

I tried your suggestion and didn’t work… the notification is received but the standard/default sound is played.  Meaning, it is not working however, the audio file IS in the correct location in the sandbox.  Let me know! Thanks!

-Max

@george18

my original code runs fine without errors.  However, am I formatting my code correctly for the notification sound?

If you could make sure and verify that is how the code should be implemented that would be awesome too! Thanks.

@george18

BUMP

Hello,

You can try to use the following but iOS may only allow sound files for notifications that bundled with your app.

/Documents/newRecording.aif

Thanks.

When a notification is received while your app is active, the code will execute DidReceiveRemoteNotification immediately, so you can manually play a sound there instead of just calling native.showAlert as it does now.

To have the sound play for notifications that are received while the app is not active, it should work if you specify the full path to the sound file. Try using system.pathForFile( “newRecording.aif”, system.DocumentsDirectory) as the value for the ios_sound option.

@george18

I need the sound to play when the notification is received when the app is not active. 

I tried your suggestion and didn’t work… the notification is received but the standard/default sound is played.  Meaning, it is not working however, the audio file IS in the correct location in the sandbox.  Let me know! Thanks!

-Max

@george18

my original code runs fine without errors.  However, am I formatting my code correctly for the notification sound?

If you could make sure and verify that is how the code should be implemented that would be awesome too! Thanks.

@george18

BUMP

Hello,

You can try to use the following but iOS may only allow sound files for notifications that bundled with your app.

/Documents/newRecording.aif

Thanks.