我想通过远程通知通知护士,病人发生了一些不好的事情。所以我想在她/他收到通知时振动 8 次。
当我的应用收到远程推送通知时,它会触发我的通知服务扩展
func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
在那个功能中,我打电话
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
sleep(1)
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
但只有默认振动跳闸一次。
接收推送通知时是否可以多次振动? 或者我可以使用自定义振动模式使其振动多次? 如果不可能,你能提供一些官方文件来表明这一点吗?很难证明一件事是不可能的。
您是否尝试过在不同的队列中播放声音和睡眠?
我写了一个简单的类来播放系统声音,就我而言,它运行良好(但我没有尝试在扩展目标中使用它):
import Foundation
import AudioToolbox
public class SoundPlayer {
private let sound: SystemSoundID
private var playing = false
private let limit: Int
public init(sound: SystemSoundID = kSystemSoundID_Vibrate,
limit: Int = 8) {
self.sound = sound
self.limit = limit
}
public func play() {
guard !playing else {
return
}
playing = true
play(idx: 0)
}
private func play(idx: Int) {
guard idx < limit else {
return
}
AudioServicesPlayAlertSound(sound)
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
self?.play(idx: idx + 1)
}
}
}
在您的应用委托中
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
switch UIApplication.shared.applicationState {
case .active:
break
case .inactive: //phone off
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
sleep(1)
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
break
case .background: //not inside the app
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
sleep(1)
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
break
default: //while using the app
break
}
completionHandler()
}
费率很有用,谢谢