我两次使用Notification Service Extension振动设备,后台模式开启,以下方法在iOS 11
和12.0.1
上有效,但在iOS 12.1
上无效
背景模式如下:
这是代码:
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.7) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1.4) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
}
contentHandler(bestAttemptContent)
}
}
答案 0 :(得分:0)
我发现,如果关闭振动设置,它将无法正常工作。 (转到系统设置\声音=>振铃振动。)
如果您发送的推送通知仅带有声音和徽章,但没有文本提示,则如果您的手机未处于静音模式,它将播放声音,但是如果您处于静音模式,则不会播放声音
现在,如果您向该通知添加文本警报,即使在振动模式下,它也可以很好地播放声音。
围绕此可能还要进行更多测试。例如:如果您发送自定义键/值对,它会播放声音吗?不确定。