Handler

    Handler


    Article summary

    In the Shoplive Player, notifications that occur are passed to the client through a Handler function for necessary processing.


    ShopLive.setHandler

    This is ShopLive event handler.

    fun setHandler(handler: ShopLiveHandler)
    

    sample code

    class MainActivity : AppCompatActivity() {
    
        private val handler = object : ShopLiveHandler() {
    
            override fun handleNavigation(context: Context, url: String) {
              	Log.d(TAG, "handleNavigation >> url=$url")
            }
    
            override fun onChangeCampaignStatus(context: Context, campaignStatus: String) {
              	Log.d(TAG, "onChangeCampaignStatus >> $campaignStatus")
            }
        }
    
        private fun init() {
          	ShopLive.setHandler(handler)
        }
    }
    

    ShopLiveHandler.handleNavigation

    When you select a product, banner, etc. in Shoplive, information about the selected product or banner is delivered through handleNavigation

    ❗️When the url is passed to handleNavigation, the player switches to PIP, so there is no need to call ShopLive.startPictureInPicture().

    If you do not want to switch to PIP, please refer to the ShopLive.setNextActionOnHandleNavigation interface.


    fun handleNavigation(context: Context, url: String)
    

    parameter

    explanation

    url

    URL to go to when selecting a product or banner

    sample code

    private val handler = object : ShopLiveHandler() {
        override fun handleNavigation(context: Context, url: String) {
            val type: ActionType = "your_action_type"
            when (type) {
                ActionType.PIP, ActionType.CLOSE -> {
                    val intent: Intent = Intent(this@SampleActivity, WebViewActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
                    intent.putExtra("url", url)
                    startActivity(intent)
                }
                ActionType.KEEP -> {
                    val webDialogFragment = WebViewDialogFragment(url)
                    ShopLive.showDialogFragment(webDialogFragment)
                }
            }
        }
    }
    

    Coupon

    This is the status used to set the coupon's activation status when completing the coupon processing.

    public enum CouponPopupStatus {
        SHOW,  // 쿠폰 다시 활성
        HIDE,  // 쿠폰 사라짐
        KEEP   // 상태 유지
    }
    

    Sets the type of notification message that appears when completing coupon processing, if there is one.

    public enum CouponPopupResultAlertType {
        ALERT,  // 얼럿
        TOAST   // 토스트
    }
    

    ShopLiveHandler.handleDownloadCoupon

    When a coupon is selected (tapped) in Shoplive, the coupon information is transmitted to the client. The client's coupon processing result is then communicated back to the Shoplive Android SDK through a callback, which is used to set the coupon status in the Shoplive Player.

    fun handleDownloadCoupon(context: Context, couponId: String, callback: ShopLiveHandlerCallback)
    

    parameter

    explanation

    context

    Context

    couponId

    Selected coupon ID

    callback

    ShopLive will notify you when the coupon is processed.callback

    callback ShopLiveHandlerCallback.couponResult

    When the coupon download is completed, it is called to deliver information regarding the success or failure of the result, along with any relevant messages.

    /**
    * @param isDownloadSuccess - true: success, false: fail
    * @param message - message to be delivered
    * @param couponStatus - SHOW: Showing coupon, HIDE: Hiding coupon, KEEP: Keeping current status
    * @param alertType - ALERT: popup, TOAST: toast message
    * */
    fun couponResult(
        isDownloadSuccess: Boolean, 
        message: String?, 
        couponStatus: ShopLive.CouponPopupStatus, 
        alertType: ShopLive.CouponPopupResultAlertType) {}
    

    sample code

    private val handler = object : ShopLiveHandler() {
        override fun handleDownloadCoupon(
          context: Context,
          couponId: String,
          callback: ShopLiveHandlerCallback
        ) {
            val builder: AlertDialog.Builder = Builder(context)
            builder.setTitle("Coupon download")
            builder.setMessage("couponId $couponId")
            builder.setPositiveButton("success") { dialog, which ->
                // callback for success
                callback.couponResult(
                  true,
                  "Coupon download success",
                  ShopLive.CouponPopupStatus.HIDE,
                  ShopLive.CouponPopupResultAlertType.TOAST
                )
            }
    
            builder.setNegativeButton("실패") { dialog, which ->
                // callback on fail
                callback.couponResult(
                  false,
                  "Coupon download fail",
                  ShopLive.CouponPopupStatus.SHOW,
                  ShopLive.CouponPopupResultAlertType.ALERT
                )
            }
            val dialog: Dialog = builder.create()
            dialog.show() 
        }
    }
    

    ShopLiveHandler.handleCustomAction

    In a pop-up menu, set the event to custom, and when the pop-up is clicked, event pass the pop-up information such as pop-up's id, type, and payload.

    fun handleCustomAction(context: Context, id: String, type: String, payload: JSONObject, callback: ShopLiveHandlerCallback)
    

    parameter

    explanation

    context

    Context

    id

    Coupon or banner ID

    type

    Coupon or banner type

    payload

    User-defined payload

    callback

    customWhen processing is complete, a callback will notify ShopLive

    callback ShopLiveHandlerCallback.customActionResult

    To communicate the success or failure status, as well as any associated messages, when the customAction processing is completed, you call this to send those messages.

    /**
    * @param isSuccess - true: 성공, false: 실패
    * @param message - 성공 또는 실패 메시지
    * @param couponStatus - SHOW: 재활성, HIDE: 숨김, KEEP: 상태 유지
    * @param alertType - ALERT: 팝업, TOAST: 메시지
    * */
    fun customActionResult(
        isSuccess: Boolean, 
        message: String?, 
        couponStatus: ShopLive.CouponPopupStatu, 
        alertType: ShopLive.CouponPopupResultAlertType?) {}
    

    sample code

    private val handler = object : ShopLiveHandler() {
    
        override fun handleCustomAction(
          context: Context,
          id: String,
          type: String,
          payload: JSONObject,
          callback: ShopLiveHandlerCallback
        ) {
            val builder: AlertDialog.Builder = Builder(context)
            builder.setTitle("커스텀 액션(팝업)")
            builder.setMessage("id: $id" + " type: " + R.attr.type + " payload: " + payload.toString())
            builder.setPositiveButton("성공") { dialog, which ->
                // 성공했을 때 콜백 설정
                callback.couponResult(
                  true,
                  "Custom action(Popup) 처리가 성공하였습니다.",
                  ShopLive.CouponPopupStatus.HIDE,
                  ShopLive.CouponPopupResultAlertType.TOAST
                )
            }
    
            builder.setNegativeButton("실패") { dialog, which ->
                // 실패했을 때 콜백 설정
                callback.couponResult(
                  false,
                  "Custom action(Popup) 처리가 실패하였습니다.",
                  ShopLive.CouponPopupStatus.SHOW,
                  ShopLive.CouponPopupResultAlertType.ALERT
                )
            }
    
            val dialog: Dialog = builder.create()
            dialog.show()
        }
    }
    

    ShopLiveHandler.handleShare

    When you choose to share during a live broadcast, it is the Handler. To implement it directly without using the Android Share Sheet, you must override it.

    fun handleShare(context: Context, shareUrl: String) {}
    

    parameter

    explanation

    context

    Context

    shareUrl

    URL or scheme to share

    sample code

    private val handler = object : ShopLiveHandler() {
        override fun handleShare(context: Context?, shareUrl: String?) {
          	Toast.makeText(context, "url=$shareUrl", Toast.LENGTH_SHORT).show()
        } 
    }
    

    ShopLiveHandler.onChangeCampaignStatus

    Handler when the broadcast state changes.

    /**
    * @param context - Context
    * @param campaignStatus - READY | ONAIR | CLOSED
    */
    fun onChangeCampaignStatus(context: Context, campaignStatus: String) {}
    

    parameter

    explanation

    context

    Context

    campaignStatus

    campaignStatus


    READY, ONAIR, CLOSED

    sample code

    private val handler = object : ShopLiveHandler() {
        override fun onChangeCampaignStatus(context: Context, campaignStatus: String) {
          	Log.d(TAG, "onChangeCampaignStatus >> $campaignStatus")
        }
    }
    

    ShopLiveHandler.onCampaignInfo

    This is broadcast information in JSON Object format.

    fun onCampaignInfo(campaignInfo: JSONObject) {}
    

    parameter

    explanation

    campaignInfo

    campaignInfo


    예) {'title':'방송 제목'}

    sample code

    private val handler = object : ShopLiveHandler() {
        override fun onCampaignInfo(campaignInfo: JSONObject) {
          	Log.d(TAG, "onCampaignInfo >> $campaignInfo") 
        }
    }
    

    ShopLiveHandler.onChangedPlayerStatus

    Receives the Shoplive Player status.

    fun onChangedPlayerStatus(isPipMode: Boolean, playerLifecycle: ShopLive.PlayerLifecycle) { }
    

    parameter

    explanation

    isPipMode

    Current PIP?

    playerLifecycle

    Shoplive Player Status , ,


    CREATEDCLOSINGDESTROYED

    enum PlayerLifecycle

    explanation

    CREATED

    Player created

    CLOSING

    Before the player ends

    DESTROYED

    Player terminated

    sample coden

    private val handler = object : ShopLiveHandler() {
        override fun onChangedPlayerStatus(
          isPipMode: Boolean,
          playerLifecycle: ShopLive.PlayerLifecycle) {
            when(playerLifecycle) {
                ShopLive.PlayerLifecycle.CREATED -> {
    
                }
                ShopLive.PlayerLifecycle.CLOSING -> {
    
                }
                ShopLive.PlayerLifecycle.DESTROYED -> {
    
                }
            }
        }
    }
    

    ShopLiveHandler.onSetUserName

    Called when the username has changed.

    fun onSetUserName(jsonObject: JSONObject) {}
    

    parameter

    explanation

    jsonObject

    User information


    example){'userId':'123', 'userName':'test'}

    sample code

    private val handler = object : ShopLiveHandler() {
        override fun onSetUserName(jsonObject: JSONObject?) {
         	 Log.d(TAG, "onSetUserName >> $jsonObject")
        }
    }
    

    ShopLiveHandler.handlePreview

    When you select the Shoplive preview screen, it's a Handler.

    Choosing the preview screen will lead you to the corresponding broadcast (default behavior). To implement it directly without entering the broadcast, you must override it.

    However, please note that this event is only delivered when using the preview screen provided as an overlay on top of other apps. In the OS PIP (Picture-in-Picture) mode, the preview event won't be delivered, and it will automatically enter full-screen broadcast mode.

    fun handlePreview(context: Context, campaignKey: String) {}
    

    parameter

    explanation

    context

    Context

    campaignKey

    Campaign (broadcast) key

    sample code

    private val handler = object : ShopLiveHandler() {
        override fun handlePreview(context: Context, campaignKey: String) {
          	Log.d(TAG, "campaignKey=$campaignKey") 
        }
    }
    

    ShopLiveHandler.onReceivedCommand

    It is called when a ‘command’ message is received.

    fun onReceivedCommand(context: Context, command: String, data: JSONObject) {}
    

    parameter

    explanation

    context

    Context

    command

    command passed

    data

    data passed


    sample code

    private val handler = object : ShopLiveHandler() {
        override fun onReceivedCommand(context: Context, command: String, data: JSONObject) {
            when(command) {
                "LOGIN_REQUIRED" -> {
    
                }
                "CLICK_PRODUCT_CART" -> {
    
                }
                "EVENT_LOG" -> {
                    // parsing example 1
                    val name = data.getString("name")
                    val feature = data.getString("feature")
                    val parameter = data.getString("parameter")
                    Toast.makeText(
                        context,
                        "name : $name, feature : $feature, parameter : $parameter",
                        Toast.LENGTH_SHORT
                    ).show()
                    // parsing example 2
                    val log = Gson().fromJson(data.toString(), ShopLiveLog.Data::class.java)
                    if (Options.isShowClickLog()) {
                    Toast.makeText(
                        context,
                        "name : ${log.name}, feature : ${log.feature}, campaignKey : ${log.campaignKey}, parameter : ${log.parameter}",
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }
        }
    }
    

    ShopLiveHandler.log

    User behavior can be tracked.

    ShopLiveLog.Data

    field

    type

    description

    name

    String

    Name of Event

    feature

    String

    Types of Events , ,clickshowaction

    campaignKey

    String?

    Unique Key to Broadcasting

    parameter

    Map<String, Any>?

    Additional related parameters


    sample code

    private val handler = object : ShopLiveHandler() {
        ...
        override fun log(data: ShopLiveLog.Data) {
            Log.d(TAG, "name=${data.name} feature=${data.feature} campaignKey=${data.campaignKey} parameter=${data.parameter}")
        }
        ...
    }
    

    Related Links
    EventLog Tracking User Behavior


    ShopLiveHandler.onError

    This conveys messages related to error situations occurring either before or during a broadcast.

    fun onError(context: Context, code: String, message: String) {}
    

    parameter

    explanation

    context

    Context

    code

    error code

    message

    error message

    sample code

    private val handler = object : ShopLiveHandler() {
        override fun onError(context: Context, code: String, message: String) {
          	Log.d(TAG, "code=$code, message=$message")
        }
    }
    


    What's Next