fr.shywim.antoinedaniel.utils.GcmUtils.java Source code

Java tutorial

Introduction

Here is the source code for fr.shywim.antoinedaniel.utils.GcmUtils.java

Source

/*
 * Copyright (c) 2015 Matthieu Harl
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package fr.shywim.antoinedaniel.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;

import com.crashlytics.android.Crashlytics;
import com.google.android.gms.gcm.GoogleCloudMessaging;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import fr.shywim.antoinedaniel.R;

public class GcmUtils {

    public static String getRegistrationId(Context context) {
        final SharedPreferences prefs = context.getSharedPreferences(AppConstants.GOOGLE_PREFS,
                Context.MODE_PRIVATE);
        String registrationId = prefs.getString(AppConstants.GOOGLE_GCM_REGID, "");

        if (registrationId.isEmpty()) {
            return "";
        }
        // Check if app was updated; if so, it must clear the registration ID
        // since the existing regID is not guaranteed to work with the new
        // app version.
        int registeredVersion = prefs.getInt(AppConstants.GOOGLE_GCM_APPVER, Integer.MIN_VALUE);
        int currentVersion = Utils.getAppVersion(context);
        if (registeredVersion != currentVersion) {
            return "";
        }
        return registrationId;
    }

    public static void registerInBackground(final Context context, final GoogleCloudMessaging gcm) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    String regid = gcm.register(AppConstants.SENDER_ID);
                    sendRegistrationIdToBackend(context, regid, Utils.getAppVersion(context));
                    storeRegistrationId(context, regid);
                } catch (IOException ex) {
                    Crashlytics.logException(ex);
                }
                return null;
            }
        }.execute(null, null, null);
    }

    public static void sendRegistrationIdToBackend(Context context, String regid, int version) {
        try {
            HttpPost post = new HttpPost(context.getString(R.string.api_register));
            List<NameValuePair> params = new ArrayList<>(2);
            params.add(new BasicNameValuePair("regid", regid));
            params.add(new BasicNameValuePair("version", Integer.toString(version)));
            post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

            HttpClient client = new DefaultHttpClient();
            HttpResponse response = client.execute(post);

            int responseCode = response.getStatusLine().getStatusCode();
            if (responseCode == 200 || responseCode == 201) {
                context.getSharedPreferences(AppConstants.GOOGLE_PREFS, Context.MODE_PRIVATE).edit()
                        .putBoolean(AppConstants.GOOGLE_GCM_REGISTERED, true).apply();
            }
        } catch (IOException e) {
            Crashlytics.logException(e);
        }
    }

    public static void storeRegistrationId(Context context, String regId) {
        final SharedPreferences prefs = context.getSharedPreferences(AppConstants.GOOGLE_PREFS,
                Context.MODE_PRIVATE);
        int appVersion = Utils.getAppVersion(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(AppConstants.GOOGLE_GCM_REGID, regId);
        editor.putInt(AppConstants.GOOGLE_GCM_APPVER, appVersion);
        editor.apply();
    }
}