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.anypresence.android.notifications.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.anypresence.sdk.callbacks.APCallback; import com.anypresence.sdk.push.PushNotification; public class RegistrationIntentService extends IntentService { private static final String TAG = "RegIntentService"; public RegistrationIntentService() { super(TAG); } @Override protected void onHandleIntent(Intent intent) { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); try { // Subscribe to Google String token = GCMManager.get().registerDevice(this); Log.i(TAG, "GCM Registration Token saved"); // register to AP API sendRegistrationToServer(token); // [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(); } } private void finishRegistration() { // Notify UI that registration has completed, so the progress indicator can be hidden. Intent registrationComplete = new Intent(QuickstartPreferences.GCM_REGISTRATION); registrationComplete.putExtra(GCMManager.EXTRA_REGISTRATION, true); 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(final String token) { Log.d(TAG, "register"); PushNotification.subscribeToChannel(GCMManager.CHANNEL_NAME, GCMManager.APP_NAME, token, PushNotification.Provider.ANDROID, GCMManager.NAME, new APCallback<String>() { @Override public void finished(String result, Throwable err) { if (err == null) { // Successfully subscribed Log.d(TAG, "subscribe to channel success: " + result); GCMManager.get().putSentTokenToServer(RegistrationIntentService.this, true); finishRegistration(); } else { // Not able to subscribe Log.d(TAG, "Not able to subscribe", err); finishRegistration(); } } }); } }