Skip to content

Commit a87c932

Browse files
committed
rm direct ref to OneSignal in PushRegistratorFCM
* Direct OneSignal refs in PushRegistratorFCM removed - OneSignal.appContext - this is now a constructor param - OneSignalRemoteParams.Params - This now has it's own Params class which is now a constructor param.
1 parent 2dd1948 commit a87c932

File tree

2 files changed

+62
-31
lines changed

2 files changed

+62
-31
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,13 +1015,23 @@ private static PushRegistrator getPushRegistrator() {
10151015
mPushRegistrator = new PushRegistratorADM();
10161016
else if (OSUtils.isAndroidDeviceType()) {
10171017
if (OSUtils.hasFCMLibrary())
1018-
mPushRegistrator = new PushRegistratorFCM();
1018+
mPushRegistrator = getPushRegistratorFCM();
10191019
} else
10201020
mPushRegistrator = new PushRegistratorHMS();
10211021

10221022
return mPushRegistrator;
10231023
}
10241024

1025+
@NonNull
1026+
static private PushRegistratorFCM getPushRegistratorFCM() {
1027+
OneSignalRemoteParams.FCMParams fcmRemoteParams = remoteParamController.getRemoteParams().fcmParams;
1028+
PushRegistratorFCM.Params fcmParams = null;
1029+
if (fcmRemoteParams != null) {
1030+
fcmParams = new PushRegistratorFCM.Params(fcmRemoteParams.projectId, fcmRemoteParams.appId, fcmRemoteParams.apiKey);
1031+
}
1032+
return new PushRegistratorFCM(appContext, fcmParams);
1033+
}
1034+
10251035
private static void registerForPushToken() {
10261036
getPushRegistrator().registerForPush(appContext, googleProjectNumber, new PushRegistrator.RegisteredHandler() {
10271037
@Override

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

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727

2828
package com.onesignal;
2929

30+
import android.content.Context;
3031
import android.util.Base64;
3132

3233
import androidx.annotation.NonNull;
34+
import androidx.annotation.Nullable;
3335
import androidx.annotation.WorkerThread;
3436

3537
import com.google.android.gms.tasks.Task;
@@ -45,17 +47,55 @@
4547

4648
class PushRegistratorFCM extends PushRegistratorAbstractGoogle {
4749

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-
5550
private static final String FCM_APP_NAME = "ONESIGNAL_SDK_FCM_APP_NAME";
5651

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+
5781
private FirebaseApp firebaseApp;
5882

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+
5999
@Override
60100
String getProviderName() {
61101
return "FCM";
@@ -125,32 +165,13 @@ private void initFirebaseApp(String senderId) {
125165
if (firebaseApp != null)
126166
return;
127167

128-
OneSignalRemoteParams.Params remoteParams = OneSignal.getRemoteParams();
129168
FirebaseOptions firebaseOptions =
130169
new FirebaseOptions.Builder()
131170
.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)
135174
.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);
155176
}
156177
}

0 commit comments

Comments
 (0)