Skip to content

Commit 3c2208e

Browse files
authored
feat(push): Checks if client compiles leanplum-gcm and leanplum-fcm at… (#150)
* feat(push): Checks if client compiles leanplum-gcm and leanplum-fcm at the same time.
1 parent 882e070 commit 3c2208e

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

AndroidSDKPush/src/main/java/com/leanplum/LeanplumPushService.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ public static void setGcmSenderIds(String... senderIds) {
154154
* with each module separately.
155155
*
156156
* For example:
157-
* implementation 'com.leanplum:leanplum-core:+'
158157
* implementation 'com.leanplum:leanplum-fcm:+'
159158
* implementation 'com.leanplum:leanplum-location:+'
160159
*/
@@ -733,21 +732,36 @@ public static void setGcmRegistrationId(String token) {
733732
* Call this when Leanplum starts. This method will call by reflection from AndroidSDKCore.
734733
*/
735734
static void onStart() {
736-
boolean callFcmOnStart = isFirebaseEnabled();
737-
if (!callFcmOnStart) {
735+
Class leanplumGcmPushServiceClass = null;
736+
Class leanplumFcmPushServiceClass = null;
737+
738+
try {
739+
leanplumGcmPushServiceClass = Class.forName(LEANPLUM_PUSH_SERVICE_GCM);
740+
} catch (Throwable ignored) {
741+
}
742+
743+
try {
744+
leanplumFcmPushServiceClass = Class.forName(LEANPLUM_PUSH_SERVICE_FCM);
745+
} catch (Throwable ignored) {
746+
}
747+
748+
if (leanplumGcmPushServiceClass != null && leanplumFcmPushServiceClass != null) {
749+
Log.e("Leanplum does not support leanplum-gcm and leanplum-fcm library at the " +
750+
"same time. To support Leanplum GCM and Location services modify your build.gradle by " +
751+
"including only implementation 'com.leanplum:leanplum:+' " +
752+
"To support only GCM services, use implementation 'com.leanplum:leanplum-gcm:+' " +
753+
"For FCM services include implementation 'com.leanplum:leanplum-fcm:+'" +
754+
" If you wish to use Leanplum FCM and Location services you also need to include " +
755+
"implementation 'com.leanplum:leanplum-location:+'.");
756+
757+
} else if (leanplumGcmPushServiceClass != null) {
738758
try {
739-
Class.forName(LEANPLUM_PUSH_SERVICE_GCM).getDeclaredMethod("onStart")
740-
.invoke(null);
759+
leanplumGcmPushServiceClass.getDeclaredMethod("onStart").invoke(null);
741760
} catch (Throwable ignored) {
742-
callFcmOnStart = true;
743761
}
744-
}
745-
746-
// Try starting FCM if GCM module is not included.
747-
if (callFcmOnStart) {
762+
} else if (leanplumFcmPushServiceClass != null) {
748763
try {
749-
Class.forName(LEANPLUM_PUSH_SERVICE_FCM).getDeclaredMethod("onStart")
750-
.invoke(null);
764+
leanplumFcmPushServiceClass.getDeclaredMethod("onStart").invoke(null);
751765
} catch (Throwable ignored) {
752766
}
753767
}

AndroidSDKTests/src/test/java/com/leanplum/LeanplumPushServiceTest.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,21 +212,13 @@ public void testOnStart() throws Exception {
212212
mockStatic(LeanplumPushServiceGcm.class);
213213
mockStatic(LeanplumPushServiceFcm.class);
214214

215-
// Don't call GCM onStart when FCM is enabled.
216-
when(LeanplumPushService.isFirebaseEnabled()).thenReturn(true);
215+
// Don't call GCM onStart or FCM onStart if both FCM and GCM enabled.
217216
onStartMethod.invoke(pushService);
218217
assertNotNull(onStartMethod);
219218
verifyStatic(times(0));
220219
LeanplumPushServiceGcm.class.getDeclaredMethod("onStart");
221-
verifyStatic(times(1));
220+
verifyStatic(times(0));
222221
LeanplumPushServiceFcm.class.getDeclaredMethod("onStart");
223-
224-
// Call GCM onStart when FCM is not enabled.
225-
when(LeanplumPushService.isFirebaseEnabled()).thenReturn(false);
226-
onStartMethod.invoke(pushService);
227-
assertNotNull(onStartMethod);
228-
verifyStatic(times(1));
229-
LeanplumPushServiceGcm.class.getDeclaredMethod("onStart");
230222
}
231223

232224
/**

0 commit comments

Comments
 (0)