SDK

Implementation

With the Notificare's Monetize add-on you can easily sell virtual goods in Google Play. Our library will provide you all the mechanisms to quickly develop an engaging in-app store experience where you can sell digital products.

Please note that to be able to test your products, you will need to upload at least an Alpha build of your app to Google Play. Please read this guide to learn how to test products in a sandbox environment.

To be able to use in-app billing in your application, make sure you add the following dependencies to your app/build.gradle:

dependencies {
    def notificare_version = 'REPLACE_WITH_LATEST_VERSION'

    implementation "re.notifica:notificare-monetize:$notificare_version"
    implementation "re.notifica:notificare-monetize-gms:$notificare_version"         // Enable support for Google Mobile Services.
}

Before we can interact with the Google Play Billing client, we must wait until it has become available. In order to be notified about important events, you should have your activity implement the NotificareMonetize.Listener interface and add itself as a listener.

class MainActivity : AppCompatActivity(), NotificareMonetize.Listener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // more code ...

        Notificare.monetize().addListener(this)
    }

    override fun onDestroy() {
        super.onDestroy()

        // more code ...

        Notificare.monetize().removeListener(this)
    }

    override fun onBillingSetupFinished() {
        // It's safe to start a purchase.
    }

    override fun onBillingSetupFailed(code: Int, message: String) {
        // The underlying Google Play Billing client ran into a problem.
    }
}
public class MainActivity extends AppCompatActivity implements NotificareMonetize.Listener {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // more code ...

        NotificareMonetizeCompat.addListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // more code ...

        NotificareMonetizeCompat.removeListener(this);
    }

    @Override
    public void onBillingSetupFinished() {
        // It's safe to start a purchase.
    }

    @Override
    public void onBillingSetupFailed(int code, @NonNull String message) {
        // The underlying Google Play Billing client ran into a problem.
    }
}

Listing products and purchases

Both products and purchases are exposed as LiveData, so it is easy to hook up your activities or fragments to a list of products, for example:

Notificare.monetize().observableProducts.observe(this) { products ->

}

Notificare.monetize().observablePurchases.observe(this) { purchases ->

}
NotificareMonetizeCompat.getObservableProducts().observe(this, products -> {

});

NotificareMonetizeCompat.getObservablePurchases().observe(this, purchases -> {

});

If you don't want to use LiveData, you can still get the latest cached products and purchases.

// Products
Notificare.monetize().products

// Purchases
Notificare.monetize().purchases
// Products
NotificareMonetizeCompat.getProducts();

// Purchases
NotificareMonetizeCompat.getPurchases();

Starting a purchase

To start a purchase, you must run the startPurchaseFlow method which will handle the entire process with the Google Play.

Notificare.monetize().startPurchaseFlow(activity, product)
NotificareMonetizeCompat.startPurchaseFlow(activity, product);

Your app will be notified about changes in purchases through the NotificareMonetize.Listener.

class MainActivity : AppCompatActivity(), NotificareMonetize.Listener {

    // more code ...

    override fun onPurchaseFinished(purchase: NotificarePurchase) {

    }

    override fun onPurchaseRestored(purchase: NotificarePurchase) {

    }

    override fun onPurchaseCanceled() {

    }

    override fun onPurchaseFailed(code: Int, message: String) {

    }
}
public class MainActivity extends AppCompatActivity implements NotificareMonetize.Listener {

    // more code ...

    @Override
    public void onPurchaseFinished(@NonNull NotificarePurchase purchase) {

    }

    @Override
    public void onPurchaseRestored(@NonNull NotificarePurchase purchase) {

    }

    @Override
    public void onPurchaseCanceled() {

    }

    @Override
    public void onPurchaseFailed(int code, @NonNull String message) {

    }
}