5

アラームでは、通知は次のようにバックグラウンドで正常に機能します。

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

しかし、次のようにフォアグラウンドで再生するように変更すると、..動作しません..アラートのみが表示されますが、音はありません???

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

通知のサウンドファイルなどのプロパティをログに記録すると、正常に動作します...しかし、音はありません...

4

2 に答える 2

7

システムが通知を配信するときにアプリケーションが最前面に表示されている場合、アラートは表示されず、アイコンもバッジも表示されず、サウンドも再生されません。ただし、 application:didReceiveLocalNotification: は、アプリケーション デリゲートが実装している場合に呼び出されます。UILocalNotification インスタンスがこのメソッドに渡され、デリゲートはそのプロパティをチェックしたり、userInfo ディクショナリから任意のカスタム データにアクセスしたりできます。

于 2013-06-11T12:52:13.187 に答える