Skip to content

Support for custom sounds on Android #301

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

Closed
wants to merge 1 commit into from
Closed

Support for custom sounds on Android #301

wants to merge 1 commit into from

Conversation

oliversalzburg
Copy link

To make use of this, the sound must be placed in the platforms/android/res/raw folder and must be of one of the supported media types (http://developer.android.com/guide/appendix/media-formats.html).

In the push notification payload, specify the sound without the file extension. If your file is named "platforms/android/res/raw/alert.mp3", use "alert" as the name.

@bryanhiggins
Copy link

Is it not possible to have the sound relative to www? Ideally users should not have to update files within the platforms directory.

@oliversalzburg
Copy link
Author

I agree, yet I found myself having to modify contents of platforms on several occasions, like when including the push notification resources for iOS or Android application icons and splash screens (if this was a mistake on my part, I'd be happy to learn about the alternatives).

Regardless, I would prefer to use a resource under www myself, so I'll look into that.

@alexislg2
Copy link

Hey. Any news? I'ms very interested by the possibility to put the soundfile in the www folder

@oliversalzburg
Copy link
Author

Playing a file that is located in the www seems a lot more complicated. setSound requires a Uri, but you can't provide one to access an asset directly. You can only request a stream, using AssetManager.

The "normal" way to supply a custom notification sound is to provide one that resides in the resources of your package, which is what this change implements and which is what we're using.

I guess you could write the asset from your package to another location and then provide a Uri to that location to have a custom sound from your assets play. But that didn't seem like it was worth the effort.

@alexislg2
Copy link

OK thanks I understand
So you're saying you have to put the .mp3 file in the /platforms/android/res/raw/alert.mp3 and call it with payload: {sound: "alert", message"blabla"}
But then if the app is in foreground, it will try to play file alert (using Media plugin) which does not exists.
I guess we should change the field name in the payload because sound is already used for foreground notifications and this field requires file extension

@oliversalzburg
Copy link
Author

@alexislg2 Could you point me towards the relevant documentation sections regarding this?

@alexislg2
Copy link

I'm sorry I made a mistake. It's just in my own JS code for handling payload while in foreground
that I took from here
https://github.com/phonegap-build/PushPlugin/blob/master/Example/www/index.html

So please don't consider my last comment!

@alexislg2
Copy link

BTW I had to remove the setDefaults(Notification.DEFAULT_ALL) line to make it work
Thank you

@oliversalzburg
Copy link
Author

@alexislg2 Where did you remove it from? PushPlugin sets the defaults according to the default member in the payload. But, you're right, you can't use DEFAULT_ALL, I ran into exactly the same problem ;) http://stackoverflow.com/questions/25566215/notification-builder-setsound-has-no-effect/25566391

@alexislg2
Copy link

From here !
https://github.com/phonegap-build/PushPlugin/blob/master/src/android/com/plugin/gcm/GCMIntentService.java#L103
I spent 2 hours trying to figure out why your PR was not working ;)

The best way to do that would be to use DEFAULT_SOUND if no payload sound has been provided or if no ressource corresponds, otherwise use the payload sound

@oliversalzburg
Copy link
Author

Ah, yeah, I see it now. We specifically set defaults to 0 before we construct our payload on the server side. It's quite the pitfall indeed.

@lyonplus
Copy link

I tried this PR with PushPlugin/1979d972 and cordova 3.6.3-0.2.13, on Nexus S with 4.2.1 OS, using the codes from PushPlugin/Example, with following changes:

  • Change senderID in www/index.html;
  • Change API Key and regID in pushGCM.rb;
  • Replace phonegap.js with cordova.js in www/index.html.

What I got is with or without the patch:
when the test app is in foreground, I can hear the sound of beep.wav;
when the test app is in background, what I hear is default alert sound (not beep.wav);

I tried copy the beep.wav to platform/res/raw/beep.wav, but it didn't help.

I also tried change the defaults from Notification.DEFAULT_ALL to zero in GCMIntentService.java, it just mutes the sound when the app is in background. When the app is in foreground, I got the same sound of beep.wav.

And I tried changing "beep.wav" to "beep" in pushGCM.rb, it will then mute the sound when the app is in foreground.

So it seems this patch doesn't change anything to me, or I did something wrong?

@oliversalzburg
Copy link
Author

@lyonplus I can assure you that the patch has an effect as we're using it successfully in production.

I've never used any of the examples, so I can't tell you where the problem with those might be. You definitely must not set DEFAULT_ALL (this is best done through the push payload), you must supply the filename without extension and the file must be placed in the platform/res/raw folder.

@lyonplus
Copy link

@oliversalzburg Thanks for confirm. Could you please tell me what is the difference you see in your environment? Like before your patch it is always the default alert sound and with your patch it is your custom sound, both foreground and background?

@oliversalzburg
Copy link
Author

@lyonplus When we're in the foreground, we don't get any sound, because no notification is created. This is the default behavior of PushPlugin. (for reference)

Our patch allows you to specify a custom sound. And that sound will be played whenever PushPlugin would play a sound.

@alexislg2
Copy link

If you want foreground notifications you have to use the Media plugin to
play the sound you want, as in the example
For background notification don't forget to put it in the /res/raw folder
and call it in the payload without file extension
On Oct 24, 2014 10:40 AM, "Oliver Salzburg" [email protected]
wrote:

@lyonplus https://github.com/lyonplus When we're in the foreground, we
don't get any sound, because no notification is created. This is the
default behavior of PushPlugin. (for reference
https://github.com/phonegap-build/PushPlugin/blob/master/src/android/com/plugin/gcm/GCMIntentService.java#L67
)

Our patch allows you to specify a custom sound. And that sound will be
played whenever PushPlugin would play a sound.

