Invites Guide

Prerequisites

Invites Integration

An additional deep linking function of the StreamLayer SDK is the way we integrate Branch.io to invite users. Please see the documentation in the StreamLayer Studio section of this document. (Note Branch.io service fees apply.)

  1. Create a new account on Branch.io or use your existing account.
  2. Link your Branch.io credentials with your StreamLayer account in StreamLayer Studio.
  3. Install Branch.io on the client side and add Branch.io keys into your gradle file. This is optional step - you can put keys directly to your Android Manifest, but this way is more flexible. You can find your Branch key by logging into your Branch Dashboard

defaultConfig {
        manifestPlaceholders += [branchIoScheme: "your-app-scheme"]
        manifestPlaceholders += [branchIoKey: "your-branchIo-sdk-key"]
        manifestPlaceholders += [branchIoHost: "your-branchIo-host"]
        manifestPlaceholders += [branchIoAlternateHost: "your-branchIo-alternate-host"]
    }
  1. Configure the Android Manifest

add meta data to Application:

<!-- META DATA -->
        <meta-data
            android:name="io.branch.sdk.BranchKey"
            <!-- link to value from gradle file, you can set it manually -->
            android:value="${branchIoKey}" />
        <meta-data
            android:name="io.branch.sdk.TestMode"
            android:value="false" />

and configure Activity which should handle Branch IO links:

<activity
            android:name=".ui.LiveActivity"
            android:launchMode="singleTask">
          
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    <!-- link to value from gradle file, you can set it manually -->
                    android:host="${branchIoHost}"
                    android:scheme="https" />
                <data
                    <!-- link to value from gradle file, you can set it manually -->
                    android:host="${branchIoAlternateHost}"
                    android:scheme="https" />

            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="open"
                    android:scheme="${branchIoScheme}" />
            </intent-filter>

        </activity>
  1. Initialize the Branch.io in the Application class:
class App : Application() {

    override fun onCreate() {
        super.onCreate()
        Branch.getAutoInstance(this)
        StreamLayer.initializeApp(this, BuildConfig.SL_SDK_KEY)
    }
}
  1. Configure your Activity, which should handle invites user interface:
<activity
            android:name=".ui.LiveActivity"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data
                    android:host="${applicationId}"
                    android:pathPrefix="/invite"
                    android:scheme="streamlayer" />
            </intent-filter>
        </activity>

We use a custom scheme streamLayer and path prefix /invite so we can directly invoke the activity from the PendingIntent. The value in ${applicationId} will be automatically link to your applicationId from app/build.gradle so you don't need to change it here. Please check that you add path prefix /invite because Invites should be independent from other StreamLayer SDK deep links, which are using /main path prefix.
This Activity should add StreamLayerFragment to the host app layout. For more info check Deep Linking Guide.

  1. Initialize the Branch.io session in the Activity of the host app, which has declared an intent-filter:
private val branchReferralInitListener =
        BranchReferralInitListener { linkProperties, error ->
            if (error == null) linkProperties?.let { jsonObject ->
            SLRInviteData.fromJsonObject(jsonObject)?.let { invite ->
                    StreamLayer.getInvite(it.toString())?.let {
                    // check if user authorized or auth isn't required for this invite
                    if (StreamLayer.isUserAuthorized() || !invite.isAuthRequired()){
                        StreamLayer.handleInvite(invite, this)
                    } else{
                        // show your custom screen or user streamlayer general invite dialog
                        StreamLayerInviteFragment.newInstance(invite)
                            .show(supportFragmentManager, StreamLayerInviteFragment::class.java.name)
                    }
                }
                }
            }
      }

   override fun onStart() {
        super.onStart()
        Branch.sessionBuilder(this)
            .withCallback(branchReferralInitListener)
            .withData(this.intent?.data).init()
    }


   override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        if (intent == null) return
        this.intent = intent
        Branch.sessionBuilder(this)
            .withCallback(branchReferralInitListener)
            .reInit()
    }

In your BranchReferralInitListener Invite link should be parsed to SLRInviteData. The SLRInviteData.fromJsonObject() will return SLRInviteData if this was referral link originated from StreamLayer SDK, otherwise the function will return null. SLRInviteData contains info about user which was invited you in app. You can execute your own logic here or pass SLRInviteData to another screen - just call StreamLayer.handleInvite() at the end of it. This function will find the StreamLayerFragment by tag and forward the payload to it based on Deep Linking routes. You also can show general the SDK Invite welcome dialog(or skip this step) and redirect user to your auth flow.

Additional integration help can be found here and here

Custom Invite links

In case if Host app doesn’t use Branch.io for some reason as an Invite link provider Streamlayer SDK provides to the Host app full control over link creating process. Host app can listen Invite link creation requests from the SDK by setup SLRInviteRequestHandler.

StreamLayer.setInviteRequestHandler(object: SLRInviteRequestHandler{
     override suspend fun getInviteLink(data: SLRInviteData): String {
         // map SLRInviteData ot JSONObject
         val jsonObject = SLRInviteData.toJsonObject(data)
         // provide invite link based on JSONObject params
         return "custom_invite_link"
     }
})

Streamlayer SDK provides SLRInviteData as incoming param. Host app should save it and provide an outcoming Invite link for the Streamlayer SDK.

After that Host app should configure own solution for processing incoming Invite link, where at the last step Invite link should be parsed to SLRInviteData:

SLRInviteData.fromJsonObject(jsonObject)?.let { invite ->
     StreamLayer.getInvite(it.toString())?.let {
          // check if user authorized or auth isn't required for this invite
          if (StreamLayer.isUserAuthorized() || !invite.isAuthRequired()){
                StreamLayer.handleInvite(invite, this)
          } else{
          // show your custom screen or user streamlayer general invite dialog
          StreamLayerInviteFragment.newInstance(invite)
                .show(supportFragmentManager, StreamLayerInviteFragment::class.java.name)
          }
    }
}

Disable Invites feature

This settings will hide all views with invites action

class App : Application() {

    override fun onCreate() {
        super.onCreate()
      
        StreamLayer.initializeApp(this, BuildConfig.SL_SDK_KEY)
        // set this feature off
        StreamLayer.setInvitesEnabled(false)
    }
}