1

I want to notify nurses with remote notification that some bad thing happen to patients. So I want to vibrate 8 times when the s/he receive notification.

When my app receive remote push notification, it would trigger my notification service extension's

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

In that func, I call

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

But only the default vibration trigged one time.

Is it possible to vibrate many times when receive push notification? Or can I use custom vibration pattern to make it vibration many times? If it's not possible, can you provide some official document that indicate this? It's quite hard to prove one thing is impossible.

Marwen Doukh
  • 1,946
  • 17
  • 26
RedSIght
  • 538
  • 5
  • 13
  • Did you try to schedule 7 Local Notifications with a little schedule intervals? – Vladlex Jul 18 '19 at 11:47
  • 1
    Maybe this answer would help: https://stackoverflow.com/questions/45101474/user-notification-custom-vibration-pattern – Scriptable Jul 18 '19 at 12:15
  • @Scriptable Sorry. As the questioner there said "This answer is not related to my question" The answers provided there are not related to how to use custom vibration for notification at all. – RedSIght Jul 19 '19 at 02:40
  • @Vladlex My SA said more than one notification would mislead the nurses that there are more than one patient have problem. – RedSIght Jul 22 '19 at 02:01
  • Ok. Could you say please on what thread did you try to sleep/play sounds? – Vladlex Jul 23 '19 at 06:31
  • @Vladlex I did it in didReceive in notification service extension. I guess it's the main thread in notification service extension. – RedSIght Jul 23 '19 at 07:39
  • @RedSIght I've added an answer below. Not sure whether it works or not, but there is too much code to place it to the comment, sorry. – Vladlex Jul 23 '19 at 09:46

2 Answers2

1

Did you try to play sounds and sleep on different queues?

I wrote a simple class for playing a system sound, and in my case, it works well (but I didn't try to use it in an extension target):

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)
        }
    }
}
Vladlex
  • 845
  • 8
  • 16
0

in your app delegate

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

rate it was useful thanks

  • According to official document for UNUserNotificationCenterDelegate, "Use the methods of the UNUserNotificationCenterDelegate protocol to handle user-selected actions from notifications, and to process notifications that arrive when your app is running in the foreground." This method would only triggered in foreground or notification selected by user. I want it to vibrate in "background". I tried it but it just wasn't triggered when remote push received in background. Well, thanks anyway. – RedSIght Jul 19 '19 at 03:15