Reply to this email directly or view it on GitHub
#301 (comment)
.

@lyonplus
Copy link

@oliversalzburg: With patch like below, I am now able to get the custom notification sound for both foreground and background.

diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/
index d082fae..e433eee 100644
--- a/src/android/com/plugin/gcm/GCMIntentService.java
+++ b/src/android/com/plugin/gcm/GCMIntentService.java
@@ -124,9 +124,9 @@ public class GCMIntentService extends GCMBaseIntentService {

            String soundName = extras.getString("sound");
            if (soundName != null) {
            -       Resources r = getResources();
            -       int resourceId = r.getIdentifier(soundName, "raw", context.getPacka
            -       Uri soundUri = Uri.parse("android.resource://" + context.getPackage
            +       Uri soundUri = Uri.parse("android.resource://" + context.getPackage
            +       defaults &= ~Notification.DEFAULT_SOUND;
            +       mBuilder.setDefaults(defaults);
                    mBuilder.setSound(soundUri);
            }

@oliversalzburg
Copy link
Author

@lyonplus That change isn't necessary. You can already adjust the defaults through the push payload.
That being said, I guess the change also doesn't hurt.

@andreapozzetti
Copy link

@oliversalzburg
Hi,
i change the java file as you made in the file attached.
In push payload i set "sound" with "mysound" without the extension
but it seems not work neither in background neither in foreground

what i'm wrong?

@oliversalzburg
Copy link
Author

@andreapozzetti

  1. Make sure you've put the sound file in the correct folder: platforms/android/res/raw/
  2. Make sure it's a support file format: http://developer.android.com/guide/appendix/media-formats.html
  3. Make sure to take care of the defaults property as discussed in this thread.
  4. Make sure you have the volume up on the device ;)

@zbranzov
Copy link

Hi,
I had to remove this line of code https://github.com/hartwig-cordova/PushPlugin/blob/android-sound/src/android/com/plugin/gcm/GCMIntentService.java#L105 before make it works in background mode. Tested on HTC One 4.4.3 with AppBuilder project. I am not familiar with Java development and plugin in depth but it did the trick. Although I have no explanation why.

@oliversalzburg
Copy link
Author

@zbranzov You don't have to remove the line, just supply defaults in your push payload when you also supply a custom notification sound.

@bostondv
Copy link

bostondv commented May 7, 2015

@oliversalzburg Thanks for this patch! I've has success using it in my project.

One thing though, have you ever experienced the default notification sound playing alongside the custom sound?

I set defaults to 0, and sometimes the notifications come in with just the custom sound, other times both sounds play. Looking at logcat it seems like the notification is being received multiple times...

@oliversalzburg
Copy link
Author

@bostondv No, we're not experiencing that. We're using custom notification sounds exclusively and we never get the default sound additionally.

Maybe try not setting a sound at all, and see if the default sound still appears. It might be another part of the application causing the sound.

@bostondv
Copy link

bostondv commented May 8, 2015

Thanks, have tried that actually. I'm able to get sound to go away entirely
with setting "defaults" to 2 or 4 (vibrate and lights). To get my sound to
play defaults has to be 0, though that's when the default sound plays
sometimes as well.
On Fri, May 8, 2015 at 4:22 AM Oliver Salzburg [email protected]
wrote:

@bostondv https://github.com/bostondv No, we're not experiencing that.
We're using custom notification sounds exclusively and we never get the
default sound additionally.

Maybe try not setting a sound at all, and see if the default sound still
appears. It might be another part of the application causing the sound.

Reply to this email directly or view it on GitHub
#301 (comment)
.

@nurbed
Copy link

nurbed commented May 14, 2015

Sorry, but the solution of @oliversalzburg don't work for me...
In my case:

  1. the sound is on res/raw folder
  2. the push notification server, make a payload with sound: "filename" and default:"6" (4 + 2: DEFAULT_LIGHTS + DEFAULT_VIBRATE)
  3. with the app on background, on notification, the device vibrates, but I do not hear any sound

A simple question: in the createNotification function no one makes the call to setSound of mBuilder
The android documentation for NotificationCompat.Builder says:
public NotificationCompat.Builder setSound (Uri sound)
Set the sound to play. It will play on the default stream.

(http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html)

This may be the error?
Many thanks in advance

@miparnisari
Copy link

miparnisari commented Jul 7, 2016

Hi, what's the status of this? Why isn't it merged? I placed the notification sounds in platforms/android/res/raw but I'm still not hearing anything 👎 I read somewhere that Ionic only has access to the www folder, should I place the sounds in www/res/raw?

@animista01
Copy link

@miparnisari For Android you need to create a folder inside platform/android/res/ directory and called it "raw" and put there the sound file, the audio file must be .wav, and in Ionic.io json: {"badge" => 1, "sound" => "savvynotification"} notice that it doesn't need the extension name.

@miparnisari
Copy link

Why wav? I saved it as mp3 and it works on my android. Is it for older
devices?

Also, I'm already sending the "badge" property (through node-gcm) but it
doesn't seem to be doing anything..

-- Maria Ines
On Jul 8, 2016 6:50 PM, "Bryan Villafañe" [email protected] wrote:

@miparnisari https://github.com/miparnisari For Android you need to
create a folder inside platform/android/res/ directory and called it "raw"
and put there the sound file, the audio file must be .wav, and in Ionic.io
json: {"badge" => 1, "sound" => "savvynotification"} notice that it doesn't
need the extension name.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#301 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AFIDpw9K-XEhqghf2kQXExSyvrcEmOXtks5qTsY0gaJpZM4CclV-
.

@oliversalzburg oliversalzburg deleted the android-sound branch June 26, 2018 14:10
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

Successfully merging this pull request may close these issues.

10 participants