前景中的本地通知

use*_*504 5 notifications local objective-c ios

在警报中,通知在后台工作正常如下:

    UILocalNotification *notification1=[[UILocalNotification alloc]init];
    notification1.fireDate=alramtime;
    notification1.alertBody=@"Training Time";
    notification1.repeatInterval=NSDayCalendarUnit;

    notification1.soundName=@"Alarm.caf";

    ///////
    previousnotif=[[NSUserDefaults standardUserDefaults]objectForKey:@"notif1"];
    previous=[NSKeyedUnarchiver unarchiveObjectWithData:previousnotif];

    NSLog(@"alarm %@",previous);
    if (previous!= NULL) {
        [[UIApplication sharedApplication]cancelLocalNotification:previous];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"notif1"];

    }
    NSData *alarm1=[NSKeyedArchiver archivedDataWithRootObject:notification1];
    [notifdefaults setObject:alarm1 forKey:@"notif1"];
    /////////


    [[UIApplication sharedApplication] scheduleLocalNotification:notification1];
    NSLog(@"new alarm %@",notification1);
Run Code Online (Sandbox Code Playgroud)

但是当我修改它以便在前景中播放时如下:..它不工作..只有警报出现但没有声音???

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{

UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {


   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"KNIP"
                                                   message:notification.alertBody
                                                   delegate:self cancelButtonTitle:@"Close"
                                          otherButtonTitles:nil];  

[alert show];

}
@end
Run Code Online (Sandbox Code Playgroud)

当我记录声音文件等通知属性时......它们工作得很好......但是没有声音......

Ani*_*ese 8

在前台,如果需要,您必须提供警报视图和播放声音,通知将只调用applicationDidReceiveLocalNotification.你可以使用播放声音AVAudioPlayer

 //Playing sound
        NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath],notification.soundName]];

        AVAudioPlayer *newAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
        self.audioPlayer = newAudioPlayer;
        self.audioPlayer.numberOfLoops = -1;
        [self.audioPlayer play];
        [newAudioPlayer release];
Run Code Online (Sandbox Code Playgroud)


Shu*_*ank 7

如果应用程序在系统发送通知时最重要且可见,则不会显示警报,也不会标记图标,也不会播放任何声音.但是,如果应用程序委托实现它,则调用application:didReceiveLocalNotification :. UILocalNotification实例将传递到此方法,并且委托可以检查其属性或从userInfo字典访问任何自定义数据.