Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Native service for an in-app review to display on Android, using the new v1.8 Play Core Library binding for Xamarin
[assembly: Xamarin.Forms.Dependency(typeof(NAMESPACE.Droid.InAppReviewService))]
namespace NAMESPACE.Droid
{
public class InAppReviewService : IInAppReview
{
public void LaunchReview()
{
#if DEBUG
// FakeReviewManager does not interact with the Play Store, so no UI is shown
// and no review is performed. Useful for unit tests.
var manager = new FakeReviewManager(MainActivity.GlobalContext);
#else
var manager = ReviewManagerFactory.Create(MainActivity.GlobalContext);
#endif
var request = manager.RequestReviewFlow();
request.AddOnCompleteListener(new OnCompleteListener(manager));
}
}
public class OnCompleteListener : Java.Lang.Object, IOnCompleteListener
{
FakeReviewManager _fakeReviewManager;
IReviewManager _reviewManager;
bool _usesFakeManager;
void IOnCompleteListener.OnComplete(Com.Google.Android.Play.Core.Tasks.Task p0)
{
if (!p0.IsSuccessful)
return;
// Canceling the review raises an exception which needs to be handled
try
{
LaunchReview(p0);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void LaunchReview(Task p0)
{
{
var review = p0.GetResult(Java.Lang.Class.FromType(typeof(ReviewInfo)));
if (_usesFakeManager)
{
var x = _fakeReviewManager.LaunchReviewFlow(MainActivity.GlobalContext, (ReviewInfo)review);
x.AddOnCompleteListener(new OnCompleteListener(_fakeReviewManager));
}
else
{
var x = _reviewManager.LaunchReviewFlow(MainActivity.GlobalContext, (ReviewInfo)review);
x.AddOnCompleteListener(new OnCompleteListener(_reviewManager));
}
}
}
public OnCompleteListener(FakeReviewManager fakeReviewManager)
{
_fakeReviewManager = fakeReviewManager;
_usesFakeManager = true;
}
public OnCompleteListener(IReviewManager reviewManager)
{
_reviewManager = reviewManager;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment