|
27 | 27 |
|
28 | 28 | package com.onesignal;
|
29 | 29 |
|
| 30 | +import android.content.Context; |
30 | 31 | import android.util.Base64;
|
31 | 32 |
|
32 | 33 | import androidx.annotation.NonNull;
|
| 34 | +import androidx.annotation.Nullable; |
33 | 35 | import androidx.annotation.WorkerThread;
|
34 | 36 |
|
35 | 37 | import com.google.android.gms.tasks.Task;
|
|
45 | 47 |
|
46 | 48 | class PushRegistratorFCM extends PushRegistratorAbstractGoogle {
|
47 | 49 |
|
48 |
| - // project_info.project_id |
49 |
| - private static final String FCM_DEFAULT_PROJECT_ID = "onesignal-shared-public"; |
50 |
| - // client.client_info.mobilesdk_app_id |
51 |
| - private static final String FCM_DEFAULT_APP_ID = "1:754795614042:android:c682b8144a8dd52bc1ad63"; |
52 |
| - // client.api_key.current_key |
53 |
| - private static final String FCM_DEFAULT_API_KEY_BASE64 = "QUl6YVN5QW5UTG41LV80TWMyYTJQLWRLVWVFLWFCdGd5Q3JqbFlV"; |
54 |
| - |
55 | 50 | private static final String FCM_APP_NAME = "ONESIGNAL_SDK_FCM_APP_NAME";
|
56 | 51 |
|
| 52 | + static class Params { |
| 53 | + // project_info.project_id |
| 54 | + private static final String FCM_DEFAULT_PROJECT_ID = "onesignal-shared-public"; |
| 55 | + // client.client_info.mobilesdk_app_id |
| 56 | + private static final String FCM_DEFAULT_APP_ID = "1:754795614042:android:c682b8144a8dd52bc1ad63"; |
| 57 | + // client.api_key.current_key |
| 58 | + private static final String FCM_DEFAULT_API_KEY_BASE64 = "QUl6YVN5QW5UTG41LV80TWMyYTJQLWRLVWVFLWFCdGd5Q3JqbFlV"; |
| 59 | + |
| 60 | + @NonNull private final String projectId; |
| 61 | + @NonNull private final String appId; |
| 62 | + @NonNull private final String apiKey; |
| 63 | + |
| 64 | + Params() { |
| 65 | + this(null, null, null); |
| 66 | + } |
| 67 | + |
| 68 | + Params( |
| 69 | + @Nullable String projectId, |
| 70 | + @Nullable String appId, |
| 71 | + @Nullable String apiKey |
| 72 | + ) { |
| 73 | + this.projectId = projectId != null ? projectId : FCM_DEFAULT_PROJECT_ID; |
| 74 | + this.appId = appId != null ? appId : FCM_DEFAULT_APP_ID; |
| 75 | + |
| 76 | + String defaultApiKey = new String(Base64.decode(FCM_DEFAULT_API_KEY_BASE64, Base64.DEFAULT)); |
| 77 | + this.apiKey = apiKey != null ? apiKey : defaultApiKey; |
| 78 | + } |
| 79 | + } |
| 80 | + |
57 | 81 | private FirebaseApp firebaseApp;
|
58 | 82 |
|
| 83 | + @NonNull private final Context context; |
| 84 | + @NonNull private final Params params; |
| 85 | + |
| 86 | + PushRegistratorFCM( |
| 87 | + @NonNull Context context, |
| 88 | + @Nullable Params params |
| 89 | + ) { |
| 90 | + this.context = context; |
| 91 | + |
| 92 | + if (params == null) { |
| 93 | + this.params = new Params(); |
| 94 | + } else { |
| 95 | + this.params = params; |
| 96 | + } |
| 97 | + } |
| 98 | + |
59 | 99 | @Override
|
60 | 100 | String getProviderName() {
|
61 | 101 | return "FCM";
|
@@ -125,32 +165,13 @@ private void initFirebaseApp(String senderId) {
|
125 | 165 | if (firebaseApp != null)
|
126 | 166 | return;
|
127 | 167 |
|
128 |
| - OneSignalRemoteParams.Params remoteParams = OneSignal.getRemoteParams(); |
129 | 168 | FirebaseOptions firebaseOptions =
|
130 | 169 | new FirebaseOptions.Builder()
|
131 | 170 | .setGcmSenderId(senderId)
|
132 |
| - .setApplicationId(getAppId(remoteParams)) |
133 |
| - .setApiKey(getApiKey(remoteParams)) |
134 |
| - .setProjectId(getProjectId(remoteParams)) |
| 171 | + .setApplicationId(params.appId) |
| 172 | + .setApiKey(params.apiKey) |
| 173 | + .setProjectId(params.projectId) |
135 | 174 | .build();
|
136 |
| - firebaseApp = FirebaseApp.initializeApp(OneSignal.appContext, firebaseOptions, FCM_APP_NAME); |
137 |
| - } |
138 |
| - |
139 |
| - private static @NonNull String getAppId(OneSignalRemoteParams.Params remoteParams) { |
140 |
| - if (remoteParams.fcmParams.appId != null) |
141 |
| - return remoteParams.fcmParams.appId; |
142 |
| - return FCM_DEFAULT_APP_ID; |
143 |
| - } |
144 |
| - |
145 |
| - private static @NonNull String getApiKey(OneSignalRemoteParams.Params remoteParams) { |
146 |
| - if (remoteParams.fcmParams.apiKey != null) |
147 |
| - return remoteParams.fcmParams.apiKey; |
148 |
| - return new String(Base64.decode(FCM_DEFAULT_API_KEY_BASE64, Base64.DEFAULT)); |
149 |
| - } |
150 |
| - |
151 |
| - private static @NonNull String getProjectId(OneSignalRemoteParams.Params remoteParams) { |
152 |
| - if (remoteParams.fcmParams.projectId != null) |
153 |
| - return remoteParams.fcmParams.projectId; |
154 |
| - return FCM_DEFAULT_PROJECT_ID; |
| 175 | + firebaseApp = FirebaseApp.initializeApp(context, firebaseOptions, FCM_APP_NAME); |
155 | 176 | }
|
156 | 177 | }
|
0 commit comments