The UILocalNotification sound does not stop playing

I have set a custom .aif 30 second file as the local notification sound name. And below is my code for scheduling the local notification.

//Function to schedule local notification
-(void)schedulelocalnotification:(NSDate *)particularfiredate ringtone: (NSString *)particularringtone name:(NSString *)alarmname info:(NSDictionary *)dicttext
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = particularfiredate;
notification.soundName = [arrayAIFFFiles objectAtIndex:[arraysoundfilesnames indexOfObject:particularringtone]];
notification.alertBody = alarmname;
notification.userInfo = dicttext;

[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

But when the device is locked, and the user slides on the notification to enter the app, the sound keeps on playing even when the user enters the app. It continues to play even when the user quits/uninstalls the app.
Please suggest what could be the possible reasons.

  • iOS 8 .scheduledLocalNotifications empty
  • Local notifications stopped working, can't figure out why
  • iPhone : Daily local notifications
  • detect “Allow Notifications” is on/off for iOS8
  • Scheduling local notification from within a Today extension
  • Schedule Interactive UILocalNotification - Obj-C
  • - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    {
    
        [[NSUserDefaults standardUserDefaults] setValue:@"0" forKey:@"demo"];
        NSLog(@"%i",[[[UIApplication sharedApplication] scheduledLocalNotifications] count]);
        NSString *viewcontrollerstring = [notification.userInfo objectForKey:@"smiletosnooze"];
        NSLog(@"++++++------%@",viewcontrollerstring);
    
    
    }
    

    PS: I checked – UILocalNotification stop sound after notification is dismissed and this – Stop UILocalNotification Sound on slide to view but it was of no help. 🙁

    Solutions Collect From Internet About “The UILocalNotification sound does not stop playing”

    This appears to be an open bug with iOS 7 as filed here. It also seems that when a device is passcode-locked, this issue does not appear. A pretty ugly hack that worked for me is setting a value for the application badge number and removing it immediately when the app comes into foreground. A sample code would be:

    - (void)applicationWillEnterForeground:(UIApplication *)application
    {
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
    
    }
    

    EDIT:

    Apparently, the above mentioned hack is not actually working on iOS 7.1+. The only work-around I found is the following, but I’m very hesitant in calling it an actual answer:

    - (void)applicationWillEnterForeground:(UIApplication *)application
    {
       AVAudioSession *session = [AVAudioSession sharedInstance];
       [session setCategory:AVAudioSessionCategoryPlayback error:nil];
       [session setActive:YES error:nil];
       MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
        [musicPlayer setVolume:0.0f]; 
    }
    

    But there are a number of serious flaws with the code above:

    1. setVolume method is deprecated since iOS 7 (although apparently many apps are still using it)
    2. You have at some point of your app to re-set the volume to a proper (non-zero level)
    3. Setting the volume property will most definitely have side-effects in other apps that might be playing sounds or music at the same time

    UPDATE September 18, 2014

    This issue seems to be resolved on iOS 8.