将声音设置为通知声音(无解决方案)



我的项目中有三个声音:

}-(IBAction)声音1:(NSDate*)火灾日期;

{
  [[NSUserDefaults standardUserDefaults] setObject:@"Sound1.aiff" forKey:@"UserSoundChoice"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:[NSDate date]];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[localNotification setAlertBody:@"Alarm went off!"];   
[localNotification setAlertAction:@"View"]; 
[localNotification setHasAction:YES]; 
localNotification.soundName= [[NSUserDefaults standardUserDefaults] objectForKey:@"UserSoundChoice"]; 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
}
- (IBAction)Sound2:(NSDate *) fireDate;
{
  [[NSUserDefaults standardUserDefaults] setObject:@"Sound2.aiff" forKey:@"UserSoundChoice"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:[NSDate date]];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[localNotification setAlertBody:@"Alarm went off!"];   
[localNotification setAlertAction:@"View"]; 
[localNotification setHasAction:YES]; 
localNotification.soundName= [[NSUserDefaults standardUserDefaults] objectForKey:@"UserSoundChoice"]; 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
}
- (IBAction)Sound3:(NSDate *) fireDate;
{
       [[NSUserDefaults standardUserDefaults] setObject:@"Sound3.aiff" forKey:@"UserSoundChoice"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:[NSDate date]];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[localNotification setAlertBody:@"Alarm went off!"];   
[localNotification setAlertAction:@"View"]; 
[localNotification setHasAction:YES]; 
localNotification.soundName= [[NSUserDefaults standardUserDefaults] objectForKey:@"UserSoundChoice"]; 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
- (IBAction)SetDatePicker
{
NSDateFormatter *dateFormatter =[ [NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.dateStyle = NSDateFormatterShortStyle;
NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date];
NSLog (@"Alarm saved: %@", dateTimeString);
[self Sound1:dateTimePicker.date];
[self Sound2:dateTimePicker.date];
[self Sound3:dateTimePicker.date];

}

-(void)scheduleLocalNotificationWithDate:(NSDate *)fireDate   


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

   notifiction.FireDate = fireDate; 
   notifiction.AlertBody = @"Wake Up!!!";    
  notifiction.soundName =UILocalNotificationDefaultSoundName;   
  notifiction.repeatInterval= NSMinuteCalendarUnit;   
[[UIApplication sharedApplication] scheduleLocalNotification: notifiction];

}

我想让用户选择其中一个设置为通知声音我已经搜索了很多,但没有找到任何有助于我使用的解决方案

您可以为本地和推送通知指定音频文件。允许用户选择要作为警报声音的文件。将该首选项保存在NSUserDefaults中,然后使用声音创建UILocalNotification。

示例:

您需要在Xcode项目中包括您的3个声音文件(例如Sound1.aiff、Sound2.aiff和Sound3.aiff)。

- (IBAction)Sound1
{
    [[NSUserDefaults standardUserDefaults] setObject:@"Sound1.aiff" forKey:@"UserSoundChoice"];
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    [localNotification setFireDate:[NSDate date]];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
    [localNotification setAlertBody:@"Alarm went off!"];   
    [localNotification setAlertAction:@"View"]; 
    [localNotification setHasAction:YES]; 
    localNotification.soundName= [[NSUserDefaults standardUserDefaults] objectForKey:@"UserSoundChoice"]; 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
}

来源:iPhone推送通知声音的限制?

UILocalNotifications播放自定义声音

https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/WhatAreRemoteNotif.html#//apple_ref/doc/uid/TP40008194-CH102-SW1

相关内容

最新更新