Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
715 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...