Open luigi37 opened 6 years ago
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??
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.
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
Cool thanks! Found something here https://github.com/phonegap/phonegap-plugin-push/issues/17 about putting caf sound file in platform/iOS/ but had no chance to try it yet...
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!
ffmpeg -i alarm.mp3 alarm.wav
They are similar in size and both very large compared to .mp32- 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
Interesting enough that Apple documentation contradicts what I saw about mp3...
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
Hi Luigi. Nice work, this is fantastic. I think what we need to do here is:
Im not sure if I will be able to do this, this week but I will get back to you
Sorry didn't mean to close it
Thanks Chris! :-)
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...
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