ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
12.04k stars 999 forks source link

Local notification sounds #1146

Closed CrixuAMG closed 5 years ago

CrixuAMG commented 5 years ago

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.

import { Plugins } from '@capacitor/core';
const { LocalNotifications } = Plugins;

LocalNotifications.schedule({
  notifications: [
    {
      title: "Title",
      body: "Body",
      id: 1,
      schedule: { at: new Date(Date.now() + 1000 * 5) },
      sound: 'assets/sounds/sound.aiff',
      attachments: null,
      actionTypeId: "",
      extra: null
    }
  ]
});

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!

mlynch commented 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.

CrixuAMG commented 5 years ago

@mlynch thank you for replying!

Would the same fix also apply for iOS devices?

lucascardoso commented 5 years ago

Hello @CrixuAMG! I still can't make the sound work. I tried to do it that way

imports notification

Can you help me? Thank you very much in advance!

CrixuAMG commented 5 years ago

@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

naveedahmed1 commented 5 years ago

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.

zlannin commented 4 years ago

@naveedahmed1 Did you ever get the sound to work? Having the same issue currently.

naveedahmed1 commented 4 years ago

No

Faroukymedia commented 4 years ago

same problem here, any fix ?

modernappsllc commented 4 years ago

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

bitflower commented 4 years ago

Thanks @moderntechllc

The actual position in the video is this: https://youtu.be/bww4a4B43tM?t=466

Maraaghib commented 3 years ago

I have fixed this issue by creating first a channel:

image The 'slap.wav' file is stored in android\app\src\main\res\raw\ folder

Then, I schedule my notification without speciying the sound path:

image

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

KumarShubham001 commented 3 years ago

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.

mayankkataria commented 2 years ago

I tried all above ways. Still sound is not coming. I even tried cordova plugin. Idk what's the issue.

ionitron-bot[bot] commented 1 year ago

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.