前台中的本地通知 [英] Local notification in Foreground

查看:84
本文介绍了前台中的本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在警报中,通知可以在后台正常运行,如下所示:

in alarm ,notification works fine in background as follows:

    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);

但是当我修改它也可以在前台播放时,如下所示:..它不起作用..仅出现警报,但没有声音?

but when i modify it to play in foreground too as follows:..its not working..Only alert appears but no sound???

-(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

当我记录通知的声音文件等属性时,它们工作正常...但是没有声音...

When i log soundfile etc properties of notification..they work fine...but no sound is there...

推荐答案

在前台,您必须提供警报视图并根据需要播放声音,通知将仅调用 applicationDidReceiveLocalNotification .您可以使用AVAudioPlayer

In foreground you have to provide alert view and play sound if it requires, the notification will just call applicationDidReceiveLocalNotification. You can play the sound using 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];

这篇关于前台中的本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