Custom iOS UILocalNotification sound does not play

2020-07-10 06:56发布

I'm trying to get a custom sound working on a UILocalNotification, and I'm just getting no sound at all. If I use UILocalNotificationDefaultSoundName, I indeed get the default sound, but when the custom sound is specified, there is no sound, just the message. The sound is less than 30 seconds and it's in the right format, as far as I can tell. Here's a screenshot of the file info:

I've inspected the .app directory in XCode's DerivedData directory, and the alarm.caf file is at the root of the app, which I believe means it's in the bundle (right?).

I'm pretty sure this was working a while ago, and I've since upgraded Xcode. Maybe that is a hint?

I've also tried deleting/reinstalling/rebooting as mentioned in other answers. As you can see, I'm calling cancelAllLocalNotifications first.

Does anyone have any idea what could be wrong?

[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSLog(@"installing alarm");
[arguments pop]; // name
[arguments pop]; // title
alarm.alertBody = [arguments pop];
alarm.fireDate = [[NSDate date] addTimeInterval:[[arguments pop] intValue]/1000];
//alarm.soundName = UILocalNotificationDefaultSoundName;
alarm.soundName = @"alarm.caf";

[[UIApplication sharedApplication] scheduleLocalNotification:alarm];

6条回答
smile是对你的礼貌
2楼-- · 2020-07-10 07:45

Ok, so here's what happened. I forgot how the app handles the notification itself if it is still running. My code was only displaying a UIAlertView and not playing the sound. I'm not sure why it worked with the default sound. In any case, I added code like this to my AppDelegate:

- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
  NSLog(@"didReceiveLocalNotification");
  if (application.applicationState == UIApplicationStateActive) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"MarkMyTime"
      message:notification.alertBody
      delegate:self cancelButtonTitle:@"OK"
      otherButtonTitles:nil];
    NSString *soundFilePath = [[NSBundle mainBundle] 
      pathForResource:notification.soundName ofType:nil];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
    [fileURL release];
    player.delegate = self;
    [player prepareToPlay];
    [player play];
    [alertView show];
    if (alertView) {
      [alertView release];
    }
  }
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 
{
  NSLog(@"Releasing player");
  [player release];
}

This will show a UIAlertView and play the sound on the notification object. You also need to add the AVAudioPlayerDelegate interface to the AppDelegate to be able to assign the delegat to the player. I think if you are using ARC, this code could be simplified a bit.

@interface AppDelegate : PhoneGapDelegate <AVAudioPlayerDelegate> {

I'm not sure if this is the best approach, so feel free to chime in with any improvements.

查看更多
该账号已被封号
3楼-- · 2020-07-10 07:48

I don't know the reason (and I didn't read documentation too), I just turned on the action property notification setHasAction:YES and the sound began to play.

查看更多
Emotional °昔
4楼-- · 2020-07-10 07:57

please make sure that the iPhone is not in silent mode( because your code seems to be good )

just check the button on the side of your iPhone

查看更多
贪生不怕死
5楼-- · 2020-07-10 07:58

Your code seems to be good.

Try to clean your project, uninstall your app from your device/simulator, then re-install it. It could help maybe :)

查看更多
【Aperson】
6楼-- · 2020-07-10 07:58

Maybe you do not add the sound file (*.caf) in Xcode project: Build Phases/Copy Bundle Resources.

查看更多
Explosion°爆炸
7楼-- · 2020-07-10 07:58

your code is good , but check your iphone setting setting -> Notification center -> Your App -> Sound - > "On" the Sound Should be "On"

So, to enable this , checked Inter App Audio at Capabilities in Targets of the application and it was Off Capabilities in Inter-app audio change this to On

then local notification sound is working inshallah :)

查看更多
登录 后发表回答