Play Sound when Onesignal notificacion received

Play Sound when Onesignal notificacion received

  

I need to Play a Sound on a Mobile App when it receives a Onesignal Notification, I tried to use the onesignal audio property but I'm not able to make it work.

So I'm trying to place somewhere in the app a Javascript to play sound but I don't know where the App runs when it's closed, seems that onesignal remains like a service to listen to the notification when received.

Any suggestion?

Hi.

You can use something like this.

ccaceres wrote:

I need to Play a Sound on a Mobile App when it receives a Onesignal Notification, I tried to use the onesignal audio property but I'm not able to make it work.

So I'm trying to place somewhere in the app a Javascript to play sound but I don't know where the App runs when it's closed, seems that onesignal remains like a service to listen to the notification when received.

Any suggestion?

Hi. Were you able to solve this requirement you described? Feel free to share it with the community or on the other hand, tell us if you have some further questions.

Cheers,

GM


Solution

Gonçalo Martins wrote:

ccaceres wrote:

I need to Play a Sound on a Mobile App when it receives a Onesignal Notification, I tried to use the onesignal audio property but I'm not able to make it work.

So I'm trying to place somewhere in the app a Javascript to play sound but I don't know where the App runs when it's closed, seems that onesignal remains like a service to listen to the notification when received.

Any suggestion?

Hi. Were you able to solve this requirement you described? Feel free to share it with the community or on the other hand, tell us if you have some further questions.

Cheers,

GM


Yes, I was able to solve it, what I did was to use the receive notification handler event of the OneSignal plugin, to be able to do this I made the following steps:

  • I added the PushNotification block to the layout.
  • Created a Client Action for the NotificationReceivedEvent
  • In the Client Action I have the following:
  • Now the Plugin Appers on every Screen so I have another Client Action that contains the sound script.
  • The sound Script contains the following:
    var audio = new Audio('/ProyectName/platforms/android/res/raw/beep.mp3');
    audio.play();
  • I made step 4 for each screen in the App and now when the OnseSignal receives a Notification the App plays the Sound.

Now I’m working on applying the Background Mode to avoid on closing the app.

I hope this can be helpful.


Solution

ccaceres wrote:

Gonçalo Martins wrote:

ccaceres wrote:

I need to Play a Sound on a Mobile App when it receives a Onesignal Notification, I tried to use the onesignal audio property but I'm not able to make it work.

So I'm trying to place somewhere in the app a Javascript to play sound but I don't know where the App runs when it's closed, seems that onesignal remains like a service to listen to the notification when received.

Any suggestion?

Hi. Were you able to solve this requirement you described? Feel free to share it with the community or on the other hand, tell us if you have some further questions.

Cheers,

GM


Yes, I was able to solve it, what I did was to use the receive notification handler event of the OneSignal plugin, to be able to do this I made the following steps:

  • I added the PushNotification block to the layout.
  • Created a Client Action for the NotificationReceivedEvent
  • In the Client Action I have the following:
  • Now the Plugin Appers on every Screen so I have another Client Action that contains the sound script.
  • The sound Script contains the following:
    var audio = new Audio('/ProyectName/platforms/android/res/raw/beep.mp3');
    audio.play();
  • I made step 4 for each screen in the App and now when the OnseSignal receives a Notification the App plays the Sound.

Now I’m working on applying the Background Mode to avoid on closing the app.

I hope this can be helpful.


Great explanation.

Also, I'm glad to see that my post about playing sound was useful since you're using it ;)

Cheers and thank you for sharing this with the community.


Gonçalo Martins wrote:

ccaceres wrote:

Gonçalo Martins wrote:

ccaceres wrote:

I need to Play a Sound on a Mobile App when it receives a Onesignal Notification, I tried to use the onesignal audio property but I'm not able to make it work.

So I'm trying to place somewhere in the app a Javascript to play sound but I don't know where the App runs when it's closed, seems that onesignal remains like a service to listen to the notification when received.

Any suggestion?

Hi. Were you able to solve this requirement you described? Feel free to share it with the community or on the other hand, tell us if you have some further questions.

Cheers,

GM


Yes, I was able to solve it, what I did was to use the receive notification handler event of the OneSignal plugin, to be able to do this I made the following steps:

  • I added the PushNotification block to the layout.
  • Created a Client Action for the NotificationReceivedEvent
  • In the Client Action I have the following:
  • Now the Plugin Appers on every Screen so I have another Client Action that contains the sound script.
  • The sound Script contains the following:
    var audio = new Audio('/ProyectName/platforms/android/res/raw/beep.mp3');
    audio.play();
  • I made step 4 for each screen in the App and now when the OnseSignal receives a Notification the App plays the Sound.

Now I’m working on applying the Background Mode to avoid on closing the app.

I hope this can be helpful.


Great explanation.

Also, I'm glad to see that my post about playing sound was useful since you're using it ;)

Cheers and thank you for sharing this with the community.


That's right it was very helpful.

Thanks..!!!


Hi,

We too want to play a sound on a mobile phone when a OneSignal notification is received. I mean, literally, at the moment the notification is received. I have tried the above, but that only has the sound played when the app is already active, which is not a complete solution for what we want.

When I look at the structure of the Notification that is received, under Payload there is a Sound attribute, but how this is to be used is not clear. Also unclear is how to set this attribute from a web application. In the OneSignalAPI there is a SendPushNotificationToDevice action, but no way to set a Sound attribute as far as I can tell.

Anyone have any tips on how to set these attributes and have a sound played on notification received, even when the app is not active?

Thanks!