Pub/Sub notifications provide a way for you to receive alerts about newly
enrolled devices, device reports, and recently issued commands.

To set up Pub/Sub notifications, you need to enable the Pub/Sub API and create
a topic. To receive messages published to a topic, create a subscription to
that topic. The subscription connects the topic to a subscriber application that
receives and processes messages published to the topic. After you create a
subscription, you need to grant Android Device Policy permission to
publish to your topic.

5. Update enterprise to support notifications

To connect notifications about an enterprise to the topic you created, call
enterprises.patch
and specify the following parameters:

pubsubTopic: The name of your pub/sub topic in the form
projects/{project}/topics/{topic}.

enabledNotificationTypes: Include all the notification types you
wish to receive. Choose from ENROLLMENT, STATUS_REPORT, and COMMAND.

6. Use the Pub/Sub API to get notifications

A subscription can use either the push or pull mechanism for message delivery.
Guidelines and directions on how to receive notifications through both of these
mechanisms are available in the Pub/Sub Subscriber Guide.

Message format

Messages take the form of a PubsubMessage.
The attributes field of the message contains an attribute with key
notificationType and value set to the notification type that
triggered the message (e.g. ENROLLMENT). The data field of the message
contains the JSON representation of the resource that was updated, encoded as a
UTF-8 string. For COMMAND notifications, the type of the resource is
Operation. For all other notifications, the type of the resource is
Device.

When setting the pubsubTopic on an Enterprise, an initial
message will be published with notificationType set to test. This message is
sent to verify Android Device Policy has the publish permission on the topic and
should be ignored.

Note: When using the Pub/Sub REST API, the data field is encoded as a base64
string. To get the resource JSON, first perform base64 decoding on the contents
of data, then interpret the resulting bytes as a UTF-8 string.