Skip to content

Commit be937e2

Browse files
authored
Xiaomi MiPush Messaging Integration (#442)
* Add MiPush module * Temporarily comment VerifyError fix * Add delivery channel to push metrics. Parse MiPush payload. * Support Mute inside app * Add unit tests * Uncomment sending of regId. Improve logging. * Send correct reg id * Fix docs and logs * Resolve lp_message from description * Add sentTime to push metrics * Remove DeliveryChannel enum to simplify the code * Use hashCode for notification id, because UUID is sent in some cases * Protect against exceptions * Fix thread safety issues * Handle lp_notification_id (occurrence id) and send with push event params * Do not initialize MiPush for non-Xiaomi devices * SDK-302 - enable/disable push delivery tracking * Change event param name for the occurrence id * Fix local pushes for offline and locked device * Change lp_occurrence_id to lp_occurrenceId * Change push param lp_occurrenceId to lp_occurrence_id
1 parent 6ed40df commit be937e2

27 files changed

+1124
-52
lines changed

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020, Leanplum, Inc. All rights reserved.
2+
* Copyright 2021, 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
@@ -110,10 +110,9 @@ public class Leanplum {
110110
private static String customAppVersion = null;
111111
private static boolean userSpecifiedDeviceId;
112112
private static boolean locationCollectionEnabled = true;
113+
private static volatile boolean pushDeliveryTrackingEnabled = true;
113114
private static Context context;
114115

115-
private static Runnable pushStartCallback;
116-
117116
private static CountAggregator countAggregator = new CountAggregator();
118117
private static FeatureFlagManager featureFlagManager = FeatureFlagManager.INSTANCE;
119118

@@ -692,6 +691,8 @@ private static void startHelper(
692691

693692
String fcmRegistrationId = SharedPreferencesUtil.getString(context,
694693
Constants.Defaults.LEANPLUM_PUSH, Constants.Defaults.PROPERTY_FCM_TOKEN_ID);
694+
String miPushRegistrationId = SharedPreferencesUtil.getString(context,
695+
Constants.Defaults.LEANPLUM_PUSH, Constants.Defaults.PROPERTY_MIPUSH_TOKEN_ID);
695696

696697
HashMap<String, Object> params = new HashMap<>();
697698
params.put(Constants.Params.INCLUDE_DEFAULTS, Boolean.toString(false));
@@ -706,6 +707,9 @@ private static void startHelper(
706707
if (!TextUtils.isEmpty(fcmRegistrationId)) {
707708
params.put(Constants.Params.DEVICE_FCM_PUSH_TOKEN, fcmRegistrationId);
708709
}
710+
if (!TextUtils.isEmpty(miPushRegistrationId)) {
711+
params.put(Constants.Params.DEVICE_MIPUSH_TOKEN, miPushRegistrationId);
712+
}
709713
params.put(Constants.Keys.TIMEZONE, localTimeZone.getID());
710714
params.put(Constants.Keys.TIMEZONE_OFFSET_SECONDS, Integer.toString(timezoneOffsetSeconds));
711715
params.put(Constants.Keys.LOCALE, Util.getLocale());
@@ -1555,10 +1559,13 @@ static void setRegistrationId(PushProviderType type, final String registrationId
15551559
case FCM:
15561560
attributeName = Constants.Params.DEVICE_FCM_PUSH_TOKEN;
15571561
break;
1562+
case MIPUSH:
1563+
attributeName = Constants.Params.DEVICE_MIPUSH_TOKEN;
1564+
break;
15581565
default:
15591566
return;
15601567
}
1561-
pushStartCallback = new Runnable() {
1568+
Runnable startIssuedHandler = new Runnable() {
15621569
@Override
15631570
public void run() {
15641571
if (Constants.isNoop()) {
@@ -1576,7 +1583,7 @@ public void run() {
15761583
}
15771584
}
15781585
};
1579-
LeanplumInternal.addStartIssuedHandler(pushStartCallback);
1586+
LeanplumInternal.addStartIssuedHandler(startIssuedHandler);
15801587
}
15811588

15821589
/**
@@ -2295,4 +2302,20 @@ public static void setEventsUploadInterval(EventsUploadInterval uploadInterval)
22952302
RequestSenderTimer.get().setTimerInterval(uploadInterval);
22962303
}
22972304
}
2305+
2306+
/**
2307+
* Enable or disable push delivery tracking. It is enabled by default.
2308+
*/
2309+
public static void setPushDeliveryTracking(boolean enable) {
2310+
pushDeliveryTrackingEnabled = enable;
2311+
}
2312+
2313+
/**
2314+
* Returns whether the push delivery tracking is enabled.
2315+
*
2316+
* @return True if push delivery tracking is enabled, false otherwise.
2317+
*/
2318+
public static boolean isPushDeliveryTrackingEnabled() {
2319+
return pushDeliveryTrackingEnabled;
2320+
}
22982321
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
package com.leanplum;
2323

2424
enum PushProviderType {
25-
FCM
25+
FCM,
26+
MIPUSH
2627
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public static class Defaults {
8787
public static final String LEANPLUM_PUSH = "__leanplum_push__";
8888
public static final String APP_ID = "__app_id";
8989
public static final String PROPERTY_FCM_TOKEN_ID = "registration_id";
90+
public static final String PROPERTY_MIPUSH_TOKEN_ID = "mipush_registration_id";
9091
public static final String PROPERTY_SENDER_IDS = "sender_ids";
9192
public static final String NOTIFICATION_CHANNELS_KEY = "__leanplum_notification_channels";
9293
public static final String DEFAULT_NOTIFICATION_CHANNEL_KEY = "__leanplum_default_notification_channels";
@@ -108,6 +109,7 @@ public static class Params {
108109
public static final String DEVICE_MODEL = "deviceModel";
109110
public static final String DEVICE_NAME = "deviceName";
110111
public static final String DEVICE_FCM_PUSH_TOKEN = "gcmRegistrationId";
112+
public static final String DEVICE_MIPUSH_TOKEN = "miPushRegId";
111113
public static final String DEVICE_SYSTEM_NAME = "systemName";
112114
public static final String DEVICE_SYSTEM_VERSION = "systemVersion";
113115
public static final String EMAIL = "email";
@@ -175,8 +177,18 @@ public static class Keys {
175177
public static final String PUSH_MESSAGE_ID_NO_MUTE = "_lpn";
176178
public static final String PUSH_MESSAGE_ID_MUTE = "_lpv";
177179
public static final String PUSH_MESSAGE_ID = "lp_messageId";
180+
public static final String PUSH_MESSAGE_NOTIFICATION_ID = "lp_notificationId";
178181
public static final String PUSH_MESSAGE_TEXT = "lp_message";
182+
public static final String PUSH_VERSION = "lp_version";
179183
public static final String PUSH_MESSAGE_IMAGE_URL = "lp_imageUrl";
184+
public static final String PUSH_METRIC_SENT_TIME = "sentTime";
185+
public static final String PUSH_METRIC_OCCURRENCE_ID = "occurrenceId";
186+
public static final String PUSH_METRIC_CHANNEL = "channel";
187+
public static final String PUSH_METRIC_MESSAGE_ID = "messageID";
188+
public static final String CHANNEL_INTERNAL_KEY = "_channel_internal";
189+
public static final String PUSH_MESSAGE_SILENT_TRACK = "lp_silent_track";
190+
public static final String PUSH_SENT_TIME = "lp_sent_time";
191+
public static final String PUSH_OCCURRENCE_ID = "lp_occurrence_id";
180192
public static final String REGION = "region";
181193
public static final String REGION_STATE = "regionState";
182194
public static final String REGIONS = "regions";

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,4 +656,13 @@ public static int generateIdFromResourceName(String resourceName) {
656656
Log.d("Could not extract resource id from provided resource name: ", resourceName);
657657
return 0;
658658
}
659+
660+
/**
661+
* Checks if device is manufactured by Xiaomi.
662+
*
663+
* @return True if device is manufactured by Xiaomi.
664+
*/
665+
public static boolean isXiaomiDevice() {
666+
return Build.MANUFACTURER != null && Build.MANUFACTURER.toLowerCase().contains("xiaomi");
667+
}
659668
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020, Leanplum, Inc. All rights reserved.
2+
* Copyright 2021, 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
@@ -26,6 +26,7 @@
2626
import com.google.firebase.messaging.FirebaseMessagingService;
2727
import com.google.firebase.messaging.RemoteMessage;
2828
import com.leanplum.internal.Constants;
29+
import com.leanplum.internal.Constants.Keys;
2930
import com.leanplum.internal.Log;
3031
import java.util.Map;
3132

@@ -57,8 +58,18 @@ public void onNewToken(String token, Context context) {
5758
public void onMessageReceived(RemoteMessage remoteMessage, Context context) {
5859
try {
5960
Map<String, String> messageMap = remoteMessage.getData();
61+
62+
String channel;
63+
if (messageMap.containsKey(Keys.PUSH_MESSAGE_SILENT_TRACK)) {
64+
channel = PushTracking.CHANNEL_FCM_SILENT_TRACK;
65+
} else {
66+
channel = PushTracking.CHANNEL_FCM;
67+
}
68+
6069
if (messageMap.containsKey(Constants.Keys.PUSH_MESSAGE_TEXT)) {
61-
LeanplumPushService.handleNotification(context, getBundle(messageMap));
70+
Bundle notification = getBundle(messageMap);
71+
notification.putString(Keys.CHANNEL_INTERNAL_KEY, channel);
72+
LeanplumPushService.handleNotification(context, notification);
6273
}
6374
Log.d("Received push notification message: %s", messageMap.toString());
6475
} catch (Throwable t) {

AndroidSDKMiPush/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

AndroidSDKMiPush/build.gradle

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
apply plugin: 'com.android.library'
2+
apply plugin: 'com.jfrog.artifactory'
3+
apply plugin: 'com.jfrog.bintray'
4+
apply plugin: 'maven-publish'
5+
apply from: "../common-methods.gradle"
6+
7+
android {
8+
compileSdkVersion COMPILE_SDK_VERSION
9+
defaultConfig {
10+
minSdkVersion MIN_SDK_VERSION
11+
defaultConfig {
12+
consumerProguardFiles 'consumer-proguard-rules.pro'
13+
}
14+
}
15+
16+
buildTypes {
17+
release {
18+
minifyEnabled true
19+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20+
}
21+
}
22+
23+
compileOptions {
24+
sourceCompatibility JAVA_VERSION
25+
targetCompatibility JAVA_VERSION
26+
}
27+
}
28+
29+
dependencies {
30+
debugApi project(':AndroidSDKCore')
31+
releaseApi project(':AndroidSDKCore')
32+
debugApi project(':AndroidSDKPush')
33+
releaseApi project(':AndroidSDKPush')
34+
35+
// MiPush SDK is located in libs
36+
implementation fileTree(dir: 'libs', include: ['*.jar'])
37+
}
38+
39+
task generateJavadoc(type: Javadoc) {
40+
source = android.sourceSets.main.java.srcDirs
41+
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
42+
destinationDir = file("./javadoc/")
43+
failOnError false
44+
}
45+
46+
publishing_task(LEANPLUM_MIPUSH_ARTIFACT_ID)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Keep default Leanplum classes.
2+
-keepclassmembers class * {
3+
@com.leanplum.annotations.* <fields>;
4+
}
5+
6+
-keep class com.leanplum.** { *; }
7+
-dontwarn com.leanplum.**
663 KB
Binary file not shown.

AndroidSDKMiPush/proguard-rules.pro

Whitespace-only changes.

0 commit comments

Comments
 (0)