# addEventHandler

open fun addEventHandler(handler: GamebaseEventHandler)

Add a Gamebase event handler to be called when every events are arrived. You have to convert the message data to VO below according to the category value.

    GamebaseEventCategory.LOGGED_OUT : GamebaseEventLoggedOutData
    GamebaseEventCategory.SERVER_PUSH_APP_KICKOUT_MESSAGE_RECEIVED : GamebaseEventServerPushData
    GamebaseEventCategory.SERVER_PUSH_APP_KICKOUT : GamebaseEventServerPushData
    GamebaseEventCategory.SERVER_PUSH_TRANSFER_KICKOUT : GamebaseEventServerPushData
    GamebaseEventCategory.OBSERVER_LAUNCHING : GamebaseEventObserverData
    GamebaseEventCategory.OBSERVER_HEARTBEAT : GamebaseEventObserverData
    GamebaseEventCategory.OBSERVER_NETWORK : GamebaseEventObserverData
    GamebaseEventCategory.PURCHASE_UPDATED : PurchasableReceipt
    GamebaseEventCategory.PUSH_RECEIVED_MESSAGE : PushMessage
    GamebaseEventCategory.PUSH_CLICK_MESSAGE : PushMessage
    GamebaseEventCategory.PUSH_CLICK_ACTION : PushAction
    LOGGED_OUT : This event is fired when the Gamebase Access Token expires and the user is logged out.
    SERVER_PUSH : Receive messages from the Gamebase server.
    OBSERVER : This is an event that fires when the launch, login account(heartbeat), or network connection 'status changes'.
    PURCHASE_UPDATED : Promotion payment events can be received.
    PUSH_RECEIVED_MESSAGE : This event operates when a Push message is received. It has a 'isForeground' boolean value in the extras field.
    PUSH_CLICK_MESSAGE : This event is executed when the Push message is clicked. Note that there is no 'isForeground' boolean value.
    PUSH_CLICK_ACTION : This is an event that is triggered when the action button added through the rich message function is clicked.

Content copied to clipboard

Example Usage:

    public static void addGamebaseEventHandler(Activity activity) {
        Gamebase.addEventHandler(new GamebaseEventHandler() {
            @Override
            public void onReceive(@NonNull GamebaseEventMessage message) {
                switch (message.category) {
                    case GamebaseEventCategory.SERVER_PUSH_APP_KICKOUT:
                    case GamebaseEventCategory.SERVER_PUSH_TRANSFER_KICKOUT:
                        GamebaseEventServerPushData serverPushData = GamebaseEventServerPushData.from(message.data);
                        if (serverPushData != null) {
                            processServerPush(activity, message.category, serverPushData);
                        }
                        break;
                    case GamebaseEventCategory.OBSERVER_LAUNCHING:
                    case GamebaseEventCategory.OBSERVER_HEARTBEAT:
                    case GamebaseEventCategory.OBSERVER_NETWORK:
                        GamebaseEventObserverData observerData = GamebaseEventObserverData.from(message.data);
                        if (observerData != null) {
                            processObserver(activity, message.category, observerData);
                        }
                        break;
                    case GamebaseEventCategory.PURCHASE_UPDATED:
                        PurchasableReceipt receipt = PurchasableReceipt.from(message.data);
                        if (receipt != null) {
                            // If the user got item by 'Promotion Code' or 'Lazy purchase', or 'Parents permission',...,
                            // this event will be occurred.
                        }
                        break;
                    case GamebaseEventCategory.PUSH_RECEIVED_MESSAGE:
                        PushMessage pushMessage = PushMessage.from(message.data);
                        if (pushMessage != null) {
                            // When you received push message.
                            try {
                                JSONObject json = new JSONObject(pushMessage.extras);
                                // There is 'isForeground' information.
                                boolean isForeground = json.getBoolean("isForeground");
                                Object customValue = json.get("YourCustomKey");
                            } catch (Exception e) {}
                        }
                        break;
                    case GamebaseEventCategory.PUSH_CLICK_MESSAGE:
                        PushMessage clickedMessage = PushMessage.from(message.data);
                        if (clickedMessage != null) {
                            // When you clicked push message.
                        }
                        break;
                    case GamebaseEventCategory.PUSH_CLICK_ACTION:
                        PushAction pushAction = PushAction.from(message.data);
                        if (pushAction != null) {
                            // When you clicked action button by 'Rich Message'.
                        }
                        break;
                    default:
                        break;
                }
            }
        });
    }
    void processServerPush(String category,
                           GamebaseEventServerPushData data) {
        if (category.equals(GamebaseEventCategory.SERVER_PUSH_APP_KICKOUT)) {
            // Kicked out from Gamebase server.(Maintenance, banned or etc..)
            // Return to title and initialize Gamebase again.
        } else if (category.equals(GamebaseEventCategory.SERVER_PUSH_TRANSFER_KICKOUT)) {
            // If the user wants to move the guest account to another device,
            // if the account transfer is successful,
            // the login of the previous device is released,
            // so go back to the title and try to log in again.
        }
    }

    void processObserver(String category,
                         GamebaseEventObserverData data) {
        if (category.equals(GamebaseEventCategory.OBSERVER_LAUNCHING)) {
            int launchingStatusCode = data.code;
            String launchingMessage = data.message;
            switch (launchingStatusCode) {
                case LaunchingStatus.IN_SERVICE:
                    // Finished maintenance.
                    break;
                case LaunchingStatus.INSPECTING_SERVICE:
                case LaunchingStatus.INSPECTING_ALL_SERVICES:
                    // Under maintenance.
                    break;
                ...
            }
        } else if (category.equals(GamebaseEventCategory.OBSERVER_HEARTBEAT)) {
            int errorCode = data.code;
            switch (errorCode) {
                case GamebaseError.INVALID_MEMBER:
                    // You can check the invalid user session in here.
                    // ex) After transferred account to another device.
                    break;
                case GamebaseError.BANNED_MEMBER:
                    // You can check the banned user session in here.
                    break;
            }
        } else if (category.equals(GamebaseEventCategory.OBSERVER_NETWORK)) {
            int networkTypeCode = data.code;
            // You can check the changed network status in here.
            if (networkTypeCode == NetworkManager.TYPE_NOT) {
                // Network disconnected.
            } else {
                // Network connected.
            }
        }
    }

Content copied to clipboard

#### Since

Added 2.10.0

#### Parameters

handler

The listener that will run.

#### See also

GamebaseEventHandler
GamebaseEventMessage
GamebaseEventServerPushData
GamebaseEventObserverData
PurchasableReceipt
PushMessage
PushAction