SDK

Implement In-App Billing

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 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 you sure you add the following permission to your AndroidManifest.xml:

1
<uses-permission android:name="com.android.vending.BILLING" />

You will enable this feature in your app by invoking the following method from your Intent Receiver:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class MyIntentReceiver extends DefaultIntentReceiver {

	@Override
	public void onReady() {

		//more code ...

		Notificare.shared().enableBilling();

		//more code ...
	}
	//more code ...
}

After this point, our library will automatically load any products you've previously created.

To actually display them to the user in an activity or fragment you will need to implement the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class ProductsActivity extends BaseActivity implements Notificare.OnBillingReadyListener, BillingManager.OnRefreshFinishedListener, BillingManager.OnPurchaseFinishedListener {

	//more code ...

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	  if (Notificare.shared().getBillingManager().handleActivityResult(requestCode, resultCode, data)) {
		// Billingmanager handled the result
		inProgress = true; // wait for purchase to finish before doing other calls
	  }
	}
	@Override
	protected void onResume() {
	  super.onResume();
	  Notificare.shared().addBillingReadyListener(this);
	}
	@Override
	protected void onPause() {
	  super.onPause();
	  Notificare.shared().removeBillingReadyListener(this);
	}
	@Override
	public void onBillingReady() {
	  if (!inProgress) {
		productListAdapter.clear();
		Notificare.shared().getBillingManager().refresh(this);
	  }
	}
	@Override
	public void onPurchaseFinished(BillingResult billingResult, Purchase purchase) {
	  inProgress = false;
	  productListAdapter.clear();
	  Notificare.shared().getBillingManager().refresh(this);
	}
	@Override
	public void onRefreshFinished() {
	  productListAdapter.addAll(Notificare.shared().getBillingManager().getProducts());
	}
	@Override
	public void onRefreshFailed(NotificareError notificareError) {
	  Toast.makeText(this, "billing refresh failed: " + notificareError.getMessage(), Toast.LENGTH_LONG).show();
	}

	...more code
}

In the code above, we are assuming that you will be consuming the products loaded in a ListView but you can basically present your products in any other way you may see fit.

To actually handle the purchase flow of one of your products, make sure you implement some like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...more code

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_products);

	...more code

	listView.setOnItemClickListener(new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			NotificareProduct item = productListAdapter.getItem(position);
			Notificare.shared().getBillingManager().launchPurchaseFlow(ProductsActivity.this, item, ProductsActivity.this);
		}
	});

	...more code
}

...more code

This will be all you need to allow your user to buy any of your products.