|
36 | 36 | import com.google.android.gms.tasks.Tasks;
|
37 | 37 | import com.google.firebase.FirebaseApp;
|
38 | 38 | import com.google.firebase.FirebaseOptions;
|
39 |
| -import com.google.firebase.iid.FirebaseInstanceId; |
40 | 39 | import com.google.firebase.messaging.FirebaseMessaging;
|
41 | 40 |
|
42 | 41 | import java.io.IOException;
|
| 42 | +import java.lang.reflect.InvocationTargetException; |
| 43 | +import java.lang.reflect.Method; |
43 | 44 | import java.util.concurrent.ExecutionException;
|
44 | 45 |
|
45 | 46 | class PushRegistratorFCM extends PushRegistratorAbstractGoogle {
|
@@ -79,10 +80,34 @@ String getToken(String senderId) throws ExecutionException, InterruptedException
|
79 | 80 | return getTokenWithClassFirebaseInstanceId(senderId);
|
80 | 81 | }
|
81 | 82 |
|
| 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 |
82 | 87 | @WorkerThread
|
83 | 88 | 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); |
86 | 111 | }
|
87 | 112 |
|
88 | 113 | @WorkerThread
|
|
0 commit comments