Broadcast communication

Updated at:

The cloud can send broadcast notifications to multiple devices. This topic describes how devices can handle these broadcasts. The cloud supports batch and targeted broadcasts. These two types use different topic formats. A device must handle a broadcast based on the corresponding topic format.

Batch broadcast

Batch broadcasts are system broadcasts that a device can receive without subscribing to a topic. The topic format is /sys/${pk}/${dn}/broadcast/request/+.

Targeted broadcast

For targeted broadcasts, a device must subscribe to a topic to receive notifications. The topic format is /broadcast/${pk}/${custom-action}.

Receiving broadcasts

To receive broadcast notifications, you must initialize the SDK and register a downstream listener. For targeted broadcasts, the device must subscribe to the topic defined by the sender. This topic must use the /broadcast/${pk}/${custom-action} format, where `custom-action` can be any valid value.

// Register the downstream listener.
LinkKit.getInstance().registerOnPushListener(notifyListener);
/**
 * Downstream listener. All downstream MQTT data from the cloud triggers this callback.
 */
private static IConnectNotifyListener notifyListener = new IConnectNotifyListener() {
    /**
         * onNotify is triggered only if shouldHandle does not block this topic.
         * @param connectId The connection type. Check if it is a persistent connection: connectId == ConnectSDK.getInstance().getPersistentConnectId().
         * @param topic The downstream topic.
         * @param aMessage The downstream data content.
         */
    @Override
    public void onNotify(String connectId, String topic, AMessage aMessage) {
        String data = new String((byte[]) aMessage.data);
        // Example of data returned from the server: data = {"method":"thing.service.test_service","id":"12*****67","params":{"vv":60},"version":"1.0.0"}
        ALog.d(TAG, "onNotify() called with: connectId = [" + connectId + "], topic = [" + topic + "], aMessage = [" + data + "]");
        if (ConnectSDK.getInstance().getPersistentConnectId().equals(connectId) && !TextUtils.isEmpty(topic) &&
                   topic.startsWith("/sys/" + DemoApplication.productKey + "/" + DemoApplication.deviceName + "/broadcast/request/")) {
            /**
                 * Topic format: /sys/${pk}/${dn}/broadcast/request/+
                 * No subscription is required. By default, no business ACK is needed, but you can implement a custom business ACK between the cloud and the device.
                 * Example: /sys/a14*****ZA/android_lp_test1/broadcast/request/1229336863924294656
                 * Note: The data from the sender must be Base64-encoded. Otherwise, garbled characters may appear on the device.
                 * For example, on the server-side: org.apache.commons.codec.binary.Base64.encodeBase64String("broadcastContent".getBytes())
                 */
            //
            ToastUtils.showToast("Received batch broadcast from cloud: topic=" + topic + ",data=" + data);
            //TODO: Add business logic to handle the batch broadcast.
        } else if (ConnectSDK.getInstance().getPersistentConnectId().equals(connectId) && !TextUtils.isEmpty(topic) &&
                   topic.startsWith("/broadcast/" + DemoApplication.productKey )) {
            //
            /**
                 * A subscription to this topic is required to receive messages. Topic format: /broadcast/${pk}/${custom-action}. This must match the topic that the sender uses in the cloud.
                 * Example: /broadcast/a14*****ZA/oldBroadcast
                 * Note: The data from the sender must be Base64-encoded. Otherwise, garbled characters may appear on the device.
                 * For example, on the server-side: org.apache.commons.codec.binary.Base64.encodeBase64String("broadcastContent".getBytes())
                 */
            ToastUtils.showToast("Received broadcast from cloud: topic=" + topic + ",data=" + data);
            //TODO: Add business logic to handle the broadcast.
        } else {
            ToastUtils.showToast("Received downstream data from cloud: topic=" + topic + ",data=" + data);
            /**
                 * TODO
                 * Add business logic based on the specific subscribed topic.
                 */
        }
    }

    /**
     * @param connectId The connection type. Check if it is a persistent connection: connectId == ConnectSDK.getInstance().getPersistentConnectId().
     * @param topic The downstream topic.
     * @return Specifies whether to handle this topic. If true, the onNotify callback is triggered. If false, onNotify is not called for data on this topic. Set this to true by default.
     */
    @Override
    public boolean shouldHandle(String connectId, String topic) {
        return true;
    }
};