Integrate with FCM Push

Last Updated on : 2024-06-26 09:45:56download

This topic describes how to integrate Google’s Firebase Cloud Messaging (FCM) into your Android project. The instructions in this topic apply to your services deployed outside mainland China.

Preparation

Configure FCM

Configure Sender ID and Server Key and upload the google-services.json file in the Push (Google FCM) section at Tuya Developer Platform > App > App SDK > Optional Setting > Certificates. For more information, see Register FCM.

We recommend that you copy and paste the required content to the Sender ID and Server Key fields to ensure correct settings.

Integrate with FCM Push

Register a token

Call the onTokenRefresh method that inherits the class FirebaseInstanceIdService to listen for the generated token and register it to the cloud.

ThingHomeSdk.getPushInstance().registerDevice(String token, String pushProvider, new IResultCallback() {
    @Override
    public void onError(String code, String error) {
    }

    @Override
    public void onSuccess() {

    }
});

Parameters

Parameter Description
token The token generated by FCM.
pushProvider The type of push notification. Set the value to fcm.

Receive and process messages

Call the onMessageReceived method that inherits the class FirebaseMessagingService to receive and process push notifications. These messages are included in message.getData().

public class MyFcmListenerService extends FirebaseMessagingService {
    public static final String TAG = "MyFcmListenerService";
    public static HashMap<String, Long> pushTimeMap = new HashMap<>();

    @Override
    public void onMessageReceived(RemoteMessage message) {
        Log.d(TAG, "FCM message received" + message.getData().toString());
    }
}

Unbind a user

You can call the following FCM method to remove a registration token. This applies when you want to unbind your app from users in specific scenarios, such as logout.

FirebaseInstanceId.getInstance().deleteInstanceId();

The API methods mentioned in this topic are based on SDK v20.1.6 and they are not recommended for use. To integrate with a later version, follow the instructions in FirebaseInstanceId.

Send push notifications

After you integrate with the capabilities of push notifications, you can configure push notifications on the Tuya Developer Platform.

Create an operation push notification

Go to Tuya Developer Platform > Marketing push and create a marketing push notification. This allows users to receive your in-app messages regarding new arrivals, trending information, and more, and improves user engagement.

Integrate with FCM Push

Create a device push notification

To create a device push notification, go to Tuya Developer Platform > Product > Notifications. For more information, see Configure Push Notification.

Integrate with FCM Push