Not sure if this is helpful, but the linked post explains the use of an alarm listener.
Maybe this will define a small piece of what you are trying to do.