Java tutorial
/** * 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 com.macmoim.pang.gcm; import android.app.IntentService; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import com.android.volley.NetworkResponse; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.VolleyLog; import com.google.android.gms.gcm.GcmPubSub; import com.google.android.gms.gcm.GoogleCloudMessaging; import com.google.android.gms.iid.InstanceID; import com.macmoim.pang.R; import com.macmoim.pang.app.AppController; import com.macmoim.pang.app.CustomRequest; import com.macmoim.pang.data.AppPreferences; import com.macmoim.pang.data.LoginPreferences; import com.macmoim.pang.util.Util; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class RegistrationIntentService extends IntentService { private static final String TAG = "RegIntentService"; private static final String[] TOPICS = { "global" }; private static final String URL_GCM_DATA = Util.SERVER_ROOT + "/gcmData"; private static final String REQ_TAG = "upload-gcm-data"; public RegistrationIntentService() { super(TAG); } @Override protected void onHandleIntent(Intent intent) { try { // [START register_for_gcm] // Initially this call goes out to the network to retrieve the token, subsequent calls // are local. // [START get_token] InstanceID instanceID = InstanceID.getInstance(this); String token = instanceID.getToken(getString(R.string.gcm_sender_id), 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. 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. AppPreferences.GetInstance().putBoolean(getApplicationContext(), AppPreferences.SENT_TOKEN_TO_SERVER, true); // [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. AppPreferences.GetInstance().putBoolean(getApplicationContext(), AppPreferences.SENT_TOKEN_TO_SERVER, false); } // Notify UI that registration has completed, so the progress indicator can be hidden. Intent registrationComplete = new Intent(AppPreferences.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) { // Add custom implementation, as needed. String user_id = LoginPreferences.GetInstance().getString(getApplicationContext(), LoginPreferences.PROFILE_ID); Log.d(TAG, "sendRegistrationToServer userid " + user_id + " token " + token); String url = URL_GCM_DATA; Map<String, String> obj_body = new HashMap<String, String>(); obj_body.put("user_id", user_id); obj_body.put("instant_id", token); CustomRequest jsonReq = new CustomRequest(Request.Method.POST, url, obj_body, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { VolleyLog.d(TAG, "Response: " + response.toString()); try { if (response != null) { Log.d(TAG, "gcmData toserver ret-val " + response.getString("ret_val")); } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.d(TAG, "Error: " + error.getMessage()); NetworkResponse response = error.networkResponse; if (response != null && response.data != null) { Log.d(TAG, "FeedListView onErrorResponse statusCode = " + response.statusCode + ", data=" + new String(response.data)); } } }); // } // Adding request to volley request queue AppController.getInstance().addToRequestQueue(jsonReq, REQ_TAG); } /** * 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 */ // [START subscribe_topics] private void subscribeTopics(String token) throws IOException { for (String topic : TOPICS) { GcmPubSub pubSub = GcmPubSub.getInstance(this); pubSub.subscribe(token, "/topics/" + topic, null); } } // [END subscribe_topics] }