Cumulocity Notificaion 2: Listen to device notifcation

Product/components used and version/fix level:


Detailed explanation of the problem:

I am developing a microservice that needs to listen to the device created/deleted notification. Here are the steps I implemented using Notification 2 API with JAVA SKD:

  1. Create a subscription with context = “tenant”, in the filter, I set the API: “managedobjects” (the document stated that with tenant context, I need to set the filterType, but I don’t know what type should be set, so I didn’t set it. Without filterType, the subscription is still created successfully). The subscription looks like this:
    “context”: “tenant”,
    “subscription”: “subscription01”,
    “filter”: {
    “apis”: [“managedobjects”]
  2. Go to the platform and try to create/delete a device

After step 2, I can’t see any notifications received.
Note: I tried to subscribe to “mo” context and I can receive measurements/alarms notification.

Error messages / full error message screenshot / log file:

No error, I just can’t see any notifications about device created/deleted

Question related to a free trial, or to a production (customer) instance?

production, APJ environment

1 Like

A tenant context subscription normally doesn’t need a source.

1 Like

Sorry, it’s just my mistake when copying the example. Below is the response when I query from API:
“nonPersistent”: false,
“subscriptionFilter”: {
“apis”: [
“context”: “tenant”,
“self”: “domain/notification2/subscriptions/977163”,
“subscription”: “TenantSubscription”,
“id”: “977163”,
“source”: {
“self”: “domain/inventory/managedObjects/my-tenant-id”,
“id”: “my-tenant-id”

Your code should look like this I assume?

        NotificationSubscriptionRepresentation notification = new NotificationSubscriptionRepresentation();
        final NotificationSubscriptionFilterRepresentation filterRepresentation = new NotificationSubscriptionFilterRepresentation();
        notification = subscriptionApi.subscribe(notification);

If so you have done step 1 of 3 to receive notifications.

2nd Step is to create a token for that subscription

NotificationTokenRequestRepresentation tokenRequestRepresentation = new NotificationTokenRequestRepresentation(

And finally establish a websocket connection with that token:

public WebSocketClient connectAndReceiveNotifications(String token, String subscriber) throws Exception {
        final URI webSocketUri = getWebSocketUrl(token);
        final NotificationCallback callback = new NotificationCallback() {

            public void onOpen(URI uri) {
      "[Subscriber = {}] Connected to Cumulocity notification service over WebSocket {}", subscriber, uri);

            public void onNotification(Notification notification) {
      "[Subscriber = {}] Notification received: <{}>", subscriber, notification.getMessage());

            public void onError(Throwable t) {
                log.error("[Subscriber = {}] We got an exception: " + t, subscriber);

            public void onClose() {
      "[Subscriber = {}] Connection was closed.", subscriber);
        };"[Subscriber = {}] Connecting WebSocket client ...", subscriber);
        final WebSocketClient client = new TooTallNateWebSocketClient(webSocketUri, callback);
        return client;

Check this for a full example:

Now you can try to create/delete managedobject in the inventory and should receive notifications

1 Like

Thank you, Stefan,

Of course, I’ve done steps 2 and 3, the websocket is ready.
Actually, I’ve made 2 subscriptions, 1 for “mo” context and 1 for “tenant” context, then created 2 tokens and 2 websocket for them.
I can receive the notification from “mo” context, but only “tenant” context is not working.

That’s hard to debug without the code.
But I could guess: Did you re-use the same subscription name and switched from tenant context to mo (for testing)?
If so this could lead issues when the subscription was in the mo context first but now changed to tenant. Try a new subscription name and subscriber name for the tenant.

If this is still not working afterwards I guess there is something wrong with the websocket message handler not receiving the tenant messages for managed objects.

I am using 2 different names for 2 contexts. And FYI, with “tenant” context, I can receive notification for alarm. I think there is a bug with manageobject.

Hi Khiem, I can’t reproduce your problem. I created two subscriptions and two websocket at the same time but in Python. One with ‘tenant’ context for managedObjects and alarms, one with ‘mo’ context for alarms. They have different subscription names and subscriber names. Both of them can receive expected notifications when I create new alarms and new devices. So the managedObject API works fine for me. Could you check your service and test the ‘tenant’ context by creating new devices again?

Maybe you can share your code snippet here. I also think there is something wrong with your microservice managing the subscriptions / connections.

Thank you,

Looks like there was something wrong with the APJ environment. I think they have fixed it and now I can see the managedObject notification with tenant context.
But do you know why only the CREATE events are coming, I can’t see the DELETE events with tenant context

Hi Khiem, with “Tenant” context, you will not receive messages about DELETE operations. You can use “MO” context to receive DELETE operations.