Skip to content

Commit e3cac7d

Browse files
jinliu9508jkasten2
authored andcommitted
Add UI element to DemoApp to manually test JWT impl
1 parent 28b51bb commit e3cac7d

File tree

4 files changed

+151
-18
lines changed

4 files changed

+151
-18
lines changed

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/application/MainApplication.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import androidx.annotation.NonNull;
77
import androidx.annotation.Nullable;
88
import androidx.multidex.MultiDexApplication;
9-
109
import com.onesignal.Continue;
10+
import com.onesignal.IUserJwtInvalidatedListener;
1111
import com.onesignal.OneSignal;
12+
import com.onesignal.UserJwtInvalidatedEvent;
1213
import com.onesignal.inAppMessages.IInAppMessageClickListener;
1314
import com.onesignal.inAppMessages.IInAppMessageClickEvent;
1415
import com.onesignal.inAppMessages.IInAppMessageDidDismissEvent;
@@ -140,6 +141,16 @@ public void onUserStateChange(@NonNull UserChangedState state) {
140141
}
141142
});
142143

144+
OneSignal.addUserJwtInvalidatedListner(new IUserJwtInvalidatedListener() {
145+
@Override
146+
public void onUserJwtInvalidated(@NonNull UserJwtInvalidatedEvent event) {
147+
// !!! For manual testing only
148+
String jwt = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIxNjg4ZDhmMi1kYTdmLTQ4MTUtOGVlMy05ZDEzNzg4NDgyYzgiLCJpYXQiOjE3MTgzMDk5NzIsImlkZW50aXR5Ijp7ImV4dGVybmFsX2lkIjoiYWxleC0wNjE0Iiwib25lc2lnbmFsX2lkIjoiYTViYjc4NDYtYzExNC00YzdkLTkzMWYtNGQ0NjhiMGE5OWJhIn0sInN1YnNjcmlwdGlvbnMiOlt7InR5cGUiOiJFbWFpbCIsInRva2VuIjoidGVzdEBkb21haW4uY29tIn0seyJpZCI6ImE2YzQxNmY3LTMxMGUtNDgzNi05Yjc4LWZiZmQ5NTgyNWNjNCJ9XX0.HsjsA2qNPwd9qov_8Px01km-dzRug-YKNNG85cMrGYI9Pdb2uoPQSdAN3Uqu7_o4pL8FRxXliYJrC52-9wH3FQ";
149+
OneSignal.updateUserJwt(event.getExternalId(), jwt);
150+
Log.v(Tag.LOG_TAG, "onUserJwtInvalidated fired with ID:" + event.getExternalId());
151+
}
152+
});
153+
143154
OneSignal.getInAppMessages().setPaused(true);
144155
OneSignal.getLocation().setShared(false);
145156

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/model/MainActivityViewModel.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import android.view.View;
1919
import android.view.ViewTreeObserver;
2020
import android.widget.Button;
21+
import android.widget.EditText;
2122
import android.widget.LinearLayout;
2223
import android.widget.RelativeLayout;
2324
import android.widget.Switch;
@@ -83,6 +84,10 @@ public class MainActivityViewModel implements ActivityViewModel, IPushSubscripti
8384
private Button loginUserButton;
8485
private Button logoutUserButton;
8586

