Is it possible to stop the sound of a UILocalNotification from playing when the notification is delivered?

StackOverflow https://stackoverflow.com/questions/4762816

سؤال

If a UILocalNotification fires with a sound set, and the user taps "Cancel" on the notification alert, the sound is stopped. But if the user taps "View", iOS then delivers the notification to the app and the sound keeps on playing. Is there any way to cancel this sound from the app? Canceling the notification in the app once the notification is delivered doesn't work (I didn't expect it to, the notification has already been delivered after all), and since I don't have the sound's system sound ID (and I didn't allocate it in the first place), I can't call AudioServicesDisposeSystemSoundID (can I?).

Is it possible to stop a UILocalNotification sound from playing if the user taps the Action button of the notification's alert?

هل كانت مفيدة؟

المحلول 2

Apparently the problem only exists on the simulator (iOS 4.2 sdk), not on the actual device.

نصائح أخرى

It does not stop on the device too (5.1)

I have been trying to fix it but I can't figure it out.

I actually got it to stop on the simulator using this

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotif) {
        [[UIApplication sharedApplication] cancelLocalNotification:localNotif];
    }    
    return YES;
}

but It still doesn't stop on the device

It can be handled in application delegate method as follows, if the user taps the Action button of the notification's alert, then the following method will be called, there we can cancel the notification

- (void)application:(UIApplication *)app
didReceiveLocalNotification:(UILocalNotification *)notif {
[[UIApplication sharedApplication]cancelLocalNotification:notif];
}

I had the same problem when initially testing with an iPhone 4.

The problem appears to have gone away as of iOS 8 and now Local Notification Sounds stop when canceled after transitioning to the App.

iPhone4 [ iOS - 7.1.2 ] - Local Notification sound keeps playing in App no matter what
iPhone6 [ iOS - 8.1.1 ] - Local Notification sound stop when canceled programmatically

From what I can infer, it appears a fix exists somewhere in iOS 8.x
(Alas I didn't manage to find any documentation or release notes on the matter.)

The Problem becomes a non-issue for apps focusing on iOS 8
(provided you cancel the Local Notification coming in)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top