1818import com .devsu .library .pushclient .prefs .PrefsConstants ;
1919import com .devsu .library .pushclient .service .RegistrationIntentService ;
2020import com .devsu .library .pushclient .service .RegistrationResultReceiver ;
21+ import com .devsu .library .pushclient .service .UnregistrationIntentService ;
2122import com .google .android .gms .common .ConnectionResult ;
2223import com .google .android .gms .common .GoogleApiAvailability ;
23- import com .google .android .gms .gcm .GoogleCloudMessaging ;
2424
2525import java .io .IOException ;
2626
@@ -54,11 +54,6 @@ public final class PushClient implements RegistrationResultReceiver.Receiver {
5454 */
5555 private String mGcmId ;
5656
57- /**
58- * The GCM instance.
59- */
60- private GoogleCloudMessaging mGcm ;
61-
6257 /**
6358 * The retrieved registation ID.
6459 */
@@ -136,42 +131,61 @@ public static void initialize(Context context, String gcmId, InitCallback initCa
136131 sInstance .mGcmId = gcmId ;
137132 sInstance .mInitCallback = initCallback ;
138133 sInstance .mPushDelegate = delegate ;
139- sInstance .startIntentServiceIfNeeded ();
134+ sInstance .startRegistrationIntentServiceIfNeeded ();
140135 }
141136
142137 /**
143138 * Launches the GCM Registration IntentService if app is not registered.
144139 */
145- private void startIntentServiceIfNeeded () {
140+ private void startRegistrationIntentServiceIfNeeded () {
146141 if (!hasPlayServices ()) {
147142 doOnCallbackError (new PlayServicesNotFoundException ());
148143 return ;
149144 }
150- mGcm = GoogleCloudMessaging .getInstance (mContext );
151145 mRegistrationId = loadRegistrationId ();
152146 if (!TextUtils .isEmpty (mRegistrationId )) {
153147 doOnCallbackSuccess (false );
154148 return ;
155149 }
156- startIntentService ();
150+ startRegistrationIntentService ();
157151 }
158152
159153 /**
160154 * Launches the GCM Registration IntentService.
161155 */
162- private void startIntentService () {
156+ private void startRegistrationIntentService () {
163157 Intent intent = new Intent (mContext , RegistrationIntentService .class );
164158 intent .putExtra (RegistrationResultReceiver .TAG , mReceiver );
165159 intent .putExtra (PrefsConstants .PREF_GCM_ID , mGcmId );
166160 mContext .startService (intent );
167161 }
168162
169163 /**
170- * Receives the GCM Registration IntentService's result.
164+ * Receives any IntentService's result, and handles Registration and Unregistration events .
171165 * @param resultCode The IntentService's result code.
172166 * @param resultData The IntentService's extras.
173167 */
174168 public void onReceiveResult (int resultCode , Bundle resultData ) {
169+ if (resultData == null ) {
170+ return ;
171+ }
172+ String origin = resultData .getString (PrefsConstants .SERVICE_ORIGIN );
173+ if (origin == null ) {
174+ return ;
175+ }
176+ if (origin .equals (RegistrationIntentService .class .getSimpleName ())) {
177+ onReceiveRegistrationResult (resultCode , resultData );
178+ } else if (origin .equals (UnregistrationIntentService .class .getSimpleName ())) {
179+ onReceiveUnregistrationResult (resultCode , resultData );
180+ }
181+ }
182+
183+ /**
184+ * Receives the GCM Registration IntentService's result.
185+ * @param resultCode The IntentService's result code.
186+ * @param resultData The IntentService's extras.
187+ */
188+ public void onReceiveRegistrationResult (int resultCode , Bundle resultData ) {
175189 if (mReceiver != null && resultCode == Activity .RESULT_OK ) {
176190 mRegistrationId = resultData .getString (PrefsConstants .PREF_REG_ID );
177191 storeRegistrationId (mRegistrationId );
@@ -180,13 +194,35 @@ public void onReceiveResult(int resultCode, Bundle resultData) {
180194 }
181195 if (mReceiver != null && resultCode == Activity .RESULT_CANCELED ) {
182196 IOException e = (IOException ) resultData .getSerializable (PrefsConstants .REGISTRATION_EXCEPTION );
183- if (e == null )
197+ if (e == null ) {
184198 return ;
199+ }
185200 doOnCallbackError (e .getMessage ().equalsIgnoreCase (INVALID_SENDER )
186201 ? new InvalidSenderIdException (mGcmId ) : new PushClientException (e ));
187202 }
188203 }
189204
205+ /**
206+ * Receives the GCM Unregistration IntentService's result.
207+ * @param resultCode The IntentService's result code.
208+ * @param resultData The IntentService's extras.
209+ */
210+ public void onReceiveUnregistrationResult (int resultCode , Bundle resultData ) {
211+ if (mReceiver != null && resultCode == Activity .RESULT_OK ) {
212+ sInstance .mRegistrationId = null ;
213+ sInstance .getPushPreferences ().edit ().clear ().apply ();
214+ Log .d (TAG , "Unregistration successful." );
215+ return ;
216+ }
217+ if (mReceiver != null && resultCode == Activity .RESULT_CANCELED ) {
218+ IOException e = (IOException ) resultData .getSerializable (PrefsConstants .REGISTRATION_EXCEPTION );
219+ if (e == null ) {
220+ return ;
221+ }
222+ Log .e (TAG , e .getMessage ());
223+ }
224+ }
225+
190226 /**
191227 * Executes an initialization error callback.
192228 * @param e The exception for the error callback.
@@ -273,15 +309,21 @@ private String loadRegistrationId() {
273309 * Unregisters this device from GCM.
274310 */
275311 public static void unregister () {
312+ if (sInstance == null )
313+ throw new RuntimeException (TAG + " has not been initialized." );
276314 if (sInstance .mRegistrationId == null ) {
277315 throw new RuntimeException (TAG + " is not registered" );
278316 }
279- try {
280- sInstance .mGcm .unregister ();
281- sInstance .getPushPreferences ().edit ().clear ().apply ();
282- } catch (Exception e ) {
283- throw new RuntimeException (e );
284- }
317+ sInstance .startUnregistrationIntentService ();
318+ }
319+
320+ /**
321+ * Launches the GCM Unregistration IntentService.
322+ */
323+ private void startUnregistrationIntentService () {
324+ Intent intent = new Intent (mContext , UnregistrationIntentService .class );
325+ intent .putExtra (RegistrationResultReceiver .TAG , mReceiver );
326+ mContext .startService (intent );
285327 }
286328
287329 /**
@@ -301,6 +343,14 @@ public static void setDelegate(PushDelegate pushDelegate) {
301343
302344 }
303345
346+ /**
347+ * Gets the GCM ID (or Sender ID)
348+ * @return The GCM ID (or Sender ID)
349+ */
350+ public static String getGcmId () {
351+ return sInstance .mGcmId ;
352+ }
353+
304354 /**
305355 * Gets the delegate that handles the push message display.
306356 * @return The delegate that handles the push message display.
0 commit comments