Skip to content

Commit 4574702

Browse files
committed
Call FirebaseMessaging.getToken with reflection
* Since we can't bump firebase-messaging high enougth to get the FirebaseMessaging.getToken method compile with we are instead calling it with reflection. * We can't bump any higher as this would interduce AndroidX which would interduce breaking changes we can't do on this 3.x.x branch.
1 parent 31f80d2 commit 4574702

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

OneSignalSDK/onesignal/consumer-proguard-rules.pro

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

9+
# Need to keep as getToken as it is called with reflection from com.onesignal.PushRegistratorFCM
10+
-keep class com.google.firebase.messaging.FirebaseMessaging {
11+
com.google.android.gms.tasks.Task getToken();
12+
}
913

1014
-keep class com.onesignal.ActivityLifecycleListenerCompat** {*;}
1115

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import com.google.firebase.messaging.FirebaseMessaging;
4545

4646
import java.io.IOException;
47+
import java.lang.reflect.InvocationTargetException;
48+
import java.lang.reflect.Method;
4749
import java.util.concurrent.ExecutionException;
4850

4951
// TODO: 4.0.0 - Switch to using <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
@@ -100,8 +102,8 @@ String getToken(String senderId) throws ExecutionException, InterruptedException
100102

101103
try {
102104
return getTokenWithClassFirebaseMessaging();
103-
} catch (NoClassDefFoundError | NoSuchMethodError e) {
104-
// Class or method wil be missing at runtime if firebase-message older than 21.0.0 is used.
105+
} catch (Error e) {
106+
// Class or method will be missing at runtime if firebase-message older than 21.0.0 is used.
105107
OneSignal.Log(
106108
OneSignal.LOG_LEVEL.INFO,
107109
"FirebaseMessaging.getToken not found, attempting to use FirebaseInstanceId.getToken"
@@ -124,9 +126,22 @@ private String getTokenWithClassFirebaseMessaging() throws ExecutionException, I
124126
// as the latter uses the default Firebase app. We need to use a custom Firebase app as
125127
// the senderId is provided at runtime.
126128
FirebaseMessaging fcmInstance = firebaseApp.get(FirebaseMessaging.class);
129+
130+
Exception exception;
127131
// FirebaseMessaging.getToken API was introduced in firebase-messaging:21.0.0
128-
Task<String> tokenTask = fcmInstance.getToken();
129-
return Tasks.await(tokenTask);
132+
try {
133+
Method getTokenMethod = fcmInstance.getClass().getMethod("getToken");
134+
Object tokenTask = getTokenMethod.invoke(fcmInstance);
135+
return Tasks.await((Task<String>)tokenTask);
136+
} catch (NoSuchMethodException e) {
137+
exception = e;
138+
} catch (IllegalAccessException e) {
139+
exception = e;
140+
} catch (InvocationTargetException e) {
141+
exception = e;
142+
}
143+
144+
throw new Error("Reflection error on FirebaseMessaging.getToken()", exception);
130145
}
131146

132147
private void initFirebaseApp(String senderId) {

0 commit comments

Comments
 (0)