Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 832f2e0

Browse files
committed
Fixed GCM.unregister() and GCM.MESSAGE_TYPE_MESSAGE deprecations.
1 parent 573c97b commit 832f2e0

File tree

13 files changed

+159
-97
lines changed

13 files changed

+159
-97
lines changed

CHANGELOG.MD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## [1.1.0](https://github.com/devsu/gcm-push-client/releases/tag/1.1.0) (2015-12-22)
4+
5+
+ Fixed GCM.unregister() method deprecation by creating an UnregisterService.
6+
+ Fixed GCM.MESSAGE_TYPE_MESSAGE constant deprecation by creating a class that extends GcmListenerService.
7+
38
## [1.0.0](https://github.com/devsu/gcm-push-client/releases/tag/1.0.0) (2015-12-22)
49

510
First release of the GCM Push Client.

pushclient/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
minSdkVersion 12
99
targetSdkVersion 23
1010
versionCode 1
11-
versionName "1.0"
11+
versionName "1.1.0"
1212
}
1313

1414
buildTypes {
@@ -27,10 +27,10 @@ ext {
2727
libraryName = 'GCM Push Client'
2828
artifact = 'pushclient'
2929

30-
libraryDescription = 'Library that allows you to receive GCM Push Messages on Android.'
30+
libraryDescription = 'Library that allows you to easily receive GCM Push Messages on Android.'
3131

32-
siteUrl = 'https://bitbucket.org/rion18/pushclient'
33-
gitUrl = 'https://[email protected]/rion18/pushclient.git'
32+
siteUrl = 'https://github.com/devsu/gcm-push-client'
33+
gitUrl = 'https://github.com/devsu/gcm-push-client.git'
3434

3535
libraryVersion = android.defaultConfig.versionName
3636

pushclient/src/main/AndroidManifest.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</receiver>
2929

3030
<service
31-
android:name=".service.PushClientIntentService"
31+
android:name=".service.PushListenerService"
3232
android:exported="false" >
3333
<intent-filter>
3434
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
@@ -48,6 +48,11 @@
4848
android:exported="false">
4949
</service>
5050

51+
<service
52+
android:name=".service.UnregistrationIntentService"
53+
android:exported="false">
54+
</service>
55+
5156
</application>
5257

5358
</manifest>

pushclient/src/main/java/com/devsu/library/pushclient/client/PushClient.java

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import com.devsu.library.pushclient.prefs.PrefsConstants;
1919
import com.devsu.library.pushclient.service.RegistrationIntentService;
2020
import com.devsu.library.pushclient.service.RegistrationResultReceiver;
21+
import com.devsu.library.pushclient.service.UnregistrationIntentService;
2122
import com.google.android.gms.common.ConnectionResult;
2223
import com.google.android.gms.common.GoogleApiAvailability;
23-
import com.google.android.gms.gcm.GoogleCloudMessaging;
2424

2525
import 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.

pushclient/src/main/java/com/devsu/library/pushclient/delegate/SimpleNotificationDelegate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void handleNotification(Context context, Bundle extras) {
145145
* Retrieves the contentIntent for the default activity.
146146
* @param context The context.
147147
* @param currentTimeStamp The time of Notification.
148-
* @return
148+
* @return The content intent.
149149
*/
150150
private PendingIntent getContentIntent(Context context, int currentTimeStamp) {
151151
Intent intent = new Intent();

pushclient/src/main/java/com/devsu/library/pushclient/prefs/PrefsConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ public class PrefsConstants {
99
public static final String PREF_REG_ID = "registration_id";
1010
public static final String PREF_GCM_ID = "gcm_id";
1111
public static final String REGISTRATION_EXCEPTION = "registration_exception";
12+
public static final String SERVICE_ORIGIN = "service_origin";
1213
}

pushclient/src/main/java/com/devsu/library/pushclient/receiver/GCMBroadcastReceiver.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

pushclient/src/main/java/com/devsu/library/pushclient/service/PushClientIntentService.java

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.devsu.library.pushclient.service;
2+
3+
import android.os.Bundle;
4+
5+
import com.devsu.library.pushclient.client.PushClient;
6+
import com.google.android.gms.gcm.GcmListenerService;
7+
8+
/**
9+
* The IntentService that processes the Push Message.
10+
*/
11+
public class PushListenerService extends GcmListenerService {
12+
13+
/**
14+
* Retrieves the GCM Push Message.
15+
*/
16+
@Override
17+
public void onMessageReceived(String from, Bundle data) {
18+
if (PushClient.getGcmId().contentEquals(from))
19+
PushClient.getDelegate().handleNotification(this, data);
20+
}
21+
}

pushclient/src/main/java/com/devsu/library/pushclient/service/RegistrationIntentService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ public RegistrationIntentService() {
3131

3232
/**
3333
* Generates a new registration ID using InstanceID.
34-
* @param intent The intent with the generated registration ID OR the Exception that ocurred.
34+
* @param intent The intent with the Receiver tag, and GCM ID.
3535
*/
3636
@Override
3737
public void onHandleIntent(Intent intent) {
3838
ResultReceiver receiver = intent.getParcelableExtra(RegistrationResultReceiver.TAG);
3939
Bundle bundle = new Bundle();
40+
bundle.putString(PrefsConstants.SERVICE_ORIGIN, TAG);
4041
try {
4142
String gcmId = intent.getStringExtra(PrefsConstants.PREF_GCM_ID);
4243
InstanceID instanceID = InstanceID.getInstance(this);

0 commit comments

Comments
 (0)