Custom iOS UILocalNotification sound does not play (possibly related to Xcode update)

爱⌒轻易说出口 提交于 2019-12-04 04:57:21

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 :)

Lovato

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.

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

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.

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

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 :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!