I'm doing a VOIP app when the incoming call is coming and app is in the foreground. I play the ringer with the code below
self.ringTimer = [[TimerService sharedInstance] createIntervalTimerWithInterval:2500 queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) andBlock:^{
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
}];
NSString *ringingPath = [[NSBundle mainBundle] pathForResource:@"incoming_sound" ofType:@"mp3"];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionMixWithOthers error:nil];
// for normal phone receiver route it to speaker
if ([[audioSession audioRoute] isEqualToString:kAudioSessionDevice_Phone]) {
[audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
}
NSURL *soundUrl = [NSURL fileURLWithPath:ringingPath];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&error];
self.audioPlayer.numberOfLoops = -1;
[self.audioPlayer play];
The above code works well and it also respect the silent switch button when playing sound.
However, if I minimize the app the device will stop vibrating and playing incoming sound. This can be overcome if I set AVAudioSession
category to AVAudioSessionCategoryPlayback
but this category will not silent the sound when the silent switch is on.
I see the other apps like
- (Receive incoming in the app) they can keep vibrate the device after minimize until the call timeout is reached (no one answer) about one minute without notification showing before stopping (Receive call end notification).
- (Receive incoming when app closed) they also do the same because when I dismiss the notification the device can still vibrate for the same duration.
(Receive incoming in the app) they show a long notification to keep device vibrate. If I dismiss that notification it will stop right away.
(Receive incoming when app closed) if I dismiss the notification, the device vibrate for 2-3 more times and show a long notification (same as when receive incoming in the app)
(Receive incoming in the app) I'm not sure for skype, but for viber when minimize the app it stop vibrate instantly.
(Receive incoming when app closed) I think they have done the same way as facebook (not sure)
How can I make the device vibrate without notification like Line
when minimize the app?
If I manage to find a way and make it works like Line
do I need to use Voip push when the app is not in foreground to implement that method? (Will normal push notification work?)