3

在 iOS 7 中,当用户从锁屏滑动我的通知并被带到我的应用程序时,通知声音会继续播放(与 iOS 6 不同)。当我的应用在 iOS 7 中启动时,有什么方法可以以编程方式停止该声音?

注意:请参阅接受的答案以了解伪劣的解决方法。

4

3 回答 3

7

我很确定这是 Apple 的一个错误,请参阅devforums.apple.com/message/888091(感谢 Gui13)。提交一份重复的错误报告以让 Apple 关注它,因为这是 Apple 为错误分配优先级的方式。与此同时,以下方法将起作用,但也会清除通知中心中的所有通知,这当然是一种劣质的解决方法,但在我的情况下,在解决此问题之前是值得的:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];    
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
于 2013-10-09T13:21:58.483 回答
0

使用它没有帮助

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

通过点击通知进入应用程序后。

在处理带有声音的通知时,我通过发送另一个空通知解决了这个问题:

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];
    }
}
于 2015-07-20T08:35:24.373 回答
0

对于 iOS 7,接受的答案可能是唯一可行的选择。对于来这里可以支持最低iOS 10的开发人员来说,这是可行的。

您可以通过调用从通知中心删除所有通知

UNUserNotificationCenter.current().removeAllDeliveredNotifications()

这与接受的答案具有非常相似的影响:音频停止播放并且所有通知都从通知中心删除。

一个改进的解决方案是使用

UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [String])

这只会停止给定通知的音频,并将它们从通知中心中删除。

要获取所有已发送通知的 ID,请使用

UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
  let ids = notifications.map { $0.request.identifier }
}
于 2020-06-01T19:31:14.340 回答