简体   繁体   中英

Cordova Phonegap local notification sound on iOS

I'm trying to test the local notifications on iOS using Cordova.

Everything it's working fine except the sound, I only get the default notification sound, not the custom sound I've added in the options.

This is only happening on iOS .

Code:

var now = new Date().getTime(),
    _10_seconds_from_now = new Date(now + 3*1000);

window.plugin.notification.local.add({
            id:      1,
            title:   'Reminder',
            message: 'Dont forget to buy some flowers.',
            repeat:  'weekly',
            date:    _10_seconds_from_now,
            sound:  'www/alarms/burglar.caf'
});

Okay, after a few looks in the objective-c code I finally made it work!

The solution it's very simple, but not documented anywhere. You have to put the prefix file:// and all will work. sound: 'file://alarms/factory.wav'

Example:

window.plugin.notification.local.add({
            id:      2,
            title:   'Reminder',
            message: 'Dont forget to buy some flowers blah blah blah.',
            repeat:  'weekly',
            date:    _10_seconds_from_now,
            sound:  'file://alarms/factory.wav'
        });

API says:

Notification sound on iOS

Note: Custom sounds must be under 30 seconds when played. If a custom sound is over that limit, the default system sound is played instead.

This selected answer suggested:

From the Cordova Docs :

  • Cordova implements beep by playing an audio file via the media API.
  • The user must provide a file with the desired beep tone.
  • This file must be less than 30 seconds long, located in the www/ root, and must be named beep.wav

I also faced that problem you can use this code as i applied and it's working fine for me

cordova local notification sound not working in ios and andorid

open the link I think it will help to you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM