Motaz Dev
Motaz Dev

Reputation: 90

Set a sound as notification sound (no solution)?

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

Upvotes: 0

Views: 929

Answers (1)

Chris Truman
Chris Truman

Reputation: 903

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

Upvotes: 1

Related Questions