Developer Console

Topic Based Messaging in A3L

Topic Based Messaging allows you to send messages to groups of users subscribed to the same topic. To use Topic Based Messaging to send messages, your app instance on an Android or Fire OS device must subscribe to a topic. A3L Messaging provides abstractions for the subscribeToTopic() and unsubscribeFromTopic() methods found in the Amazon Device Messaging (ADM) and Firebase Cloud Messaging (FCM) APIs.

Register your security profile

For Fire OS devices, you must register your security profile for Topic Based Messaging through ADM. For instructions, see Register a security profile for Topic Based Messaging. This step is not required for using Topic Based Messaging in FCM.

Subscribe or unsubscribe to a topic

To subscribe your app to an ADM or FCM topic, call the A3L Messaging subscribeToTopic() method and pass it the topic name. This method returns a Task that can be used by a completion listener to determine whether the subscription succeeded, as shown in the following code.

A3LMessaging.subscribeToTopic(topic).addOnCompleteListener(
        new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                String msg = "Subscribed";
                if (!task.isSuccessful()) {
                    msg = "Subscribe failed";
                }
                Log.d(TAG, msg);
            }
        }
);

To unsubscribe your app from an ADM or FCM topic, call the A3L Messaging unsubscribeFromTopic() method and pass it the topic name. This method returns a Task that can be used by a completion listener to determine whether unsubscribing was successful, as shown in the following code.

A3LMessaging.unsubscribeFromTopic(topic).addOnCompleteListener(
        new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                String msg = "Unsubscribed";
                if (!task.isSuccessful()) {
                    msg = "Unsubscribe failed";
                }
                Log.d(TAG, msg);
            }
        }
);

Test with server-side scripts

To test Topic Based Messaging, you can use these server-side scripts to send a message to a topic.

  • ADMSendMessageToTopic.py is a python script that sends a topic-based message to a Fire OS device.
  • FCMSendMessageToTopic.py is a python script that sends a topic-based message to an Android device.

Click the following buttons to get the code for the scripts. To send a test message, copy the code, paste it into an editor, and update the values indicated in the comments. Then save each as a .py file and run the scripts.

Test with solution-specific APIs

You can also test Topic Based Messaging using solution-specific APIs. For detailed instructions on how to send a message to a topic through ADM and FCM APIs, use the following guides.


Last updated: Feb 27, 2023