IOS: How to get the Timer app to play custom sounds when the APP is in the background?

General problem / scope / purpose:

I have a timer app and I would like to play a custom sound when the app is in BACKGROUND state and receives a UILocalNotification.

I am using UILocalNotification to start a timer (its the only way when the application cannot start a process in the background for more than 10 minutes).

Questions:

  • Is there a way to play a different UILocalNotification sound? If so, how can I do this? (In other words: when the app receives a UILocalNotification, a custom sound is played - which is not loud enough. I would like to get the correct alarm sound)

This is how I set my sound when my UILocalNotification is sent:

_localNotif.soundName = UILocalNotificationDefaultSoundName;

      

Then I create a notification and send it. After the application receives it, I process it in the following way (please read the code comments for more information):

- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification {
    NSLog(@"received notification");

    // Here I try to play a custom sound created in a custom object 
    // however the sound does NOT get played when the APP is in BACKGROUND mode (but I still see the NSLog message in console "received notification" 
    SoundManager* audio = [SoundManager sharedInstance];
    [audio playSound:Standard];
}

      

+3


source to share


3 answers


Try to change your line

_localNotif.soundName = UILocalNotificationDefaultSoundName;

      

Something like that:



_localNotif.soundName = "customSoundFileName.customSoundFileExtension";

      

As stated in the official docs , you must provide the filename of the sound resource in the main app bundle. I have a working example that I can share with you if you need it.
Good luck!

0


source


In the view manager, where you trigger local notification:

.h

#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController <AVAudioPlayerDelegate>

@property (nonatomic, strong) AVAudioPlayer *notificationSound;

      

You don't need to fire local notification, if it goes into background in app delegate, local notification will fire whether they are in background or not. didReceiveLocalNotification

is how you handle it when the app is active: from the docs here :

Sent to a delegate when a running application receives a local notification.

.m

-(void)startTimer {
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runTimerTasks:) userInfo:nil repeats:YES];
    [self fireLocalNotification];
}

-(void)fireLocalNotification {
 UILocalNotification *timerDoneNotification = [[UILocalNotification alloc] init];
 timerDoneNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:secondsLeft];
 timerDoneNotification.alertBody = @"Time Up!";
 timerDoneNotification.alertAction = @"OK";
 timerDoneNotification.timeZone = [NSTimeZone defaultTimeZone];
 timerDoneNotification.soundName = @"alarm.aif"; 
 [[UIApplication sharedApplication] scheduleLocalNotification:timerDoneNotification];
}

      

And then if you just want to play / stop the sound elsewhere, as if the second hand was 0 when the app is active, you call it like this:



[self playAlarm];

-(void)playAlarm {
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:@"alarm" ofType:@"aif"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSError *error = nil;
self.notificationSound = [[AVAudioPlayer alloc] initWithData:fileData error:&error];
self.notificationSound.numberOfLoops = -1;
[self.notificationSound play];
}

      

And to stop it:

[self.notificationSound stop]; 

      

Downside to this UILocalNotification sound can't be longer than 30 seconds, but it answers the question.


Ref : UILocalNotification Docs

Sights:

When the system sends a local notification, several things can happen depending on the state of the application and the type of notification. If the app is not the most forward and visible, the system displays a warning message, app icons, and plays a sound - whatever is specified in the notification. If the notification is an alert and the user presses an action button (or if the device is locked by dragging the action slider), the app wakes up or starts. (If the user removes one of the custom actions that you specify using the addActions property, the application wakes up or starts in the background.) In your application: didFinishLaunchingWithOptions method:an application delegate can obtain a UILocalNotification object from the launch parameter dictionary using the UIApplicationLaunchOptionsLocalNotificationKey. The delegate can check the properties of the notification and, if the notification includes user data in its user information dictionary, he can access that data and process it accordingly. On the other hand, if the local notification is only app icon icons, and the user launches the app in response, the: doneFinishLaunchingWithOptions: method is called, but the UILocalNotification object is not included in the options dictionary. When the user selects a custom action, the application delegates to the application: handleActionWithIdentifier: forLocalNotification: completeHandler: method is called to process the action.The delegate can check the properties of the notification and, if the notification includes user data in its user information dictionary, he can access that data and process it accordingly. On the other hand, if the local notification is only the app icon icons, and the user launches the app in response, the: doneFinishLaunchingWithOptions: method is called, but the UILocalNotification object is not included in the options dictionary. When the user selects a custom action, the application delegates to the application: handleActionWithIdentifier: forLocalNotification: completeHandler: method is called to process the action.The delegate can check the properties of the notification and, if the notification includes user data in its user information dictionary, he can access that data and process it accordingly. On the other hand, if the local notification is only app icon icons, and the user launches the app in response, the: doneFinishLaunchingWithOptions: method is called, but the UILocalNotification object is not included in the options dictionary. When the user selects a custom action, the application delegates to the application: handleActionWithIdentifier: forLocalNotification: completeHandler: method is called to process the action.if the local notification is only the app icon icons and the user launches the app in response, the: doneFinishLaunchingWithOptions: method is called, but the UILocalNotification object is not included in the options dictionary. When the user selects a custom action, the application delegates to the application: handleActionWithIdentifier: forLocalNotification: completeHandler: method is called to process the action.if the local notification is only the app icon icons and the user launches the app in response, the: doneFinishLaunchingWithOptions: method is called, but the UILocalNotification object is not included in the options dictionary. When the user selects a custom action, the application delegates to the application: handleActionWithIdentifier: forLocalNotification: completeHandler: method is called to process the action.

If the app is main and visible when the system sends a notification, the app delegates to the app: didReceiveLocalNotification: is called to handle the notification. Use the information in the provided UILocalNotification object to decide what action to take. The system does not display any warnings, app icon icon, or plays any sounds when the app is already in front.

0


source


UILocalNotification *notification = [UILocalNotification new];
notification.fireDate = [NSDate date]; //
notification.alertBody = @"This is custom alarm";
notification.soundName = @"YourCustomAudioFile.aiff";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

      

BUT

Custom audio files must meet two conditions:

  • Sound length less than 30 seconds
  • The audio file format must be: raw PCM or MA4, or uLaw, or aLaw ( wav

    , aiff

    or caf

    ).

So if you have an MP3 alarm clock you have to convert it ... Here is the Apple documentation (search in the section Preparing Custom Alert Sounds

) link

0


source







All Articles