[iOS][tips]notifyutil to handle notification system

Apple provides notifyutil to handle a notification system. According to their man command, we can see below description there.

notifyutil is a command-line utility for interacting with the notify(3) notification system and the notifyd(8) server. It may be used to
post notifications, detect and report notifications, and to examine and set the state values associated with notification keys.

We can use the command like below. The blow scenario is simple. Machine says “hello” if we send done signal to the process.

$ notifyutil -v -1 done && say "hello" &
...
$ notifyutil -v -p done # Say "hello" with cool machine voice

Below is easy how to use the util. (You can see the details via man)

$ notifyutil -h
usage: notifyutil [-q] [-v] [-z msec] [-M] [-R] [command ...]
    -q             quiet mode
    -v             verbose - prints time, key, state value, and type
    -z msec        pause msec milliseconds after posting [default 100]
    -M             multiplex notifications from notifyd over a single mach port
    -R             regenerate registrations if notifyd restarts
commands:
    -port          switch to mach port for subsequent registrations [default]
    -file          switch to file descriptor for subsequent registrations
    -check         switch to shared memory for subsequent registrations
    -signal [#]    switch to signal [#] for subsequent registrations
                   initial default for signal is 1 (SIGHUP)
    -dispatch      switch to dispatch for subsequent registrations
    -p key         post a notification for key
    -w key         register for key and report notifications
    -# key         (# is an integer value, eg "-1") register for key and report # notifications
    -g key         get state value for key
    -s key val     set state value for key

By the util, we can do below kind of hack.

https://github.com/wix/AppleSimulatorUtils/blob/master/applesimutils/applesimutils/main.m#L317-L330

https://github.com/appium/appium-ios-simulator/pull/183/

They are reliable than pure AppleScript in such use case.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.