Skip to content

Commit 3515ae9

Browse files
authored
Merge pull request #1348 from OneSignal/fix/firebase-messaging-22.0.0_compat_for_3.x.x
Fix Firebase Messaging 22.0.0 Compatibility for 3.x.x branch
2 parents ed58483 + debbaf1 commit 3515ae9

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
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: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,24 @@
3333
import android.util.Base64;
3434

3535
import android.support.annotation.NonNull;
36+
import android.support.annotation.WorkerThread;
3637

38+
import com.google.android.gms.tasks.Task;
39+
import com.google.android.gms.tasks.Tasks;
3740
import com.google.firebase.FirebaseApp;
3841
import com.google.firebase.FirebaseOptions;
3942
import com.google.firebase.iid.FirebaseInstanceId;
4043
import com.google.firebase.iid.FirebaseInstanceIdService;
4144
import com.google.firebase.messaging.FirebaseMessaging;
4245

46+
import java.io.IOException;
47+
import java.lang.reflect.InvocationTargetException;
48+
import java.lang.reflect.Method;
49+
import java.util.concurrent.ExecutionException;
50+
4351
// TODO: 4.0.0 - Switch to using <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
4452
// Note: Starting with Firebase Messaging 17.1.0 onNewToken in FirebaseMessagingService should be
4553
// used instead.
46-
4754
class PushRegistratorFCM extends PushRegistratorAbstractGoogle {
4855

4956
// project_info.project_id
@@ -88,13 +95,59 @@ String getProviderName() {
8895
return "FCM";
8996
}
9097

98+
@WorkerThread
9199
@Override
92-
String getToken(String senderId) throws Throwable {
100+
String getToken(String senderId) throws ExecutionException, InterruptedException, IOException {
93101
initFirebaseApp(senderId);
102+
103+
try {
104+
return getTokenWithClassFirebaseMessaging();
105+
} catch (Error e) {
106+
// Class or method will be missing at runtime if firebase-message older than 21.0.0 is used.
107+
OneSignal.Log(
108+
OneSignal.LOG_LEVEL.INFO,
109+
"FirebaseMessaging.getToken not found, attempting to use FirebaseInstanceId.getToken"
110+
);
111+
}
112+
113+
// Fallback for firebase-message versions older than 21.0.0
114+
return getTokenWithClassFirebaseInstanceId(senderId);
115+
}
116+
117+
@WorkerThread
118+
private String getTokenWithClassFirebaseInstanceId(String senderId) throws IOException {
94119
FirebaseInstanceId instanceId = FirebaseInstanceId.getInstance(firebaseApp);
95120
return instanceId.getToken(senderId, FirebaseMessaging.INSTANCE_ID_SCOPE);
96121
}
97122

123+
@WorkerThread
124+
private String getTokenWithClassFirebaseMessaging() throws ExecutionException, InterruptedException {
125+
// We use firebaseApp.get(FirebaseMessaging.class) instead of FirebaseMessaging.getInstance()
126+
// as the latter uses the default Firebase app. We need to use a custom Firebase app as
127+
// the senderId is provided at runtime.
128+
FirebaseMessaging firebaseMessagingInstance = firebaseApp.get(FirebaseMessaging.class);
129+
130+
if (firebaseMessagingInstance == null) {
131+
throw new Error("firebaseMessagingInstance is null");
132+
}
133+
134+
Exception exception;
135+
// FirebaseMessaging.getToken API was introduced in firebase-messaging:21.0.0
136+
try {
137+
Method getTokenMethod = firebaseMessagingInstance.getClass().getMethod("getToken");
138+
Object tokenTask = getTokenMethod.invoke(firebaseMessagingInstance);
139+
return Tasks.await((Task<String>)tokenTask);
140+
} catch (NoSuchMethodException e) {
141+
exception = e;
142+
} catch (IllegalAccessException e) {
143+
exception = e;
144+
} catch (InvocationTargetException e) {
145+
exception = e;
146+
}
147+
148+
throw new Error("Reflection error on FirebaseMessaging.getToken()", exception);
149+
}
150+
98151
private void initFirebaseApp(String senderId) {
99152
if (firebaseApp != null)
100153
return;

0 commit comments

Comments
 (0)