コンテンツにスキップ

限定クーポン配布の方法

このページでは管理画面で限定クーポンを作成した際に発行されるカスタム URL による配布方法を説明します。
※限定クーポンについての詳細はこちらをご確認ください

カスタム URL の設定

限定クーポンをカスタム URL 経由で受け取る場合、カスタム URL スキームを設定する必要があります。
AndroidManifest.xml に intent-filter でカスタム URL スキームを設定し、遷移する画面を指定してください。

下記のサンプルコードでは coupon.sample というカスタム URL スキームを受け取った場合に LinkActivity に遷移し、クーポン一覧画面を表示するという処理を行います。

...

<activity
    android:name=".LinkActivity"
    android:exported="true">
    <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:scheme="coupon.sample" />
    </intent-filter>
</activity>

...

配布 API の実行

カスタム URL 経由で画面を開いた際に配布 API を実行し、クーポン一覧画面へ遷移するサンプルとなります。

class LinkActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // クーポンの配布
        distributeCoupon(intent)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)

        // クーポンの配布
        distributeCoupon(intent)
    }

    private fun distributeCoupon(intent: Intent?) {

        // カスタム URL 経由で無い場合は処理を抜ける
        if (Intent.ACTION_VIEW != intent?.action) {
            return
        }

        // URI 取得
        val uri: Uri? = intent.data
        val authority = uri?.authority
        val query = uri?.getQueryParameter("uuid")

        // クーポン配布用カスタム URL の判断
        if (authority != "jp.popinfo.coupon") {
            return
        }

        // クーポン配布 API を実行
        FanshipCouponClient.distributeCoupon(
            applicationContext,
            Popinfo.getPopinfoId(applicationContext),
            query!!,
            object : FanshipCouponAsyncCallback<FanshipCouponDistributeResponse> {
                override fun onComplete(response: FanshipCouponDistributeResponse) {

                    // クーポン一覧画面へ遷移する
                    if (response.coupon != null) {
                        val _intent = Intent(
                            applicationContext,
                            FanshipCouponList::class.java
                        )
                        _intent.putExtra(DISTRIBUTE_ID, response.coupon.uuid)
                        startActivity(_intent)
                    }

                    finish()
                }

                override fun onError(e: FanshipCouponErrorException) {
                    finish()
                }
            })

    }
}
public class LinkActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // クーポンの配布
        distributeCoupon(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        // クーポンの配布
        distributeCoupon(intent);
    }

    /**
    * 限定クーポンを配布し、一覧画面へ遷移する
    */
    private void distributeCoupon(Intent intent) {

        // カスタム URL 経由で無い場合は処理を抜ける
        if (intent == null || !Intent.ACTION_VIEW.equals(intent.getAction())) {
            return;
        }

        // URI 取得
        Uri uri = intent.getData();
        String authority = uri.getAuthority();
        String query = uri.getQueryParameter("uuid");

        // クーポン配布用カスタム URL の判断
        if (!authority.equals("jp.popinfo.coupon")) {
            return;
        }

        // クーポン配布 API を実行
        FanshipCouponClient.distributeCoupon(
                getApplicationContext(),
                Popinfo.getPopinfoId(getApplicationContext()),
                query,
                new FanshipCouponAsyncCallback<FanshipCouponDistributeResponse>() {
                    @Override
                    public void onComplete(FanshipCouponDistributeResponse response) {

                        // クーポン一覧画面へ遷移する
                        if (response.coupon != null) {
                            Intent _intent = new Intent(getApplicationContext(), FanshipCouponList.class);
                            _intent.putExtra(FanshipCouponList.DISTRIBUTE_ID, response.coupon.uuid);
                            startActivity(_intent);
                        }

                        finish();
                    }
                    @Override
                    public void onError(FanshipCouponErrorException e) {
                        finish();
                    }
                });
    }
}

Memo

クーポン一覧画面へ遷移する際、クーポンの ID をパラメータとしてセットすることで一覧画面を表示した際に指定したクーポンの箇所へスクロールします。

※ fragment での遷移をする際は Bundle に FanshipCouponListFragment.COUPON_ID というキーにクーポン ID を含めることで、同様の挙動を行います。