振动具有通知服务扩展功能的iPhone 7,但无法在后台与iOS 12.1一起使用

时间:2018-12-06 11:56:46

标签: ios swift push-notification swift4.2 ios-background-mode

我两次使用Notification Service Extension振动设备,后台模式开启,以下方法在iOS 1112.0.1上有效,但在iOS 12.1上无效

背景模式如下:

enter image description here

这是代码:

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)
}

}

1 个答案:

答案 0 :(得分:0)

我发现,如果关闭振动设置,它将无法正常工作。 (转到系统设置\声音=>振铃振动。)

如果您发送的推送通知仅带有声音和徽章,但没有文本提示,则如果您的手机未处于静音模式,它将播放声音,但是如果您处于静音模式,则不会播放声音

现在,如果您向该通知添加文本警报,即使在振动模式下,它也可以很好地播放声音。

围绕此可能还要进行更多测试。例如:如果您发送自定义键/值对,它会播放声音吗?不确定。