Xamarin.Forms popup "New Version Available"
Asked Answered
T

3

5

I am working on Xamarin.forms Android Project, I am searching away to display a pop up for user:

New Version Available

when user try to open an Application and a new update is available on play-store .

Thimble answered 18/2, 2017 at 13:25 Comment(6)
Have you checked hockey app?Disproportionate
i read about it.. maybe i will use itThimble
Why do you want to do this in the first place? Doesn't the store automatically update to newer versions if you publish your app?Genera
sure, but some user make update automatically =false, on play store..Thimble
@MikeDarwish After re-thinking about it, hockeyapp won't notify users for update coming from app stores, so I guess Bill Reiss answer is what you're looking for mainly. If you want to show a pop up that your application has been updated you can check on your web service and show it to the user with a way to redirect him to the application in the store.Disproportionate
@Paul actually some of them provide this option but I did not try it yet .. thxThimble
N
8

I think the easiest thing would be to have a web service on your own server that returns the current version number, unfortunately you would need to update this version number any time you update the app in the store.

Nerveless answered 18/2, 2017 at 14:15 Comment(0)
Q
3

Create a text file with the latest version number in a GitHub Gist account.

Get the raw URL

string url = "https://gist.githubusercontent.com/YOUR_ACCOUNT_NAME/0df1fa45aa11753de0a85893448b22de/raw/UpdateInfo.txt";

private static async Task<string> GetLatestVersion(string URL)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(URL));
        request.ContentType = "application/json"; //i am using a json file 
        request.Method = "GET";
        request.Timeout = 20000;
        // Send the request to the server and wait for the response:
        try
        {
            using (WebResponse response = await request.GetResponseAsync())
            {
                // Get a stream representation of the HTTP web response:
                using (Stream stream = response.GetResponseStream())
                {                    
                    StreamReader reader = new StreamReader(stream);
                    return reader.ReadToEnd();
                }
            }
        }
        catch (Exception ex)
        {
            return string.Empty;
        }
    }

which will returns the latest version of your app. and check with the existing app version in activity

var versionName = Application.Context.ApplicationContext.PackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0).VersionName;
        var currentVer = double.Parse(versionName);

but you have to update this version number any time you update the app in the play store.

Qualified answered 20/9, 2017 at 19:27 Comment(2)
Good solution. But I still prefer dynamic solutionThimble
@MikeDarwish: What is dynamic solution ?Luting
P
1
private class GetVersionCode extends AsyncTask<Void, String, String> {
    @Override
    protected String doInBackground(Void... voids) {

        String newVersion = null;
        try {
            newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + SplashActivity.this.getPackageName() + "&hl=it")
                    .timeout(30000)
                    .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                    .referrer("http://www.google.com")
                    .get()
                    .select("div[itemprop=softwareVersion]")
                    .first()
                    .ownText();
             return newVersion;
        } catch (Exception e) {
            return newVersion;
        }
    }

    @Override
    protected void onPostExecute(String onlineVersion) {
        super.onPostExecute(onlineVersion);
        String currentVersion = null;
        try {
            currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (onlineVersion != null && !onlineVersion.isEmpty()) {
            if (Float.valueOf(currentVersion) < Float.valueOf(onlineVersion)) {

                    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(SplashActivity.this);
                    alertDialogBuilder.setTitle("Product Update");
                    alertDialogBuilder.setMessage("A new version is available. Would you like to Upgrade now? (Current: "+currentVersion+" Latest: "+onlineVersion+")");
                    alertDialogBuilder.setPositiveButton("ok",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+SplashActivity.this.getPackageName())));
                                }
                            });

                    alertDialogBuilder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            alertDialogBuilder.setCancelable(true);
                            finish();
                            loginUserCheck();
                        }
                    });

                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();
             }
Provencher answered 27/2, 2017 at 13:53 Comment(2)
could you please add more descriptionThimble
You just take new version of your app from playstore and check it your current version is greater than app version if it is greater just show the pop upProvencher

© 2022 - 2024 — McMap. All rights reserved.