bg.phpgcm2.RegistrationIntentService.java Source code

Java tutorial

Introduction

Here is the source code for bg.phpgcm2.RegistrationIntentService.java

Source

/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package bg.phpgcm2;

import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.google.android.gms.gcm.GcmPubSub;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Iterator;
import java.util.prefs.Preferences;

public class RegistrationIntentService extends IntentService {

    private static final String TAG = "RegIntentService";
    private static final String[] TOPICS = { "global" };

    public RegistrationIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        try {
            // [START register_for_gcm]
            // Initially this call goes out to the network to retrieve the token, subsequent calls
            // are local.
            // R.string.gcm_defaultSenderId (the Sender ID) is typically derived from google-services.json.
            // See https://developers.google.com/cloud-messaging/android/start for details on this file.
            // [START get_token]
            InstanceID instanceID = InstanceID.getInstance(this);
            Log.i(TAG, "GCM Registration Token getting now ");
            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

            // [END get_token]
            Log.i(TAG, "GCM Registration Token: " + token);

            sendRegistrationToServer(token);

            // Subscribe to topic channels
            subscribeTopics(token);

            // You should store a boolean that indicates whether the generated token has been
            // sent to your server. If the boolean is false, send the token to your server,
            // otherwise your server should have already received the token.
            sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, true).apply();
            // [END register_for_gcm]
        } catch (Exception e) {
            Log.d(TAG, "Failed to complete token refresh", e);
            // If an exception happens while fetching the new token or updating our registration data
            // on a third-party server, this ensures that we'll attempt the update at a later time.
            sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply();
        }
        // Notify UI that registration has completed, so the progress indicator can be hidden.
        Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE);
        LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);

    }

    /**
     * Persist registration to third-party servers.
     *
     * Modify this method to associate the user's GCM registration token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // TODO: Implement this method to send any registration to your app's servers.
        Log.i(TAG, "registering device (token = " + token + ")");
        String serverUrl = "https://www.smclinic.bg/conn/authorize.php";
        Map<String, String> params = new HashMap<String, String>();
        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        String deviceid = tm.getDeviceId();
        String manufacturer = Build.MANUFACTURER;
        String model = Build.MODEL;
        ;
        if (!(model.startsWith(manufacturer))) {
            model = manufacturer + " " + model;
        }
        String serialno = Build.SERIAL;
        String osversion = Build.VERSION.RELEASE;
        SharedPreferences spsettings = getSharedPreferences("Motolife", MODE_PRIVATE);
        SharedPreferences.Editor speditor = spsettings.edit();

        params.put("fb", deviceid);
        speditor.putString("fb", deviceid);
        params.put("token", token);
        speditor.putString("token", token);
        params.put("fbname", "FB_name");
        speditor.putString("fbname", "FB_name");
        params.put("fbemail", "FB email");
        speditor.putString("fbemail", "FB_email");
        params.put("name", "name");
        speditor.putString("name", "name");
        params.put("email", "email");
        speditor.putString("email", "email");
        params.put("phone", "");
        speditor.putString("phone", "");
        params.put("bdate", "19770425");
        speditor.putString("bdate", "19770425");
        params.put("imei", deviceid);
        speditor.putString("imei", deviceid);
        params.put("serial", serialno);
        speditor.putString("serial", serialno);
        params.put("model", model);
        speditor.putString("model", model);
        params.put("osversion", osversion);
        speditor.putString("osversion", osversion);
        params.put("meters", "25");
        speditor.putString("meters", "25");
        params.put("xcoord", "0.123456");
        speditor.putString("xcoord", Double.toString(0.123456));
        params.put("ycoord", "0.123456");
        speditor.putString("ycoord", Double.toString(0.123456));
        params.put("version", "0.1");
        speditor.putString("version", "0.1");
        params.put("in", "0");
        speditor.putString("in", "0");
        params.put("platform", "0");
        params.put("sh", "0");
        speditor.putString("sh", "0");
        speditor.commit();
        try {
            post(serverUrl, params);
        } catch (IOException e) {
            Log.e(TAG, "Failed to register on attempt " + e);
        }
        Log.i(TAG, "registered");
    }

    private static void post(String endpoint, Map<String, String> params) throws IOException {

        URL url;
        try {
            url = new URL(endpoint);
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("invalid url: " + endpoint);
        }
        StringBuilder bodyBuilder = new StringBuilder();
        Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
        // constructs the POST body using the parameters
        while (iterator.hasNext()) {
            Entry<String, String> param = iterator.next();
            bodyBuilder.append(param.getKey()).append('=').append(param.getValue());
            if (iterator.hasNext()) {
                bodyBuilder.append('&');
            }
        }
        String body = bodyBuilder.toString();
        Log.v(TAG, "Posting '" + body + "' to " + url);
        byte[] bytes = body.getBytes();
        HttpURLConnection conn = null;
        try {
            Log.e("URL", "> " + url);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setFixedLengthStreamingMode(bytes.length);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            // post the request
            OutputStream out = conn.getOutputStream();
            out.write(bytes);
            out.close();
            // handle the response
            int status = conn.getResponseCode();
            if (status != 200) {
                throw new IOException("Post failed with error code " + status);
            }
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
    }

    /**
     * Subscribe to any GCM topics of interest, as defined by the TOPICS constant.
     *
     * @param token GCM token
     * @throws IOException if unable to reach the GCM PubSub service
     */
    private void subscribeTopics(String token) throws IOException {
        GcmPubSub pubSub = GcmPubSub.getInstance(this);
        for (String topic : TOPICS) {
            pubSub.subscribe(token, "/topics/" + topic, null);
        }
    }

}