Xamarin Forms In app billing for Android - android

I'm making an app where I have to have in app purchases (buying keys that I can further use in the app).
I have looked at this component http://components.xamarin.com/view/xamarin.inappbilling, but I have no idea how I can implement this in xamarin forms. Is there anyone out there willing to help me with this problem? Is there any open source projects with in app purchase that I can look at?

I know its late, but this might help someone:
The way to do achieve this is to create a service and then surface it to a standard interface (as per your requirement) that will be consumed within the forms project.
You can even use MessagingCenter to communicate between Android and Xamarin.Forms project.
FormsPrject:
MessagingCenter.Send<MainPage, string>(this, "BuyProduct", "buyButtonPressed");
AndroidPoject
MessagingCenter.Subscribe<MainPage, string>(this, "BuyProduct", (sender, arg) =>
{
//logic to buy product
}
Hope that helps!!

The question is very vague so I will offer a general answer and some notes that I found important. I am using Visual Studio 2015 with Xamarin Forms 2.3.0.107.
I would use abstraction for this instead of sending messages directly between the projects.
The basic idea is, you will create a public interface in your Xamarin Forms project. Since your Andriod project has a reference to the Xamarin Forms project, it can use this public interface. Then you will implement this interface in your Android project will all of the billing logic. In the Xamarin Forms project. Using the Dependency service we can get the existing instance of the implementation into the Xamarin Forms project. Then, you can code against the interface. This is especially useful if you ever want to do an iPhone or other implementation, because you would never need to make changes to the Xamarin Forms code; you can just plug in new implementations.
It might be a bit out of scope but, be sure to meet all of the Google requirements as far as setting up your developer account and your merchant account and your API account. It is all very confusing and messy.
The Xamarin component in nuget is currently version 1.5. However, the component has a newer published version. You want to use the newer (2.0 or higher) version.
Use the Android SDK manager to install the Google Play Billing Library.
In your Android project, add a reference to Xamarin.InAppBilling and add a Xamarin.InAppBilling component.
The Google object has to live in an Android activity, because you depend on overriding an activity method to complete purchases (I used the MainActivity here and made the google object a static for easy access)
Testing this with Google Play is a hassle. The documentation is confusing because of differences between versions. You cannot use actual product id's until you publish your app. They provide test id's that can be used during testing but they only offer some functionality.
I have made these code examples as minimal as possible to illustrate the concept. You will obviously want to do much more.
Xamarin Forms project
Create an interface:
public interface IInAppBilling
{
void Pay(string productId);
}
Any time you want to use the billing service, you use IInAppBilling billingService = DependencyService.Get<IInAppBilling>(); to get a reference to the device-specific (Android) implementation.
//call this from a button click or whatever
void BuySomething(string somethingId)
{
//Get any IInAppBilling object that is registered with the DependencyService.
IInAppBilling billingService = DependencyService.Get<IInAppBilling>();
billingService.Pay(somethingId);
}
Android Project
Override an activity's OnCreate method and create an InAppBillingServiceConnection:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
public static InAppBillingServiceConnection google;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
google = new InAppBillingServiceConnection(this, "MII...ApplicationKey");
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
Create a class that implements the Xamarin Forms interface we created earlier. It is important not to ignore the assembly: Dependencyannotation at the top. This is what makes the class available to the Dependency service in the Xamarin Forms object:
[assembly: Dependency(typeof(com.myapp.InAppBilling))]
namespace com.myapp
{
class InAppBilling :IInAppBilling
{
public void Pay(string productId)
{
MainActivity.google.BillingHandler.BuyProduct(productId, ItemType.Product, "MyUniquePayload");
}
}
}
Override the activity's OnActivityResult method to finalize purchases:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
// Ask the open service connection's billing handler to process this request
try
{
google.BillingHandler.HandleActivityResult(requestCode, resultCode, data);
}
catch (Exception ex)
{
//log it or something?
}
}

