Skip to content

Commit 35208c7

Browse files
committed
Throw specific error on FCM error over generic
When there is an FCM error, it always throws an ExecutionException type instead of the root cause error due to the Task.await used. To fix this we need to catch this and use task.getException() to get the root cause. https://developers.google.com/android/guides/tasks
1 parent 8c0ffe5 commit 35208c7

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ String getProviderName() {
103103

104104
@WorkerThread
105105
@Override
106-
String getToken(String senderId) throws ExecutionException, InterruptedException, IOException {
106+
String getToken(String senderId) throws Exception {
107107
initFirebaseApp(senderId);
108108

109109
try {
@@ -151,14 +151,18 @@ private String getTokenWithClassFirebaseInstanceId(String senderId) throws IOExc
151151
}
152152

153153
@WorkerThread
154-
private String getTokenWithClassFirebaseMessaging() throws ExecutionException, InterruptedException {
154+
private String getTokenWithClassFirebaseMessaging() throws Exception {
155155
// We use firebaseApp.get(FirebaseMessaging.class) instead of FirebaseMessaging.getInstance()
156156
// as the latter uses the default Firebase app. We need to use a custom Firebase app as
157157
// the senderId is provided at runtime.
158158
FirebaseMessaging fcmInstance = firebaseApp.get(FirebaseMessaging.class);
159159
// FirebaseMessaging.getToken API was introduced in firebase-messaging:21.0.0
160160
Task<String> tokenTask = fcmInstance.getToken();
161-
return Tasks.await(tokenTask);
161+
try {
162+
return Tasks.await(tokenTask);
163+
} catch (ExecutionException e) {
164+
throw tokenTask.getException();
165+
}
162166
}
163167

164168
private void initFirebaseApp(String senderId) {

0 commit comments

Comments
 (0)