Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play a custom sound for notification #37

Open
varungupta85 opened this issue Apr 24, 2016 · 4 comments
Open

Play a custom sound for notification #37

varungupta85 opened this issue Apr 24, 2016 · 4 comments

Comments

@varungupta85
Copy link
Contributor

I want to play a custom sound for a local notification. What do I need to put in the sound field? I tried the following:

Notification.create({
    subject: 'Scheduled Notification',
    message: 'Alarm triggered',
    sendAt: new Date(alarmDate),
    sound: 'alarm_1.m4a'
});

where alarm_1.m4a is an audio file that is present at <PROJECT-DIR>/android/app/src/main/res/raw/alarm_1.m4a but it still plays the default notification sound.

@zakelfassi
Copy link

zakelfassi commented Apr 25, 2016

The Notification sound should either be a constant value (default system notification sound == 1), or a URI. In your case, that would probably translate to 'android.resource://MY.PACKAGE.NAME/raw/alarm_1' OR 'android.resource://MY.PACKAGE.NAME/raw/alarm_1.m4a'.

Haven't tried this but pretty certain it should work.

@varungupta85
Copy link
Contributor Author

varungupta85 commented Apr 26, 2016

I tried using the URI as @zakelfassi instructed but I still didn't hear the custom sound. This is the usage

Notification.create({
    subject: 'Scheduled Notification',
    message: 'Galarm triggered',
    sendAt: new Date(alarmDate),
    sound: 'android.resource://com.galarmprototype1/raw/alarm_1.m4a'
  });

I also tried without the file extension also. Below is the file structure:
android_package_structure
So, I think the package name is com.galarmprototype1

I also checked the code and I actually didn't find any code that would play the custom sound file. Specifically, I checked the file /Users/abc/Projects/GalarmPrototype1/node_modules/react-native-system-notification/android/src/main/java/io/neson/react-native/notification/Notification.java. There is a Notification builder method in that class where the sound attribute is only used to check if it is equal to default and if it is, then the default notification sound is enabled.

Could someone please confirm if that is the case that the custom sound files are not supported?

I checked the android documentation and it seems that I need to call the .setSound method with the sound file URI in order to play the default sound. I will try to change the code and test it.

@zakelfassi
Copy link

The line defaults = defaults | android.app.Notification.DEFAULT_SOUND; is more like "set to default if the sound attribute isn't set."

Do you still hear the default sound or nothing at all now? (I think after adding files to raw/ you should rerun react-native run-android to be able to use it.) Also - think of running adb logcat; you should get a system warning/error.

@varungupta85
Copy link
Contributor Author

@zakelfassi I didn't hear any sound at all. I added the following lines of code in the Notification.java class

if (attributes.sound != null) {
  notificationBuilder.setSound(Uri.parse(attributes.sound));
}

After this, I was able to hear the custom sound as expected. The sound file URI shouldn't contain the extension or else the sound doesn't play.

I am not sure if this is all that one needs to play the custom sound or we need to handle any special cases here as well. I am just starting up with mobile development. For e.g. I don't know if the inboxStyle attribute makes a difference to whether we play the sound or not. If this is all that is needed, I can send a pull request if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants