Skip to content

Commit 12f2340

Browse files
committed
Upgraded firebase-messaging to inlcude 22.0.0
* Bumped the version range of firebase-messaging in onesignal/build.gradle to [19.0.0, 22.99.99]. * Since FirebaseInstanceId was droped in the version switched it's implementation to use reflection. * Added reflection rule to the consumer-progruad-rules.pro so the gralde `minifyEnabled` does not break the reflection code if someone has locked their firebase-messaging version to a pre 22.0.0 version.
1 parent 0500e51 commit 12f2340

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

OneSignalSDK/onesignal/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ext {
55

66
androidXVersion = '[1.0.0, 1.99.99]'
77
androidWorkVersion = '[2.0.0, 2.99.99]'
8-
firebaseMessagingVersion = '[19.0.0, 21.99.99]'
8+
firebaseMessagingVersion = '[19.0.0, 22.99.99]'
99
googleGMSVersion = '[17.0.0, 17.99.99]'
1010
}
1111

OneSignalSDK/onesignal/consumer-proguard-rules.pro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
void disconnect();
77
}
88

9+
# Need to keep as these 2 methods are called with reflection from com.onesignal.PushRegistratorFCM
10+
-keep class com.google.firebase.iid.FirebaseInstanceId {
11+
static com.google.firebase.iid.FirebaseInstanceId getInstance(com.google.firebase.FirebaseApp);
12+
java.lang.String getToken(java.lang.String, java.lang.String);
13+
}
914

1015
-keep class com.onesignal.ActivityLifecycleListenerCompat** {*;}
1116

OneSignalSDK/onesignal/src/main/java/com/onesignal/PushRegistratorFCM.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@
3636
import com.google.android.gms.tasks.Tasks;
3737
import com.google.firebase.FirebaseApp;
3838
import com.google.firebase.FirebaseOptions;
39-
import com.google.firebase.iid.FirebaseInstanceId;
4039
import com.google.firebase.messaging.FirebaseMessaging;
4140

4241
import java.io.IOException;
42+
import java.lang.reflect.InvocationTargetException;
43+
import java.lang.reflect.Method;
4344
import java.util.concurrent.ExecutionException;
4445

4546
class PushRegistratorFCM extends PushRegistratorAbstractGoogle {
@@ -79,10 +80,34 @@ String getToken(String senderId) throws ExecutionException, InterruptedException
7980
return getTokenWithClassFirebaseInstanceId(senderId);
8081
}
8182

83+
// This method is only used if firebase-message older than 21.0.0 is in the app
84+
// We are using reflection here so we can compile with firebase-message:22.0.0 and newer
85+
// - This version of Firebase has completely removed FirebaseInstanceId
86+
@Deprecated
8287
@WorkerThread
8388
private String getTokenWithClassFirebaseInstanceId(String senderId) throws IOException {
84-
FirebaseInstanceId instanceId = FirebaseInstanceId.getInstance(firebaseApp);
85-
return instanceId.getToken(senderId, FirebaseMessaging.INSTANCE_ID_SCOPE);
89+
// The following code is equivalent to:
90+
// FirebaseInstanceId instanceId = FirebaseInstanceId.getInstance(firebaseApp);
91+
// return instanceId.getToken(senderId, FirebaseMessaging.INSTANCE_ID_SCOPE);
92+
Exception exception;
93+
try {
94+
Class<?> FirebaseInstanceIdClass = Class.forName("com.google.firebase.iid.FirebaseInstanceId");
95+
Method getInstanceMethod = FirebaseInstanceIdClass.getMethod("getInstance", FirebaseApp.class);
96+
Object instanceId = getInstanceMethod.invoke(null, firebaseApp);
97+
Method getTokenMethod = instanceId.getClass().getMethod("getToken", String.class, String.class);
98+
Object token = getTokenMethod.invoke(instanceId, senderId, "FCM");
99+
return (String) token;
100+
} catch (ClassNotFoundException e) {
101+
exception = e;
102+
} catch (NoSuchMethodException e) {
103+
exception = e;
104+
} catch (IllegalAccessException e) {
105+
exception = e;
106+
} catch (InvocationTargetException e) {
107+
exception = e;
108+
}
109+
110+
throw new Error("Reflection error on FirebaseInstanceId.getInstance(firebaseApp).getToken(senderId, FirebaseMessaging.INSTANCE_ID_SCOPE)", exception);
86111
}
87112

88113
@WorkerThread

0 commit comments

Comments
 (0)