Related

AppsFlyer SDK Integration Test failed on app, but non-organic test data is reflected on the Dashboard

Trying to integrate the AppsFlyer SDK for Android, I've followed all the steps indicated in the AppsFlyer guide. Initially, the dependency that was stated is:
implementation 'com.appsflyer:af-android-sdk:4+#aar'
But for some reason, it wasn't pointing to the latest version on it's own, making the init() version not visible. And so I instead went ahead and adjusted it to version 4.8.15 (currently the latest version), which worked as expected in the guide.
After adding the required permissions (with the optional permission) and the BroadcastReceiver (I don't have any other receivers for the referrer so I only used the SingleInstallBroadcastReceiver), I implemented the initialization on the app's Application class like so:
public class MyApp extends Application {
private static final String AF_DEV_KEY = "DEV_KEY_FROM_CONSOLE_HERE";
#Override
public void onCreate() {
super.onCreate()
initAppsFlyer();
}
public void initAppsFlyer() {
final AppsFlyerConversionListener listener = new AppsFlyerConversionListener() {
#Override
public void onInstallConversionDataLoaded(Map<String, String> conversionData) {
}
#Override
public void onInstallConversionFailure(String errorMessage) {
}
#Override
public void onAppOpenAttribution(Map<String, String> attributionData) {
}
#Override
public void onAttributionFailure(String errorMessage) {
}
};
AppsFlyerLib.getInstance().init(AF_DEV_KEY, listener, this);
AppsFlyerLib.getInstance().startTracking(this);
}
}
The manifest is pointing to the MyApp application class for sure. So that's it. The initial setup is complete. Time for the SDK Integration test as per the guide.
So I installed the app directly from Android Studio to my device, downloaded SDK Integration test app, whitelisted the device, and selected the name of the app as mentioned in the test guide. Every step followed accordingly.
Clicked on SDK Integration Testing...
Checking configuration...
Testing your app..
*app opens*
*closes*
*opens again*
Testing your app..
Checking configuration...
..
X Oops..
This app can not be monitored. Please make sure that:
You are using a supported AppsFlyer SDK version.
AppsFyler's BroadcastReceiver is configured.
startTracking() is placed in the MainActivity
It's the latest version. So it should work.
Copy pasted the receiver. Tried removing it and then re-run the test, it showed the missing receiver error. Pasted it back in.
I'm calling startTracking() in the actual application class. But what the heck. I gave it a try, called it in the MainActivity.onCreate() same result.
I have no idea what is causing the failure in the test. Not much to go on from as well since there's no details on how to debug it further. However, I checked the Dashboard, and there it is. Non-Organic install data counted.
Now I'm confused. Is there an issue with the integration or with the SDK integration test app?

Custom notification with Google Cast SDK v3

Ever since the new Google Cast SDK v3 has been out, I've been thinking about migrating to it, as it has a few features I need and I'm always interested in keeping everything as up-to-date as possible. My previous setup used Google Cast SDK v2 + Cast Companion Library. However I've been unable to find a way to provide a completely custom notification in the new Google Cast SDK v3 which prevents me from using it.
With Google Cast SDK v2 + Cast Companion Library I was able to simply subclass the VideoCastNotificationService and override its build method to make a custom notification:
public class MyCastNotificationService extends VideoCastNotificationService {
#Override
protected void build(MediaInfo info, Bitmap bitmap, boolean isPlaying) {
// ... build the custom notification
mNotification = new NotificationCompat.Builder(this)/*...*/.build();
}
}
Then I could register the MyCastNotificationService in my CastConfiguration like this:
CastConfiguration castConfig = new CastConfiguration.Builder(APPLICATION_ID)
/*...*/
.setCustomNotificationService(MyCastNotificationService.class)
.build();
However, with Google Cast SDK v3 I've been unable to find a replacement that works. Here are the results of the bit of research I've done on this topic:
The MediaNotificationService seems to be the component replacing the VideoCastNotificationService, however it does not seem to have a build method or anything similiar.
Both the NotificationOptions.Builder and CastMediaOptions.Builder classes don't seem to have any methods for registering a custom notification service.
The only documented way of customizing the notification seems to be custom action buttons as shown in the Add Media Controls to Notification and Lock Screen section, however I need to create a notification using the RemoteViews API, so this is not sufficient for me.
Has anyone been able to provide a custom notification using the new Google Cast SDK v3? As far as I can tell, this seems unsupported, but I'd love to be proven wrong here. Thanks in advance!

Chartboost Interstitial won't show Ads on Unity

Lately, I have been trying to add static interstitial ads into my Unity game. For some reason, I could not get the system to show anything, or even react to me. After trying to work with the base Chartboost plugin, I tried to match a tutorial that I was following and purchased Prime31's Chartboost plugin and have been using that. However, neither the base plugin, nor Prime31's plugin, seem to be allowing me to show any ads. The code is pretty much done inside a single object, and it seems simple enough.
public class Advertisement : MonoBehaviour {
public string chartboostAppID = "5461129ec909a61e38b1505b";
public string chartboostAppSignature = "672b3b34e3e358e7a003789ddc36bd2bc49ea3b5";
// Use this for initialization
void Start () {
DontDestroyOnLoad(this.gameObject);
ChartboostAndroid.init (chartboostAppID, chartboostAppSignature, true);
ChartboostAndroid.cacheInterstitial(null);
}
void OnLevelWasLoaded(int level) {
ChartboostAndroid.cacheInterstitial(null);
if(Application.loadedLevelName == "Network Lobby") {
showAds();
}
}
public static void showAds() {
Debug.Log("Showing ad");
ChartboostAndroid.showInterstitial(null);
}
}
As you can see, it's pretty straightforward. This object is created at the game's splash screen, which appears only once, and it's never destroyed until the program ends. The goal is, whenever I enter the lobby scene, I want to see an ad before going to the lobby's menus. As it is, I do see the log printing "Showing ad", so I know the function is being called. However, nothing appears. Do I need to disable the GUI system first? Is there a step I'm missing?
I have already performed the following steps:
I have created and registered the app with chartboost, as well as double and triple checked the AppID and App Signature.
I have created a publishing campaign and registered it to the app.
I double-checked the orientation and confirmed that it's correct.
I registered this specific device as a test device.
The tutorial showed a call to ChartBoostAndroid.OnStart(), but there was no function like that for me to call. Perhaps that is from an older version?
I emailed Chartboost support and have not heard from them yet. I do not have that much time on this project, so if anyone can offer help, I'd appreciate it.

LVL licensing in a Live Wallpaper?

Has anybody had success integrating the Licensing Verification Library (LVL) with a Live Wallpaper? If it were just running an Activity, it'd be crystal clear to just extend my Activity from the Licensing Activity, which in turn extends Activity. But Live Wallpapers are a Service, and I'm not sure how the two are intended to interact.
I'm using code derived from this: http://www.droidforums.net/forum/android-app-developers/69899-market-license-easy-implementation-protect-your-apps.html which seems to be the code that nearly everything I can find on the web refers to.
I notice that wallpaper settings are an activity, and I have those working properly, but for some reason I can't grok the Licensing stuff...
It's actually really quite simple, you don't need to use any Activity class to implement licensing into a WallpaperService.
Make sure you've followed the directions carefully at http://developer.android.com/guide/publishing/licensing.html
Here's how I did it:
Your extended Engine class should include something similar to the following... (code not essential to your question has been removed)
class startYourEngines extends Engine {
public startYourEngines() {
super();
licenseStatus(); //custom license check method (for modularity)
//the rest of your engine would go here
}
public void onDestroy() {
super.onDestroy();
licenseChecker.onDestroy(); //we call this to close IPC connections
}
//prep work
private static final String BASE64_PUBLIC_KEY = //OMITTED//;
private LicenseCheckerCallback licenseCallback;
private LicenseChecker licenseChecker;
private byte[] salt = "rAnd0mStr!ng".getBytes();
private AESObfuscator aes;
private String deviceId;
//our custom license check method
private void licenseStatus() {
deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
aes = new AESObfuscator(salt, getPackageName(), deviceId);
licenseCallback = new licenseVerification();
licenseChecker = new LicenseChecker(context, new ServerManagedPolicy(context, aes), BASE64_PUBLIC_KEY);
licenseChecker.checkAccess(licenseCallback);
}
//our callback method
private class licenseVerification implements LicenseCheckerCallback {
#Override
public void allow() {
//allow full app use
}
#Override
public void dontAllow() {
//prevent or limit app use
}
#Override
public void applicationError(ApplicationErrorCode errorCode) {
//error handling here
}
}
}
Licensing on the Android platform was created with versatility in mind. Just be sure to read through the documentation, and you shouldn't have any issues.
I have only written applications that start activities, but looking at my source code, it seems that the only reason that you would have to have an Activity do the license check is to show dialogs.
In all of the examples available on line, the LicenseCheckerCallback implementation always shows a dialog in the allow() and dontAllow() methods. Why not just show a toast in dontAllow() and exit your wallpaper service (call stopSelf(YourService.this))?
Let me know if you want more information, because I dont think you are limited to only using an activity for license checking. As an aside, make sure that you dont keep whole strings, etc in your app or in the preferences. Anyone with root access can access your preferences and if your app is decompiled, your strings are visible...
I think I've actually got it working now. I'm extending LicenseCheckActivity to my own Activity class that I'm calling in the manifest file with the usual MAIN action and LAUNCH category. I instantiate my class, do the license check, and then either allow the wallpaper to function or not based on that result (though the best way to do that is still something I need to sort out).
It almost seems too easy that I think I must be missing something. I'd appreciate anybody with experience with selling a licensed live wallpaper on the Android Market to share whatever wisdom they care to.

android: Is it possible to sell add-ons for apps in the Market?

I would like to distribute my app for free, and then sell extra features that can be added-on later. Is it possible to do this?
If you're talking about in-app payment, you should take a look at PayPal, which offers In-App-Payment for Android:
http://www.x.com
https://www.x.com/community/ppx/sdks
If you want to distribute your app via Android Market, you would need to offer each add-on as an invididual app. Probably not a convenient way.
Yes you can do it. There are addons all over the market for the better keyboard app.
You have different ways you can do this, here are two ways I already tried.
1) You could implement all the features in the app and a key app that unlocks this features.
Here is a simple and insecure implementation, the key app and the main app need to be signed with the same key:
public boolean isPackageAvailable(String packageName) {
int sigMatch = getPackageManager().checkSignatures(getPackageName(), packageName);
return sigMatch == PackageManager.SIGNATURE_MATCH;
}
Check MarketEnabler on market-enabler.googlecode.com to see how I used it (not not a secure way but a starting point).
2) You include the plugin app as intent into a subview so it looks like just one app splitted to multiple apk's.
Check http://speakerproximity.googlecode.com where I used it to include the settings view inside the mainview:
....
public class SpeakerProximity extends ActivityGroup {
....
mainLayout.addView(getViewFromIntent("preferences", new Intent(
this, PreferenceScreen.class)));
....
public View getViewFromIntent(String tag, Intent intent) {
/** start an activity inside an ActivityGroup and get the window handler **/
final Window w = getLocalActivityManager().startActivity(tag, intent);
/** extract the view out of the window handler **/
final View wd = w != null ? w.getDecorView() : null;
return wd;
}
...
Note that I extend ActivityGroup and not Activity ;-)

Categories

Resources