Stopping ios 7 remote notification sound

2019-02-10 23:17发布

问题:

In iOS 7, when a user swipes one of my notifications from the lockscreen and is taken to my app, the notification sound keeps playing (unlike iOS 6). Is there any way to programmatically stop that sound when my app launches in iOS 7?

NOTE: see the accepted answer for a shoddy workaround.

回答1:

I'm pretty sure this is a bug on Apple's end, see devforums.apple.com/message/888091 (thanks Gui13). File a duplicate bug report to get Apple to pay attention to it, as that is how Apple assigns priority to bugs. In the meantime, the following will work but will also clear all of your notifications in the notification center, which of course is a shoddy workaround, but in my case is worth it until this gets fixed:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];    
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];


回答2:

it doesn't help by using

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];    
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];

after entering the app by clicking the notification.

I have solved this problem by sending another empty notification when dealing with the notification with sound:

if (notification.soundName != nil) {
    if (IS_IOS7) {
        UILocalNotification *emptyNotification = [[UILocalNotification alloc] init];
        emptyNotification.timeZone = [NSTimeZone defaultTimeZone];
        emptyNotification.fireDate = [NSDate date];
        emptyNotification.alertBody = @"";
        [[UIApplication sharedApplication] scheduleLocalNotification:emptyNotification];
    }
}