This is driving me nuts, I believe I have tried every possible approach by now, but my phone still decides to stay silent. I just want it to play any sound for now when I present a local notification. Currently, neither the UILocalNotificationDefaultSoundName
nor the custom sounds that I added to my app bundle are working. I wrote a simple countdown app that uses presentLocalNotification:
when the count down is finished.
- (void)showLocalNotification
{
UILocalNotification *alarmNotification = [[UILocalNotification alloc] init];
alarmNotification.alertBody = [NSString stringWithFormat:@"fired at: %@", [NSDate date]];
alarmNotification.soundName = UILocalNotificationDefaultSoundName;
// alarmNotification.soundName = @"glass.aiff"; // "glass.aiff" is a file in my application bundle
[[UIApplication sharedApplication] presentLocalNotificationNow:alarmNotification];
}
I also show a UIAlertView
that will be initialized and displayed from the AppDelegate
when the notification is received:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Local notification was received" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
The local notification is received and the alert is shown, in the notification center I can also see that the notification has been fired, only the sound is not working...
I checked every similar question on stack overflow, enabled all my sound settings, made sure that in the Settins of the phone notifications for the app are enabled, I even received push notifications from other apps while I was testing where the sound has been played...
What the hell am I missing that my phone doesn't play any sound? Do I have to import some framework or call anything else on the notification when I fire it or when it is received?
Maybe the easiest solution would be if someone would post a minimal version of the code that is needed to play the default sound when a notification is presented (although I believe I already wrote that minimal version myself - except that it doesn't work).
By the way, I tried the app both on a real device and in the simulator, none of which worked...
If your application is in the foreground when the notification fires, no sound is played automatically. It will only play the sound automatically if your app is in the background at the time.
Your screenshot looks like your application might have been active in the foreground at the time you tried it.
According to Apple's push notification docs:
If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it. The UILocalNotification instance is passed into this method, and the delegate can check its properties or access any custom data from the userInfo dictionary.
You can always play the sound yourself using the AudioServicesPlaySystemSound() method if you want to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With