问题
I’m working on the iOS version of my phonegap-based navigation app. My app tracks the user’s location around a walk route using GPS and alerts the user using audio (navigator.notification.beep) and tactile (navigator.notification.vibrate) feedback when they reach a location at which there’s new instructions for them to follow.
When my app is running in the foreground, both the audible beep and the vibration fire on reaching the geographic location but when the app is paused in the background, either by pressing the power button to turn the screen off or pressing the home button to return to the springboard, only the vibration works - the beep is not audible. I’ve added debug so I can see in the log file that the app is calling navigator.notification.beep() while in the background but the beep sound isn't made. I’ve tested my app on an iPhone 4S running iOS 6.3.1 and iPad 2 running iOS 5.1.1. Obviously the iPad doesn’t vibrate but the beep works while the app is in the foreground but not when in the background.
- My app is using Phonegap 2.5.0
- I’m using latest Xcode v4.6.2 with latest SDK for iOS 6.3.1
- I’m using a beep.wav in the /www root
- My app’s .plist sets “UIBackgroundModes” of “location” and “audio”
My config.xml contains the settings:
<plugin name="Notification" value="CDVNotification" />
<plugin name="Media" value="CDVSound" />
<preference name="MediaPlaybackRequiresUserAction" value="false" />
<preference name="AllowInlineMediaPlayback" value="true" />
Any suggestions how to fix this would be most appreciated :-)
回答1:
In case anyone else in interested, here’s how I solved this:
I updated the Local Notifications phonegap plugin for use with Cordova 2.x. I used the plugin to provide the background beep and phonegap for the foreground beep by placing the same sound for phonegap in www/ as beep.wav as for the local notification in the iOS project Resources as beep.caf.
function doBeep(){
cordova.require('cordova/plugin/localnotification').add(
function(){
console.log("Successfully added local notification");
},
function(){
console.error("Error adding local notification");
},{
date: new Date(new Date().getTime()),
repeat:'',
message: '', // No message so just beep
hasAction: true,
badge: 0,
id: '1',
background:'background',
foreground:'running',
sound: 'beep.caf'
}
);
}
function running(){
console.log("Running in the foreground so use a phonegap notification");
navigator.notification.beep();
}
function background(){
console.log("Running in the background so an iOS local notification will be used");
}
来源:https://stackoverflow.com/questions/16735449/beep-not-working-when-phonegap-app-is-in-background-on-ios