Skip to content

Commit b82fb2c

Browse files
authored
Support multiple push providers (#433)
* Support multiple push providers * Fix unit tests * Try to fix unit tests * Run failing test class only * Try only the failing test * Try to fix unit test * Fix unit test and revert travis changes * Renaming constants * Remove comment * Add default constructor for reflection
1 parent 8827a02 commit b82fb2c

File tree

16 files changed

+318
-430
lines changed

16 files changed

+318
-430
lines changed

AndroidSDK/proguard-rules.pro

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@
332332
}
333333

334334
-keep class com.leanplum.utils.BitmapUtil { public private protected *; }
335-
-keep class com.leanplum.LeanplumPushServiceFcm { *; }
336335
-keep class com.leanplum.LeanplumPushService { *; }
337336
-keep class com.leanplum.LeanplumFcmProvider { *; }
338337
-keep class com.leanplum.LeanplumCloudMessagingProvider{ *; }

AndroidSDKCore/src/main/java/com/leanplum/Leanplum.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,8 @@ private static void startHelper(
690690
Date now = new Date();
691691
int timezoneOffsetSeconds = localTimeZone.getOffset(now.getTime()) / 1000;
692692

693-
String registrationId = SharedPreferencesUtil.getString(context,
694-
Constants.Defaults.LEANPLUM_PUSH, Constants.Defaults.PROPERTY_TOKEN_ID);
693+
String fcmRegistrationId = SharedPreferencesUtil.getString(context,
694+
Constants.Defaults.LEANPLUM_PUSH, Constants.Defaults.PROPERTY_FCM_TOKEN_ID);
695695

696696
HashMap<String, Object> params = new HashMap<>();
697697
params.put(Constants.Params.INCLUDE_DEFAULTS, Boolean.toString(false));
@@ -703,8 +703,8 @@ private static void startHelper(
703703
params.put(Constants.Params.DEVICE_MODEL, Util.getDeviceModel());
704704
params.put(Constants.Params.DEVICE_SYSTEM_NAME, Util.getSystemName());
705705
params.put(Constants.Params.DEVICE_SYSTEM_VERSION, Util.getSystemVersion());
706-
if (!TextUtils.isEmpty(registrationId)) {
707-
params.put(Constants.Params.DEVICE_PUSH_TOKEN, registrationId);
706+
if (!TextUtils.isEmpty(fcmRegistrationId)) {
707+
params.put(Constants.Params.DEVICE_FCM_PUSH_TOKEN, fcmRegistrationId);
708708
}
709709
params.put(Constants.Keys.TIMEZONE, localTimeZone.getID());
710710
params.put(Constants.Keys.TIMEZONE_OFFSET_SECONDS, Integer.toString(timezoneOffsetSeconds));
@@ -1546,10 +1546,18 @@ public static void setUserAttributes(Map<String, Object> userAttributes) {
15461546
/**
15471547
* Sets the registration ID used for Cloud Messaging.
15481548
*/
1549-
static void setRegistrationId(final String registrationId) {
1549+
static void setRegistrationId(PushProviderType type, final String registrationId) {
15501550
if (Constants.isNoop()) {
15511551
return;
15521552
}
1553+
String attributeName;
1554+
switch (type) {
1555+
case FCM:
1556+
attributeName = Constants.Params.DEVICE_FCM_PUSH_TOKEN;
1557+
break;
1558+
default:
1559+
return;
1560+
}
15531561
pushStartCallback = new Runnable() {
15541562
@Override
15551563
public void run() {
@@ -1559,7 +1567,7 @@ public void run() {
15591567
try {
15601568
Request request = RequestBuilder
15611569
.withSetDeviceAttributesAction()
1562-
.andParam(Constants.Params.DEVICE_PUSH_TOKEN, registrationId)
1570+
.andParam(attributeName, registrationId)
15631571
.andType(RequestType.IMMEDIATE)
15641572
.create();
15651573
RequestSender.getInstance().send(request);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2020, Leanplum, Inc. All rights reserved.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package com.leanplum;
23+
24+
enum PushProviderType {
25+
FCM
26+
}

AndroidSDKCore/src/main/java/com/leanplum/internal/Constants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static class Defaults {
8686
public static final String INBOX_KEY = "__leanplum_newsfeed";
8787
public static final String LEANPLUM_PUSH = "__leanplum_push__";
8888
public static final String APP_ID = "__app_id";
89-
public static final String PROPERTY_TOKEN_ID = "registration_id";
89+
public static final String PROPERTY_FCM_TOKEN_ID = "registration_id";
9090
public static final String PROPERTY_SENDER_IDS = "sender_ids";
9191
public static final String NOTIFICATION_CHANNELS_KEY = "__leanplum_notification_channels";
9292
public static final String DEFAULT_NOTIFICATION_CHANNEL_KEY = "__leanplum_default_notification_channels";
@@ -107,7 +107,7 @@ public static class Params {
107107
public static final String DEVICE_ID = "deviceId";
108108
public static final String DEVICE_MODEL = "deviceModel";
109109
public static final String DEVICE_NAME = "deviceName";
110-
public static final String DEVICE_PUSH_TOKEN = "gcmRegistrationId";
110+
public static final String DEVICE_FCM_PUSH_TOKEN = "gcmRegistrationId";
111111
public static final String DEVICE_SYSTEM_NAME = "systemName";
112112
public static final String DEVICE_SYSTEM_VERSION = "systemVersion";
113113
public static final String EMAIL = "email";

AndroidSDKFcm/src/main/java/com/leanplum/LeanplumFcmProvider.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,37 @@
2727
import com.google.android.gms.tasks.Task;
2828
import com.google.firebase.iid.FirebaseInstanceId;
2929
import com.google.firebase.iid.InstanceIdResult;
30+
import com.leanplum.internal.Constants;
3031
import com.leanplum.internal.Log;
3132

3233
import androidx.annotation.NonNull;
3334

3435
/**
3536
* Leanplum provider for work with Firebase.
37+
* Class is instantiated by reflection using default constructor.
3638
*
3739
* @author Anna Orlova
3840
*/
3941
class LeanplumFcmProvider extends LeanplumCloudMessagingProvider {
4042

43+
/**
44+
* Constructor called by reflection.
45+
*/
46+
public LeanplumFcmProvider() {
47+
}
48+
49+
@Override
50+
protected String getSharedPrefsPropertyName() {
51+
return Constants.Defaults.PROPERTY_FCM_TOKEN_ID;
52+
}
53+
4154
@Override
42-
public String getRegistrationId() {
43-
return this.getStoredRegistrationPreferences(Leanplum.getContext());
55+
public PushProviderType getType() {
56+
return PushProviderType.FCM;
4457
}
4558

4659
@Override
47-
public void getCurrentRegistrationIdAndUpdateBackend() {
60+
public void updateRegistrationId() {
4861
FirebaseInstanceId.getInstance().getInstanceId()
4962
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
5063
@Override
@@ -57,17 +70,12 @@ public void onComplete(@NonNull Task<InstanceIdResult> task) {
5770
// Get new Instance ID token
5871
String tokenId = task.getResult().getToken();
5972
if (!TextUtils.isEmpty(tokenId)) {
60-
onRegistrationIdReceived(Leanplum.getContext(), tokenId);
73+
setRegistrationId(tokenId);
6174
}
6275
}
6376
});
6477
}
6578

66-
@Override
67-
public boolean isInitialized() {
68-
return true;
69-
}
70-
7179
@Override
7280
public void unregister() {
7381
try {

AndroidSDKFcm/src/main/java/com/leanplum/LeanplumFirebaseServiceHandler.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,8 @@ public void onCreate(Context context) {
4646
* Call from your implementation of {@link FirebaseMessagingService#onNewToken(String)}
4747
*/
4848
public void onNewToken(String token, Context context) {
49-
LeanplumPushService.setCloudMessagingProvider(new LeanplumFcmProvider());
50-
LeanplumPushService.getCloudMessagingProvider().storePreferences(context, token);
51-
5249
//send the new token to backend
53-
LeanplumPushService.getCloudMessagingProvider().onRegistrationIdReceived(context, token);
50+
LeanplumPushService.getPushProviders().setRegistrationId(PushProviderType.FCM, token);
5451
}
5552

5653
/**

AndroidSDKFcm/src/main/java/com/leanplum/LeanplumPushServiceFcm.java

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

AndroidSDKPush/src/main/AndroidManifest.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
<application>
66
<receiver android:name="com.leanplum.LeanplumJobStartReceiver" />
7-
<!-- Leanplum FCM Registration Service. -->
8-
<service android:name="com.leanplum.LeanplumPushRegistrationService" />
97
<!-- Leanplum Local Push Notification Service. -->
108
<service
119
android:name="com.leanplum.LeanplumLocalPushListenerService"
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016, Leanplum, Inc. All rights reserved.
2+
* Copyright 2020, Leanplum, Inc. All rights reserved.
33
*
44
* Licensed to the Apache Software Foundation (ASF) under one
55
* or more contributor license agreements. See the NOTICE file
@@ -21,34 +21,30 @@
2121

2222
package com.leanplum;
2323

24-
import android.content.Context;
24+
interface IPushProvider {
2525

26-
/**
27-
* Leanplum provider for manually registering for Cloud Messaging services.
28-
*
29-
* @author Anna Orlova
30-
*/
31-
public class LeanplumManualProvider extends LeanplumCloudMessagingProvider {
32-
LeanplumManualProvider(Context context, String registrationId) {
33-
onRegistrationIdReceived(context, registrationId);
34-
}
35-
36-
@Override
37-
public String getRegistrationId() {
38-
return getCurrentRegistrationId();
39-
}
26+
/**
27+
* Returns the type of this push provider.
28+
*/
29+
PushProviderType getType();
4030

41-
@Override
42-
public void getCurrentRegistrationIdAndUpdateBackend() {
31+
/**
32+
* Returns the stored registration ID.
33+
*/
34+
String getRegistrationId();
4335

44-
}
36+
/**
37+
* Stores the registration ID and sends it to backend.
38+
*/
39+
void setRegistrationId(String regId);
4540

46-
@Override
47-
public boolean isInitialized() {
48-
return true;
49-
}
41+
/**
42+
* Unregister from cloud messaging. Main usage is for testing purposes.
43+
*/
44+
void unregister();
5045

51-
@Override
52-
public void unregister() {
53-
}
46+
/**
47+
* Updates the current registration ID from the cloud messaging's API.
48+
*/
49+
void updateRegistrationId();
5450
}

0 commit comments

Comments
 (0)