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

Notification with Custom Sound (Android/iOS) #31

Open
luigi37 opened this issue Dec 2, 2017 · 9 comments
Open

Notification with Custom Sound (Android/iOS) #31

luigi37 opened this issue Dec 2, 2017 · 9 comments

Comments

@luigi37
Copy link

luigi37 commented Dec 2, 2017

Hi,
in Android we can select the type of sound of the notification as long as we have an mp3 file associated in the build.
It seems that in iOS is not possible and always the std notification sound is issued.
Any chance to change it?

As a workaround, if not possible, I noticed that some iOS app do have a specific Notification SOUND option enabled: instead of just sound on/off you can also choose which sound you want which for me would be good enough...

Any idea on how to enable this behaviour in the app?

Thanks

Luigi

@luigi37
Copy link
Author

luigi37 commented Dec 3, 2017

Maybe a stupid idea but... is the App waken up on receipt of notification? (I think yes for few seconds but not sure with which method). Would be possible to use this time frame to play a sound??

https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH10-SW1

Configuring a Background Update Notification

Background update notifications improve the user experience by giving you a way to wake up your app periodically so that it can refresh its data in the background. When apps do not run for extended periods of time, their data can become outdated. When the user finally launches the app again, the outdated data must be replaced, which can cause a delay in using the app. A background update notification can alert the user or it can occur silently.

@chrisjpalmer
Copy link
Collaborator

There is a feature of iOS which allows the application to be temporarily activated when a new notification comes in. This is called Remote Notifications. However we have had no success getting it work with this plugin. Moreover it would not allow you to play a sound.

If I get some time this week, I will look into this one for you.

Thanks

@luigi37
Copy link
Author

luigi37 commented Dec 4, 2017

Cool thanks! Found something here phonegap/phonegap-plugin-push#17 about putting caf sound file in platform/iOS/ but had no chance to try it yet...

@luigi37
Copy link
Author

luigi37 commented Dec 9, 2017

Hi all, I succeded in setting a custom notification soud in both Android and iOS

1- ~~~You must use a .caf file or a .wav file~~~ You can use a .caf file or .wav file or (at least on iOS 11 + Xcode 9.2 it works) a .mp3!

  • Caf file you can find converter online (or do with Mac if you have one :-) )
  • Wav file in linux is enough to do
    ffmpeg -i alarm.mp3 alarm.wav
    They are similar in size and both very large compared to .mp3

2- I found here:
https://documentation.onesignal.com/v3.0/docs/customize-notification-sounds
a great hint about where to put the audio files
https://files.readme.io/Zn35Nuk8SAeADiXyRaTA_Cordova%20iOS%20custom%20notifiction%20sound.png
it needs to be inside
/home/user/MyCordovaApp/platforms/ios/MyCordovaApp/Resources/

It seems you need to add the files manually in Xcode (but unless you remove the ios platform the audio files should stay there).
When adding into XCode, **Resources** folder I used the options:
- Copy items if needed
- Create folder references
- Add to targets: "MyCordovaApp"

NOTE: according Apple doc, there are also other possible locations but I could not figure out them in relation to Cordova.
NOTE: For Android, it's enough an hook in "after platform add" which copies the mp3 files into
`platforms/android/res/raw/`

3- The sound needs to be specified inside the "notification" side of the payload BUT differently from Android, **you do need to add extension**:
`curl -X POST --header "Authorization: key=MYKEY123"   --Header "Content-Type: application/json"  https://fcm.googleapis.com/fcm/send     -d "{\"to\":\"USERTOKEN123\",\"notification\":{\"body\":\"Yellow\",\"sound\":\"mysound.caf\"},\"priority\":10}"`

It's to be noted that Android works EVEN if the extension is omitted BUT for iOS it's mandatory.

NOTE: 
1- If the sound file is not found, the notification calls the default sound (which is a good failsafe)
2- Vibration seems to be on by default (not sure if it can be removed)
3- If someone wants a silent notification she/he can either remove "sound:" or create a silent audio file
**4- Can anyone test and report if mp3 works also for ios 10, 9 and below?**
~~~**5- Thanks if anyone could suggest the method to avoid manually adding into resource folder the audio files**~~~

Found a nice hint here:
https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/INSTALLATION.md
Just put in your root directory (the directory from which you issue "cordova prepare") the file you want (eg: test.mp3) and then add to config.xml:
```
<platform name="android">
        <resource-file src="test.mp3" target="res/raw/test.mp3" />
    </platform>
    <platform name="ios">
        <resource-file src="test.mp3" />
    </platform>
```
I confirm it gets properly added to Android. In iOS I can see the file appearing but not sure if visible inside XCode project. Thanks for checking and reporting.

Worthwhile to note that the same method could be used for the notification icon sent to Android with the payload

@luigi37 luigi37 changed the title Sound choice Notification with Custom Sound (Android/iOS) Dec 9, 2017
@luigi37 luigi37 closed this as completed Dec 9, 2017
@luigi37 luigi37 reopened this Dec 9, 2017
@luigi37
Copy link
Author

luigi37 commented Dec 9, 2017

Interesting enough that Apple documentation contradicts what I saw about mp3...

https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/SupportingNotificationsinYourApp.html#//apple_ref/doc/uid/TP40008194-CH4-SW10

Preparing Custom Alert Sounds

Local and remote notifications can specify custom alert sounds to be played when the notification is delivered. You can package the audio data in an aiff, wav, or caf file. Because they are played by the system-sound facility, custom sounds must be in one of the following audio data formats:

Linear PCM

MA4 (IMA/ADPCM)

µLaw

aLaw

Place custom sound files in your app bundle or in the Library/Sounds folder of your app’s container directory. Custom sounds must be under 30 seconds when played. If a custom sound is over that limit, the default system sound is played instead.

You can use the afconvert tool to convert sounds. For example, to convert the 16-bit linear PCM system sound Submarine.aiff to IMA4 audio in a CAF file, use the following command in the Terminal app:

afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v

@chrisjpalmer
Copy link
Collaborator

Hi Luigi. Nice work, this is fantastic. I think what we need to do here is:

  1. Add this to the documentation
  2. Edit the cordova build js script so that it can automatically copy soundfiles from a dedicated location into the platform folders.

Im not sure if I will be able to do this, this week but I will get back to you

@chrisjpalmer
Copy link
Collaborator

Sorry didn't mean to close it

@luigi37
Copy link
Author

luigi37 commented Dec 11, 2017

Thanks Chris! :-)

@luigi37
Copy link
Author

luigi37 commented Dec 14, 2017

Added on main post above a method to copy automatically the sound files through config.xml.
Thanks if someone could check if it works properly in Xcode as I fear it might need a manual transfer inside the appropriate folder within Xcode itself...

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