Closed CrixuAMG closed 5 years ago
Hey @CrixuAMG,
So this sound needs to be relative to the actual native project. If it's in your web content it won't work.
Try putting the sound in your android project in the res/raw
folder in your android project, and then pass
'android.resource://com.yourapp.whatever/raw/mysound.mp3'
Replacing the com.yourapp.whatever
and mysound.mp3
with the proper values.
Let us know if that works! More discussion here: https://stackoverflow.com/questions/48986856/android-notification-setsound-is-not-working
Linking with #1530 to better document this stuff.
@mlynch thank you for replying!
Would the same fix also apply for iOS devices?
Hello @CrixuAMG! I still can't make the sound work. I tried to do it that way
Can you help me? Thank you very much in advance!
@lucascardoso @mlynch
I finally fixed the problem. It was actually very simple to do, just had to check the iOS directory...
This is the fix:
sound: './public/assets/sounds/sound.aiff',
Works on at least iOS 12 and 13.1 beta 2
I placed sound file in android\app\src\main\res\raw
and have below settings for LocalNotifications.schedule
:
LocalNotifications.schedule({
notifications: [
{
title: data['title'],
body: data['body'],
id: 1,
schedule: { at: new Date(Date.now() + 1000 * 5) },
sound: 'android.resource://com.mypackagename/raw/pop_drip.wav',
smallIcon:'res://icon_silhouette',
actionTypeId: "",
extra: null,
attachments: null
}
]
});
But its not working, its still playing default sound.
@naveedahmed1 Did you ever get the sound to work? Having the same issue currently.
No
same problem here, any fix ?
Hey guys after long time testing, I have figured it out using the capacitor local notifications on any version of Android. **To get the sound working you must create a channel*****
First you need to put the sound.mp3 file that you have inside the raw folder located in the res folder of Android files.
Second you need to create a channel using the plugin and put in the right values, for (sound: 'sound.mp3'). Then schedule a notification with the basic values but don't include the sound param when you schedule the notification. Examples below
//// Creating channel example ///// const channel1: NotificationChannel = { id: 'mychannel', name: 'channel', importance: 5, sound: 'sound.mp3', visibility: 1,
}
Important NotificationChannel from capacitor plugins
Plugins.LocalNotification.createchannel(channel1)
//// Scheduling notifications ///// Plugins.LocalNotification.schedule({ notifications { title: 'example', id: 1, channel: 'mychannel' (channel id from above) body: 'example', trigger: 'example'
} })
Third inside the capacitor.config.json file put Plugins { LocalNotification { "smallicon": "icon" "Iconcolor": "#22222# "sound": "sound.mp3" } }
That's all after that it will be working. Once your notification triggers it will play the custom sound you assigned. If you get confused let me know. Here is the video you can follow to help you understand better. https://youtu.be/bww4a4B43tM
Thanks @moderntechllc
The actual position in the video is this: https://youtu.be/bww4a4B43tM?t=466
I have fixed this issue by creating first a channel:
The 'slap.wav' file is stored in android\app\src\main\res\raw\
folder
Then, I schedule my notification without speciying the sound path:
PS: I did not do any configuration on capcitor.config.json
It works perfectly on Samsung Galaxy A71 (SM-A715F) - Android 10
If you want to change dynamically the notification sound, you delete this channel and create another one with only a different id
Fixed the issue by creating the channel. But channel is only supporting in android. Here is my solution: constructor() { LocalNotifications.createChannel({ id: 'CurrencyAlerts', name: 'CurrencyAlerts', importance: 5, description: 'Currency alerts', sound: 'notification.mp3', visibility: 1, vibration: true, }); } You can create this channel in the service's constructor maybe...
And then use this channel's ID to trigger the notification like this: async alert(title, body) { const randomId = Date.now() + 69;
return await LocalNotifications.schedule({
notifications: [
{
title: title,
body: body,
id: randomId,
schedule: { at: new Date(Date.now() + 500) },
// sound: null,
attachments: null,
actionTypeId: '',
extra: null,
channelId: 'CurrencyAlerts',
},
],
});
} So here, You will get the unique ID for every alert. DONT USE THE SOUND in the schedule options and schedule it after 0.5secs of target date-time.
I tried all above ways. Still sound is not coming. I even tried cordova plugin. Idk what's the issue.
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.
I've looked at the current documentation for local notification, and while it does display which methods and options are available, it does not explain what type can be used for each option. What I'm trying to do is use a custom notification sound, but I haven't got it working yet. The sound is located at [ROOT_PROJECT]/public/assets/sound/sound.aiff (for iOS), and trying to load in the sound using the snippit below.
I've also tried applying
file://
in front of it (got this from ionic), and using a direct path. I know that the sound exists at that path, and that it can be played, but I have no clue what needs to be done to make it play that sound.If anyone has an idea on how to do it, please let me know!