Java tutorial
/** * Copyright [2015] [Codelyst (Prashant goyal)] * <p/> * 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 * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * 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.se. */ /* * Use this meta data in your manifests file for get * <meta-data android:name="gcm_sender_id" android:value="@string/gcm_sender_id" /> * */ package com.codelyst.ccm; 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.codelyst.ccm.utils.Utils; 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; public class RegistrationIntentService extends IntentService { private static final String TAG = "RegIntentService"; private static final String[] TOPICS = { "global" }; public static final String GCM_SENDER_META_DATA_KEY = "gcm_sender_id"; public static final String SENT_TOKEN_TO_SERVER = "sendTokenToServer"; public static final String REGISTRATION_COMPLETE = "REGISTRATION_COMPLETE"; public static final String GCM_TOKEN = "GCM_TOKEN"; public RegistrationIntentService() { super(TAG); } @Override protected void onHandleIntent(Intent intent) { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); String token = null; try { // In the (unlikely) event that multiple refresh operations occur simultaneously, // ensure that they are processed sequentially. synchronized (TAG) { // [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 gcm_sender_id = Utils.getMetaDataFromManifests(getApplicationContext(), GCM_SENDER_META_DATA_KEY); Log.d(TAG, gcm_sender_id); token = instanceID.getToken(gcm_sender_id, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); // [END get_token] Log.i(TAG, "GCM Registration Token: " + token); sharedPreferences.edit().putBoolean(GCM_TOKEN, true).apply(); // Subscribe to topic channels subscribeTopics(token); // [END register_for_gcm] } } catch (Exception e) { Log.d(TAG, "Failed to complete token refresh", e); } // Notify UI that registration has completed, so the progress indicator can be hidden. Intent registrationComplete = new Intent(REGISTRATION_COMPLETE); if (token != null) { registrationComplete.putExtra(GCM_TOKEN, token); sharedPreferences.edit().putString(GCM_TOKEN, token).apply(); } else { registrationComplete.putExtra(GCM_TOKEN, ""); sharedPreferences.edit().putString(GCM_TOKEN, "").apply(); } LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete); } /** * Persist registration to third-party servers. * <p/> * 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) { } /** * 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] }