سؤال

I want to trigger an alarm in my IOS device when the core location framework delegate method fires. The alarm rightly rings when the app is in the foreground. But when the device is locked, the alarm feature doesn't work. This is the code I have used.

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate date];

localNotification.alertBody = @"body";
localNotification.soundName = @"RING.WAV";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Is it because I have used UILocalNotification class? If so, is there an alternative.

In the lock screen, the local notification alert body is displayed, but the alarm doesn't sound.

Edit:

In appdelegate I have the delegate to receive local notification

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notification{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/RING.WAV", [[NSBundle mainBundle] resourcePath]]];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;

if (audioPlayer == nil)
    NSLog(@"error :%@",[error description]);
else 
    [audioPlayer play];
}

I figured out that this method is not fired when the device is locked. Is there a way to trigger alarm based on UILocalNotification, when the device is locked?

هل كانت مفيدة؟

المحلول 2

Setting the audio session properties did the trick in this case.

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error: nil];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
[AVAudioSession sharedInstance] setActive: YES error: nil];

نصائح أخرى

You need to tell the phone that your notification has an action

[localNotification setAlertAction:@"My Action"];
[localNotification setHasAction:YES];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top