Cumulocity Notificaion 2: Listen to device notifcation

Product/components used and version/fix level:

1017.0.268

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.
https://cumulocity.com/api/core/10.17.0/#operation/postNotificationSubscriptionResource

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”: [
“managedobjects”
]
},
“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();
        filterRepresentation.setApis(List.of("managedobjects"));
        notification.setContext("tenant");
        notification.setSubscription(subscriptionName);
        notification.setSubscriptionFilter(filterRepresentation);
        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(
                subscriber,
                subscription,
                1440,
                false);
tokenApi.create(tokenRequestRepresentation).getTokenString();

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() {

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

            @Override
            public void onNotification(Notification notification) {
                System.out.println(notification.getMessage());
                log.info("[Subscriber = {}] Notification received: <{}>", subscriber, notification.getMessage());
            }

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

            @Override
            public void onClose() {
                log.info("[Subscriber = {}] Connection was closed.", subscriber);
            }
        };

        log.info("[Subscriber = {}] Connecting WebSocket client ...", subscriber);
        final WebSocketClient client = new TooTallNateWebSocketClient(webSocketUri, callback);
        client.connect();
        return client;
    }

Check this for a full example: cumulocity-examples/notification2-examples/java-examples/src/main/java/c8y/example/notification/samples/Notification2Example.java at develop · Cumulocity-IoT/cumulocity-examples · GitHub

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.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.