mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2025-12-17 03:43:24 +00:00
Onactivate, ondeactivate & onfailure callbacks
This commit is contained in:
@@ -23,6 +23,11 @@
|
||||
|
||||
@implementation APPBackgroundMode
|
||||
|
||||
NSString *const kAPPBackgroundModeNamespace = @"cordova.plugins.backgroundMode";
|
||||
NSString *const kAPPBackgroundModeActivateEvent = @"activate";
|
||||
NSString *const kAPPBackgroundModeDeactivateEvent = @"deactivate";
|
||||
NSString *const kAPPBackgroundModeFailureEvent = @"failure";
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Initialization methods
|
||||
|
||||
@@ -99,6 +104,7 @@
|
||||
- (void) keepAwake {
|
||||
if (enabled) {
|
||||
[audioPlayer play];
|
||||
[self fireEvent:kAPPBackgroundModeActivateEvent withParams:NULL];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,6 +117,10 @@
|
||||
NSLog(@"BackgroundMode: On simulator apps never pause in background!");
|
||||
}
|
||||
|
||||
if (audioPlayer.isPlaying) {
|
||||
[self fireEvent:kAPPBackgroundModeDeactivateEvent withParams:NULL];
|
||||
}
|
||||
|
||||
[audioPlayer pause];
|
||||
}
|
||||
|
||||
@@ -149,11 +159,26 @@
|
||||
[session setActive:YES error:NULL];
|
||||
};
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Helper methods
|
||||
|
||||
/**
|
||||
* Restart playing sound when interrupted by phone calls.
|
||||
*/
|
||||
- (void) handleAudioSessionInterruption:(NSNotification*)notification {
|
||||
[self fireEvent:kAPPBackgroundModeDeactivateEvent withParams:NULL];
|
||||
[self keepAwake];
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fire an event with some parameters in the browser.
|
||||
*/
|
||||
- (void) fireEvent:(NSString*)event withParams:(NSString*)params
|
||||
{
|
||||
NSString* js = [NSString stringWithFormat:@"setTimeout('%@.on%@(%@)',0)",
|
||||
kAPPBackgroundModeNamespace, event, params];
|
||||
|
||||
[self.commandDelegate evalJs:js];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user