Yes you can do this by using AccessibilityService. Where an accessibility service runs in the background and receives callbacks by the system when AccessibilityEvents are fired. Such events denote some state transition in the user interface, for example, the focus has changed, a button has been clicked, etc. Such a service can optionally request the capability for querying the content of the active window.
Read my bolog post Capture Toast or Notification using Accessibility Service for code example.