警报(通知)声音在iOs中突然停止工作

时间:2017-08-09 14:24:18

标签: ios objective-c audio uilocalnotification

在我的应用中,您可以选择观看某个特定区域。超过边界时,会发出通知。应用程序位于前台时的alertView和声音以及应用程序在后台时发出警报声的通知。当应用程序在前台时一切正常。在后台时,不会发出通知,也不会播放警报声。它之前运作良好。

这是我的代码:

// Show alertview when active and show localNotification when inactive

-(void)AlertViewShow{

    if (i == 1) {

    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:NSLocalizedString (@"Alert",@"")
                          message:NSLocalizedString(@"AlertMoving",@"")
                          delegate:self
                          cancelButtonTitle:NSLocalizedString(@"AlertOK",@"")
                          otherButtonTitles:nil];

    [alert show];

    NSURL *soundURL = [[NSBundle mainBundle]URLForResource: @"ALARM"
                                             withExtension:@"WAV"];
    avSound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
    avSound.numberOfLoops = -1;
    [avSound play];
}
i = 2;
}


- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
    if ([avSound isPlaying]) {
        [avSound stop];
    }
}
if (i == 2) {
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

}

}

- (void)localNotification {

UILocalNotification *localNotif = [[UILocalNotification alloc] init];

if (localNotif == nil)
    return;
localNotif.fireDate = nil;
localNotif.alertBody = NSLocalizedString (@"NotifAlert",@"");
localNotif.alertAction = NSLocalizedString (@"NotifView",@"");
localNotif.soundName = @"ALARMSHORT.wav";
localNotif.applicationIconBadgeNumber = 0;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

}

// END Show alertview when active and show localNotification when inactive 

有谁知道改变了什么,为什么它突然不再起作用了? 感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您正在使用scheduleLocalNotification: deprecated in iOS 10。尝试更新到UNUserNotificationCenter并查看问题是否仍然存在。

答案 1 :(得分:0)

if (@available(iOS 10.0, *)) {
        UNMutableNotificationContent *content = [UNMutableNotificationContent new];
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        content.title = @"Title";
        content.body = @"Alert Body";
        content.badge = @([content.badge integerValue] + 1);
        content.userInfo = userInfo;//userInfo is a dictionary
        if ([sound isEqualToString:@"default"]) {
            content.sound = [UNNotificationSound defaultSound];
        }else {
            content.sound = [UNNotificationSound soundNamed:@"customsound"];
        }

        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];

        NSString *identifier = [NSString stringWithFormat:@"CaptainLocalNotification-%@,",userInfo[@"pushId"]];
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
                                                                              content:content trigger:trigger];

        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (error != nil) {
                NSLog(@"Something went wrong: %@",error);
            }
        }];
    } else {
        UILocalNotification *local = [[UILocalNotification alloc]init];
        local.timeZone = [NSTimeZone defaultTimeZone];
        local.alertBody = alertBody;
        local.applicationIconBadgeNumber = local.applicationIconBadgeNumber + 1;
        if ([sound isEqualToString:@"default"]) {
            local.soundName = UILocalNotificationDefaultSoundName;
        }else {
            local.soundName = @"customsound";
        }
        local.userInfo = userInfo;//userInfo is a dictionary
        local.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
    }