0

Three sounds in my project :

} - (IBAction)Sound1:(NSDate *) fireDate;

{

  [[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 *notifiction =[[UILocalNotification alloc]init];

   notifiction.FireDate = fireDate; 

   notifiction.AlertBody = @"Wake Up!!!";    

  notifiction.soundName =UILocalNotificationDefaultSoundName;   

  notifiction.repeatInterval= NSMinuteCalendarUnit;   

[[UIApplication sharedApplication] scheduleLocalNotification: notifiction];

}

I want to let the users choose one of them to set it as a Notification sound I have been searching a lot but i did not found any solution that helped me with

Motaz Dev
  • 90
  • 5

1 Answers1

1

You can specify an audio file for local and push notifications. Allow the user to choose which file they want as the alert sound. Save that preference in NSUserDefaults and then Create a UILocalNotification with the sound.

Example:

You need to include your 3 sound files (Sound1.aiff, Sound2.aiff, and Sound3.aiff for example) in the Xcode project.

- (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]; 
}

Sources: Limits on iPhone push notification sounds?

UILocalNotifications playing Custom sound

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

Community
  • 1
  • 1
Chris Truman
  • 913
  • 1
  • 7
  • 25
  • thanks @ChrisTruman everything clear except the (NSUserDefaults) how can i use it to save sounds if u can write the code i'll be glad thank you again – Motaz Dev Aug 07 '13 at 14:46
  • thank you so much @ChrisTruman for the example its helped me so much but my notification doesn't work will i don't know what is the problem i'll update my question if u can help me plz – Motaz Dev Aug 12 '13 at 03:24
  • What's happening now? Is the notification never appearing? Need more detail. – Chris Truman Aug 12 '13 at 14:12
  • it works for one time then stopped and never appearing again i don't know wheres the problem @ChrisTruman :/ – Motaz Dev Aug 12 '13 at 21:19
  • @MotazDev you need to schedule new notifications with dates that are in the future. As long as you keep scheduling new notifications, you will see them show up. You must schedule the local notification. – Chris Truman Aug 12 '13 at 21:27
  • @MotazDev this seems like fine code and works for me. As long as the fire date is in the future, this notification should be shown when the date occurs. – Chris Truman Aug 12 '13 at 21:50
  • its work but something wrong when i choose the sound the alarm appearing now the three sounds work one by one @ChrisTruman is something wrong with my date picker or schedulenotification ? – Motaz Dev Aug 12 '13 at 21:54
  • @MotazDev What do you mean "the three sounds work one by one"? You will need to specify the notification.soundName property to choose a sound. – Chris Truman Aug 12 '13 at 22:00
  • 1
    the notification working on time .. but the problem the three sounds playing when the alarm appearing one after one not the only sound that i choose .. and about the property for the Notification.soundName i added this .. property (nonatomic,copy) NSString *soundName; @ChrisTruman – Motaz Dev Aug 12 '13 at 22:24
  • You don't need to add a property @MotazDev you just need to set notification.soundName = @"SoundFile"; for custom sounds. If you scheduled old notifications, it will not clear out other notifications. If you choose multiple notifications, you need to clear the notifications like this: [[UIApplication sharedApplication] cancelAllLocalNotifications]; [[UIApplication sharedApplication] cancelLocalNotification:theNotification]; – Chris Truman Aug 12 '13 at 22:28
  • 1
    i have a three sounds for each sound there is a notification how can i put this code [[UIApplication sharedApplication] cancelLocalNotification:theNotification]; to cancel all the notification sounds except what i choose @ChrisTruman – Motaz Dev Aug 12 '13 at 23:01
  • Sorry I can not help you any further. Please read this thoroughly https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html and reach out to Apple support https://developer.apple.com/support/technical/submit/ – Chris Truman Aug 12 '13 at 23:22
  • 1
    thank you for your time actually everything going great for what did u told me to do and thank you very much again but now i have problem when i pressed one button and i pressed another button the two sounds notification work together what i want to do is that to set notification for the last button that i have pressed and the previously button that i pressed for set notification cancelled @ChrisTruman – Motaz Dev Sep 13 '13 at 21:28
  • Sounds like you need to clear all the notifications before scheduling a new one. [[UIApplication sharedApplication] cancelAllLocalNotifications]; Then schedule your new notification with the new sound. @MotazDev – Chris Truman Sep 13 '13 at 23:23