87+
// JWT
88+
private Button invalidateJwtButton;
89+
private Button updateJwtButton;
90+
8691
// Alias
8792
private TextView aliasTitleTextView;
8893
private RecyclerView aliasesRecyclerView;
@@ -211,6 +216,9 @@ public ActivityViewModel onActivityCreated(Context context) {
211216
loginUserButton = getActivity().findViewById(R.id.main_activity_login_user_button);
212217
logoutUserButton = getActivity().findViewById(R.id.main_activity_logout_user_button);
213218

219+
invalidateJwtButton = getActivity().findViewById(R.id.main_activity_invalidate_jwt_button);
220+
updateJwtButton = getActivity().findViewById(R.id.main_activity_update_jwt_button);
221+
214222
aliasTitleTextView = getActivity().findViewById(R.id.main_activity_aliases_title_text_view);
215223
noAliasesTextView = getActivity().findViewById(R.id.main_activity_aliases_no_aliases_text_view);
216224
addAliasButton = getActivity().findViewById(R.id.main_activity_add_alias_button);
@@ -403,7 +411,8 @@ private void setupAppLayout() {
403411
@Override
404412
public void onSuccess(String update) {
405413
if (update != null && !update.isEmpty()) {
406-
OneSignal.login(update);
414+
String jwt = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIxNjg4ZDhmMi1kYTdmLTQ4MTUtOGVlMy05ZDEzNzg4NDgyYzgiLCJpYXQiOjE3MTU5NzMwNzAsImlkZW50aXR5Ijp7ImV4dGVybmFsX2lkIjoiYWxleC0wNTE3Iiwib25lc2lnbmFsX2lkIjoiMGIzYWMyN2EtYWQ4Yi00MWVjLWJhYTYtMzI0NmNkODIyMjJkIn0sInN1YnNjcmlwdGlvbnMiOlt7InR5cGUiOiJFbWFpbCIsInRva2VuIjoiYWxleHRzYXktMDUxN0BvbmVzaWduYWwuY29tIn0seyJ0eXBlIjoiQW5kcm9pZFB1c2giLCJpZCI6ImFkMTAxY2FjLTA5MWItNDkyYy04OGJiLTgxNmZkNTNjYTBmMSJ9XX0._tlD2X8J16gDkP7__FJ8CwpqCLDwb8T14m2ugJwQvuQqbIn4b8o75cKbffbjVGcKP3YaudLCebit53aR9LTQCw";
415+
OneSignal.login(update, jwt);
407416
refreshState();
408417
}
409418
}
@@ -422,6 +431,7 @@ public void onFailure() {
422431
}
423432

424433
private void setupUserLayout() {
434+
setupJWTLayout();
425435
setupAliasLayout();
426436
setupEmailLayout();
427437
setupSMSLayout();
@@ -430,6 +440,30 @@ private void setupUserLayout() {
430440
setupTriggersLayout();
431441
}
432442

443+
private void setupJWTLayout() {
444+
invalidateJwtButton.setOnClickListener(v -> {
445+
OneSignal.updateUserJwt(OneSignal.getUser().getExternalId(), "");
446+
});
447+
updateJwtButton.setOnClickListener(v -> {
448+
dialog.createUpdateAlertDialog("", Dialog.DialogAction.UPDATE, ProfileUtil.FieldType.JWT, new UpdateAlertDialogCallback() {
449+
@Override
450+
public void onSuccess(String update) {
451+
if (update != null && !update.isEmpty()) {
452+
OneSignal.updateUserJwt(OneSignal.getUser().getExternalId(), update);
453+
//String jwt = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIxNjg4ZDhmMi1kYTdmLTQ4MTUtOGVlMy05ZDEzNzg4NDgyYzgiLCJpYXQiOjE3MTQwODA4MTMsImlkZW50aXR5Ijp7ImV4dGVybmFsX2lkIjoiMjAyNDA0MjUtYWxleDQyIn0sInN1YnNjcmlwdGlvbiI6W3sidHlwZSI6IiIsImlkIjoiMmRlZGU3MzItMTEyNi00MTlkLTk5M2UtNDIzYWQyYTZiZGU5In1dfQ.rzZ-HaDm1EwxbMxd8937bWzPhPSQDDSqSzaASgZZc5A5v8g6ZQHizN9CljOmoQ4lTLm7noD6rOmR07tlZdrI5w";
454+
//OneSignal.login(update, jwt);
455+
refreshState();
456+
}
457+
}
458+
459+
@Override
460+
public void onFailure() {
461+
462+
}
463+
});
464+
});
465+
}
466+
433467
private void setupAliasLayout() {
434468
setupAliasesRecyclerView();
435469
addAliasButton.setOnClickListener(v -> dialog.createAddPairAlertDialog("Add Alias", ProfileUtil.FieldType.ALIAS, new AddPairAlertDialogCallback() {

Examples/OneSignalDemo/app/src/main/res/layout/main_activity_layout.xml

Lines changed: 99 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,20 @@
152152
android:layout_width="wrap_content"
153153
android:layout_height="wrap_content"
154154
android:layout_gravity="start|center_vertical"
155-
android:gravity="start|center_vertical"
156-
android:layout_marginBottom="4dp"
157155
android:layout_marginStart="12dp"
158156
android:layout_marginLeft="12dp"
157+
android:layout_marginBottom="4dp"
158+
android:gravity="start|center_vertical"
159159
android:text="@string/app"
160160
android:textColor="@color/colorDarkText" />
161161

162162
<androidx.cardview.widget.CardView
163163
android:layout_width="match_parent"
164164
android:layout_height="wrap_content"
165-
android:layout_marginTop="4dp"
166-
android:layout_marginBottom="12dp"
167165
android:layout_marginStart="12dp"
166+
android:layout_marginTop="4dp"
168167
android:layout_marginEnd="12dp"
168+
android:layout_marginBottom="12dp"
169169
app:cardCornerRadius="6dp"
170170
app:cardElevation="4dp">
171171

@@ -226,23 +226,23 @@
226226
android:layout_width="match_parent"
227227
android:layout_height="56dp"
228228
android:layout_gravity="center"
229-
android:gravity="center"
230229
android:layout_marginStart="12dp"
231230
android:layout_marginTop="4dp"
232231
android:layout_marginEnd="12dp"
233232
android:layout_marginBottom="12dp"
234-
android:orientation="vertical"
235-
android:background="@color/colorPrimary">
233+
android:background="@color/colorPrimary"
234+
android:gravity="center"
235+
android:orientation="vertical">
236236

237237
<Button
238238
android:id="@+id/main_activity_app_revoke_consent_button"
239239
android:layout_width="match_parent"
240240
android:layout_height="match_parent"
241+
android:background="@drawable/ripple_selector_white_red"
241242
android:text="@string/revoke_consent"
242-
android:textSize="19sp"
243243
android:textColor="@android:color/white"
244-
android:background="@drawable/ripple_selector_white_red"
245-
android:visibility="visible"/>
244+
android:textSize="19sp"
245+
android:visibility="visible" />
246246

247247
</LinearLayout>
248248

@@ -263,11 +263,11 @@
263263
android:id="@+id/main_activity_login_user_button"
264264
android:layout_width="match_parent"
265265
android:layout_height="match_parent"
266+
android:background="@drawable/ripple_selector_white_red"
266267
android:text="@string/login_user"
267-
android:textSize="19sp"
268268
android:textColor="@android:color/white"
269-
android:background="@drawable/ripple_selector_white_red"
270-
android:visibility="visible"/>
269+
android:textSize="19sp"
270+
android:visibility="visible" />
271271

272272
</LinearLayout>
273273

@@ -288,14 +288,97 @@
288288
android:id="@+id/main_activity_logout_user_button"
289289
android:layout_width="match_parent"
290290
android:layout_height="match_parent"
291+
android:background="@drawable/ripple_selector_white_red"
291292
android:text="@string/logout_user"
292-
android:textSize="19sp"
293293
android:textColor="@android:color/white"
294-
android:background="@drawable/ripple_selector_white_red"
295-
android:visibility="visible"/>
294+
android:textSize="19sp"
295+
android:visibility="visible" />
296296
</LinearLayout>
297297
</LinearLayout>
298298

299+
<LinearLayout
300+
android:id="@+id/main_activity_jwt_linear_layout"
301+
android:layout_width="match_parent"
302+
android:layout_height="match_parent"
303+
android:clipChildren="false"
304+
android:clipToPadding="false"
305+
android:orientation="vertical">
306+
307+
<TextView
308+
android:id="@+id/main_activity_jwt_title_text_view"
309+
android:layout_width="wrap_content"
310+
android:layout_height="wrap_content"
311+
android:layout_gravity="start|center_vertical"
312+
android:layout_marginStart="12dp"
313+
android:layout_marginLeft="12dp"
314+
android:layout_marginBottom="4dp"
315+
android:gravity="start|center_vertical"
316+
android:text="@string/JWT"
317+
android:textColor="@color/colorDarkText" />
318+
319+
<androidx.cardview.widget.CardView
320+
android:layout_width="match_parent"
321+
android:layout_height="wrap_content"
322+
android:layout_marginStart="12dp"
323+
android:layout_marginTop="4dp"
324+
android:layout_marginEnd="12dp"
325+
android:layout_marginBottom="12dp"
326+
app:cardCornerRadius="6dp"
327+
app:cardElevation="4dp">
328+
329+
<LinearLayout
330+
android:id="@+id/main_activity_jwt_details_linear_layout"
331+
android:layout_width="match_parent"
332+
android:layout_height="wrap_content"
333+
android:orientation="vertical">
334+
335+
<LinearLayout
336+
android:layout_width="match_parent"
337+
android:layout_height="match_parent"
338+
android:layout_gravity="center"
339+
android:layout_marginTop="4dp"
340+
android:background="@color/colorPrimary"
341+
android:gravity="center"
342+
android:orientation="vertical">
343+
344+
<Button
345+
android:id="@+id/main_activity_invalidate_jwt_button"
346+
android:layout_width="match_parent"
347+
android:layout_height="match_parent"
348+
android:background="@drawable/ripple_selector_white_red"
349+
android:text="@string/invalidate_JWT"
350+
android:textColor="@android:color/white"
351+
android:textSize="19sp"
352+
android:visibility="visible" />
353+
354+
</LinearLayout>
355+
356+
<LinearLayout
357+
android:layout_width="match_parent"
358+
android:layout_height="match_parent"
359+
android:layout_gravity="center"
360+
android:layout_marginTop="4dp"
361+
android:background="@color/colorPrimary"
362+
android:gravity="center"
363+
android:orientation="vertical">
364+
365+
<Button
366+
android:id="@+id/main_activity_update_jwt_button"
367+
android:layout_width="match_parent"
368+
android:layout_height="match_parent"
369+
android:background="@drawable/ripple_selector_white_red"
370+
android:text="@string/update_JWT"
371+
android:textColor="@android:color/white"
372+
android:textSize="19sp"
373+
android:visibility="visible" />
374+
</LinearLayout>
375+
376+
</LinearLayout>
377+
378+
</androidx.cardview.widget.CardView>
379+
380+
</LinearLayout>
381+
299382
<!-- Aliases -->
300383
<LinearLayout
301384
android:id="@+id/main_activity_aliases_linear_layout"

Examples/OneSignalDemo/app/src/main/res/values/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
<string name="login_user">Login User</string>
1010
<string name="logout_user">Logout User</string>
1111

12+
<string name="JWT">JWT</string>
13+
<string name="invalidate_JWT">Invalidate JWT</string>
14+
<string name="update_JWT">Update JWT</string>
15+
<string name="no_jwt_added">No JWT Added</string>
16+
1217
<string name="aliases">Aliases</string>
1318
<string name="external_user_id_colon">EUID:</string>
1419
<string name="no_aliases_added">No Aliases Added</string>

0 commit comments

Comments
 (0)