zetavg / react-native-system-notification

Android system notifications for React Native. Supports push notifications with GCM integrated.
https://www.npmjs.com/package/react-native-system-notification
244 stars 102 forks source link

Play a custom sound for notification #37

Open varungupta85 opened 8 years ago

varungupta85 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

@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.