声音文件无法在本地通知iOS7上播放

时间:2014-02-26 03:03:33

标签: ios ios7 ipod localnotification

我正在iPod Touch(iOS 7)上运行本地通知应用程序。通知即将到来,但通知的声音没有播放。我已将声音文件放在文件夹中,并检查设备上的声音设置。我试过很多声音文件,但没有任何效果。以下是代码: -

- (IBAction) scheduleAlarm:(id) sender {
    [eventText resignFirstResponder];

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    // Get the current date
    NSDate *pickerDate = [self.datePicker date];

    // Break the date up into components
    NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit ) 
                                                   fromDate:pickerDate];
    NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) 
                                                   fromDate:pickerDate];

    // Set up the fire time
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:[dateComponents day]];
    [dateComps setMonth:[dateComponents month]];
    [dateComps setYear:[dateComponents year]];
    [dateComps setHour:[timeComponents hour]];
    // Notification will fire in one minute
    [dateComps setMinute:[timeComponents minute]];
    [dateComps setSecond:[timeComponents second]];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];
    [dateComps release];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = itemDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];

    // Notification details
    localNotif.alertBody = [eventText text];
    // Set the action button
    localNotif.alertAction = @"View";

    localNotif.soundName = @"alarm.wav"; ///UILocalNotificationDefaultSoundName;

    //    notify.soundName = @"../Documents/blabla.caf"

    localNotif.applicationIconBadgeNumber = 1;
    localNotif.alertBody = @"Staff meeting in 30 minutes";

    // Specify custom data for the notification
    //  NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
    // localNotif.userInfo = infoDict;

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];

    [self.tableview reloadData];
}

2 个答案:

答案 0 :(得分:0)

有时可能会因设备而发生这种情况。并尝试清除之前的通知:

UIApplication* app = [UIApplication sharedApplication];
[app cancelAllLocalNotifications];

检查answer。这可以帮助您解决问题。

答案 1 :(得分:0)

请先尝试设置 - > notificationcenter->声音是否关闭。然后检查设置 - >声音 - >滑块是否不为零。这意味着如果你的声音为0则不允许声音要响起来。 希望有所帮助。