package com.xiangyixie.picshouse.service; import android.app.IntentService; import android.content.Intent; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import com.google.android.gms.gcm.GoogleCloudMessaging; import com.google.android.gms.iid.InstanceID; import com.xiangyixie.picshouse.AppConfig; import com.xiangyixie.picshouse.R; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; /** * An {@link IntentService} subclass for handling asynchronous task requests in * a service on a separate handler thread. * <p/> * TODO: Customize class - update intent actions and extra parameters. */ public class NotificationRegistrationService extends IntentService { private final static String TAG = "NotifyRegService"; public final static String REGISTRATION_COMPLETE = "RegisterComplete"; public NotificationRegistrationService() { super(TAG); } @Override protected void onHandleIntent(Intent intent) { Log.d(TAG, "handle intent " + intent); boolean success = false; 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); String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); // [END get_token] Log.i(TAG, "GCM Registration Token: " + token); // TODO: Implement this method to send any registration to your app's servers. success = 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(REGISTRATION_COMPLETE); registrationComplete.putExtra("STATUS", success); 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 boolean sendRegistrationToServer(String token) { Log.d(TAG, "send token to server " + token); try { URL server = new URL("https://" + AppConfig.NOTIFY_SERVER + "/register"); HttpURLConnection connection = (HttpURLConnection) server.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); OutputStream out = connection.getOutputStream(); String data = "token=" + token; out.write(data.getBytes()); int code = connection.getResponseCode(); boolean success = code < 400 && code >= 200; InputStream in = success ? connection.getInputStream() : connection.getErrorStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line).append('\n'); } Log.d(TAG, "get response (" + code + ") " + sb.toString()); return success; } catch (ProtocolException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; } }