首页 文章

更新应用程序后,UILocalNotification自定义声音无法正常工作

提问于
浏览
3

我目前遇到的问题是UILocalNotifications中的自定义声音仅在第一次安装应用程序时才起作用 . 在第二次安装应用程序(即模拟升级)后,声音会恢复为默认声音,即使对于新创建的通知也是如此 . 我创建了一个新的测试项目来测试最低限度,问题仍然是重新编写的 . 下面是我的ViewController:

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let notification = createNotification()
    UIApplication.shared.scheduleLocalNotification(notification)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func createNotification() -> UILocalNotification {
    let tenSecondsLater = Date().addingTimeInterval(10)

    let notification = UILocalNotification()
    notification.fireDate = tenSecondsLater
    notification.alertTitle = "Test Notification Title"
    notification.alertBody = "body"
    notification.soundName = "alarm-20secs.wav"

    return notification
}

}

在AppDelegate中正确设置权限:

UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [UIUserNotificationType.alert, UIUserNotificationType.badge, UIUserNotificationType.sound], categories: nil))

Repro步骤:

  • 在Xcode中按Run键安装应用程序 . 按回家到后台应用程序

  • ~10秒后,警报显示正常播放声音

  • 在Xcode中按Run再次安装应用程序 . 按回家到后台应用程序

  • ~10秒后,警报显示,但现在播放默认声音 .

我已经尝试过调用cancelAllLocalNotifications(),但它似乎没有改变任何东西 . 强制杀死不会重现问题,只能升级 . 升级后,仍然可以使用AVPlayer播放声音,因此不会删除文件;只有本地通知会受到影响 .

我很清楚UILocalNotification在iOS 10中已被弃用,但新的UserNotification框架不支持重复通知,这是我需要的功能 . 在运行iOS 10和10.0.2的sim和设备中都会出现此问题 . 我找不到任何描述这个特定问题的线索,对于大多数其他人来说,根本无法让他们的声音发挥作用 . 在这种情况下,它只能在重新安装应用程序后工作正常 . 当我发布新版本时,这将是一个问题,因为升级路径会破坏通知声音,这对我的应用程序至关重要 .

1 回答

  • 2

    可能到现在为止,你知道这是由ios 10中的一个错误造成的 .

    重新启动手机(在应用更新后)也将修复它 .

相关问题