【问题标题】:Is it possible to vibrate multiple times on receiving remote push notification in background mode?在后台模式下接收远程推送通知是否可以多次振动?
【发布时间】:2019-07-18 10:49:18
【问题描述】:

我想通过远程通知通知护士患者发生了一些不好的事情。所以我想在她/他收到通知时振动 8 次。

当我的应用收到远程推送通知时,它会触发我的通知服务扩展的

func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { 

在那个函数中,我调用

AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
sleep(1)      
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))

但是只触发了一次默认的振动。

收到推送通知时是否可以多次振动? 或者我可以使用自定义振动模式使其振动多次吗? 如果不可能,您能否提供一些表明这一点的官方文件?很难证明一件事是不可能的。

【问题讨论】:

  • 您是否尝试过安排 7 次本地通知,但安排间隔很小?
  • 也许这个答案会有所帮助:stackoverflow.com/questions/45101474/…
  • @Scriptable 抱歉。正如那里的提问者所说“这个答案与我的问题无关”那里提供的答案与如何使用自定义振动进行通知完全无关。
  • @Vladlex 我的 SA 表示,不止一份通知会误导护士,认为不止一名患者有问题。
  • 好的。你能说一下你在哪个线程上尝试睡觉/播放声音吗?

标签: ios swift background vibration remote-notifications


【解决方案1】:

您是否尝试在不同的队列中播放声音和睡眠?

我编写了一个简单的类来播放系统声音,就我而言,它运行良好(但我没有尝试在扩展目标中使用它):

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

【讨论】:

    【解决方案2】:

    在您的应用委托中

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

    评价它很有用,谢谢

    【讨论】:

    • 根据 UNUserNotificationCenterDelegate 的官方文档,“使用 UNUserNotificationCenterDelegate 协议的方法来处理用户从通知中选择的操作,并处理当您的应用在前台运行时到达的通知。”此方法只会在用户选择的前台或通知中触发。我希望它在“背景”中振动。我试过了,但是在后台收到远程推送时它没有被触发。好吧,无论如何,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多