Only show notification if the user is on the computer

What is the best way to determine if the user is currently on his computer, that is, use it in some way. We have an application that should issue notifications (banner alerts using the close button) if the user is on his computer.

For example, imagine a stock trading application that warns the user of various temporary information that may only be relevant for a few minutes. If the user is away from their computer, they do not want to fire 20 warnings without warning.

+4
source share
3 answers

Technical information. I don’t know how to look at inaction, but I think you can use the timeout for notifications to close yourself after a time interval and / or limit the displayed notifications to a few, so the newest one will automatically close the oldest one.

From the apple documentation :

Note. The user wakes up for more than 15 minutes after the scheduled notification is scheduled to run, it will be discarded. If the notification is repeated at intervals of less than 15 minutes, then expires in 1 minute. Expired notifications are simply discarded if they are repeated, in which case they remain on the schedule and just fire again later.

+7
source

If you reference ApplicationServices or higher, try:

CFTimeInterval idleTime = CGEventSourceSecondsSinceLastEventType(kCGEventSourceStateCombinedSessionState, kCGAnyInputEventType); 

The time since the last event for the event source.

The kCGAnyInputEventType event type will report the latest timestamp for any input event, keyboard, mouse, or tablet. Various events defined by the system and the application do not affect this type of event time.

Again, publishing a program or application from a login session should use kCGEventSourceStateCombinedSessionState.

The device user space that interprets the hardware state and generates events must use the kCGEventSourceStateHIDS SystemState.

CG_EXTERN CFTimeInterval CGEventSourceSecondsSinceLastEventType (source CGEventSourceStateID, event type CGEventType) AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER;

+7
source

Instead, you can check user downtime. Assuming that the user leaves his computer and leaves, IOKit will report downtime on the HID (human interface devices). The code looks something like this:

 int64_t getIdleTime(void) { io_iterator_t iter; int64_t idle = 0; // Step 1: Prepare a matching dictionary for "IOHIDSystem", which is the I/O Kit // class which we will query if (IOServiceGetMatchingServices (kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"), &iter) == KERN_SUCCESS) { io_registry_entry_t entry = IOIteratorNext(iter); // Step 2: If we get the classes, get the property: if (entry) { CFMutableDictionaryRef dict; // Query the HIDIdleTime property, if present. if (IORegistryEntryCreateCFProperties(entry, &dict, kCFAllocatorDefault, 0) == KERN_SUCCESS) { CFNumberRef prop = (CFNumberRef) CFDictionaryGetValue(dict, CFSTR("HIDIdleTime")); if (prop) { int64_t nsIdle; // Value is in Nanoseconds, you might want to convert if (CFNumberGetValue(prop, kCFNumberSInt64Type, &nsIdle)) { idle = (nsIdle / 1000000000); } } CFRelease(dict); // Be nice. Clean up } IOObjectRelease(entry); // as well as here.. } IOObjectRelease(iter); // and here.. } return idle; } 
+2
source

Source: https://habr.com/ru/post/1438780/


All Articles