Why doesn't my UILocalNotification play any so

2020-03-04 08:15发布

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...

the notification center proves that all my local notifications are fired the alert proves that my app delegate receives the local notification

3条回答
仙女界的扛把子
2楼-- · 2020-03-04 08:34

UINotification doesn't play sound if the app is Active because it doesn't make sense, it assumes that you see the notification if the app is Active, but it will play sound if the app is not running or, in Background to notify user that a notification has come.

Cheers.

查看更多
叛逆
3楼-- · 2020-03-04 08:44

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.

查看更多
家丑人穷心不美
4楼-- · 2020-03-04 08:45

You have to use *.caf format. I spent a lot of time but i found solution. I tried *.mp3 *.wav but doesn't work.

I hope it works.

How can i convert to *.caf format?

1) Open terminal

2) Example:

cd /Users/username/Desktop/FolderName

Click Enter

3)

afconvert -f caff -d LEI16@22050 -c 1 BlaBla.mp3 BlaBlaNew.caf

Click Enter

查看更多
登录 后发表回答