Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.multidex.MultiDexApplication;

import com.onesignal.Continue;
import com.onesignal.IUserJwtInvalidatedListener;
import com.onesignal.OneSignal;
import com.onesignal.UserJwtInvalidatedEvent;
import com.onesignal.inAppMessages.IInAppMessageClickListener;
import com.onesignal.inAppMessages.IInAppMessageClickEvent;
import com.onesignal.inAppMessages.IInAppMessageDidDismissEvent;
Expand Down Expand Up @@ -65,7 +66,7 @@ public void onCreate() {
// This will reproduce result similar to Kotlin CouroutineScope.launch{}, which may potentially crash the app
ExecutorService executor = Executors.newSingleThreadExecutor();
@SuppressLint({"NewApi", "LocalSuppress"}) CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
OneSignal.getNotifications().requestPermission(true, Continue.none());
//OneSignal.getNotifications().requestPermission(true, Continue.none());
}, executor);
future.join(); // Waits for the task to complete
executor.shutdown();
Expand Down Expand Up @@ -140,6 +141,16 @@ public void onUserStateChange(@NonNull UserChangedState state) {
}
});

OneSignal.addUserJwtInvalidatedListner(new IUserJwtInvalidatedListener() {
@Override
public void onUserJwtInvalidated(@NonNull UserJwtInvalidatedEvent event) {
// !!! For manual testing only
String jwt = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIwMTM5YmQ2Zi00NTFmLTQzOGMtODg4Ni00ZTBmMGZlM2EwODUiLCJleHAiOjE3MjY3Njk1MDYsImlkZW50aXR5Ijp7ImV4dGVybmFsX2lkIjoiamluIn0sInN1YnNjcmlwdGlvbnMiOlt7InR5cGUiOiJFbWFpbCIsInRva2VuIjoidGVzdEBkb21haW4uY29tIn0seyJ0eXBlIjoiU01TIiwidG9rZW4iOiIrMTIzNDU2NzgifSx7InR5cGUiOiJBbmRyb2lkUHVzaCIsImlkIjoiMTIzZTQ1NjctZTg5Yi0xMmQzLWE0NTYtNDI2NjE0MTc0MDAwIn1dfQ.g48KD-3GPjjbFOotpWcC3Xxc2Rou_ne4Zbumy2XkQLS0VEF_hbZ01q1jLGdL_hlbSzYzhjo8kWpL2bKb0ZE0yw";
OneSignal.updateUserJwt(event.getExternalId(), jwt);
Log.v(Tag.LOG_TAG, "onUserJwtInvalidated fired with ID:" + event.getExternalId());
}
});

OneSignal.getInAppMessages().setPaused(true);
OneSignal.getLocation().setShared(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
Expand Down Expand Up @@ -83,6 +84,10 @@ public class MainActivityViewModel implements ActivityViewModel, IPushSubscripti
private Button loginUserButton;
private Button logoutUserButton;

// JWT
private Button invalidateJwtButton;
private Button updateJwtButton;

// Alias
private TextView aliasTitleTextView;
private RecyclerView aliasesRecyclerView;
Expand Down Expand Up @@ -211,6 +216,9 @@ public ActivityViewModel onActivityCreated(Context context) {
loginUserButton = getActivity().findViewById(R.id.main_activity_login_user_button);
logoutUserButton = getActivity().findViewById(R.id.main_activity_logout_user_button);

invalidateJwtButton = getActivity().findViewById(R.id.main_activity_invalidate_jwt_button);
updateJwtButton = getActivity().findViewById(R.id.main_activity_update_jwt_button);

aliasTitleTextView = getActivity().findViewById(R.id.main_activity_aliases_title_text_view);
noAliasesTextView = getActivity().findViewById(R.id.main_activity_aliases_no_aliases_text_view);
addAliasButton = getActivity().findViewById(R.id.main_activity_add_alias_button);
Expand Down Expand Up @@ -403,7 +411,8 @@ private void setupAppLayout() {
@Override
public void onSuccess(String update) {
if (update != null && !update.isEmpty()) {
OneSignal.login(update);
String jwt = "InitialJWT";
OneSignal.login(update, jwt);
refreshState();
}
}
Expand All @@ -422,6 +431,7 @@ public void onFailure() {
}

private void setupUserLayout() {
setupJWTLayout();
setupAliasLayout();
setupEmailLayout();
setupSMSLayout();
Expand All @@ -430,6 +440,30 @@ private void setupUserLayout() {
setupTriggersLayout();
}

private void setupJWTLayout() {
invalidateJwtButton.setOnClickListener(v -> {
OneSignal.updateUserJwt(OneSignal.getUser().getExternalId(), "");
});
updateJwtButton.setOnClickListener(v -> {
dialog.createUpdateAlertDialog("", Dialog.DialogAction.UPDATE, ProfileUtil.FieldType.JWT, new UpdateAlertDialogCallback() {
@Override
public void onSuccess(String update) {
if (update != null && !update.isEmpty()) {
OneSignal.updateUserJwt(OneSignal.getUser().getExternalId(), update);
//String jwt = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIxNjg4ZDhmMi1kYTdmLTQ4MTUtOGVlMy05ZDEzNzg4NDgyYzgiLCJpYXQiOjE3MTQwODA4MTMsImlkZW50aXR5Ijp7ImV4dGVybmFsX2lkIjoiMjAyNDA0MjUtYWxleDQyIn0sInN1YnNjcmlwdGlvbiI6W3sidHlwZSI6IiIsImlkIjoiMmRlZGU3MzItMTEyNi00MTlkLTk5M2UtNDIzYWQyYTZiZGU5In1dfQ.rzZ-HaDm1EwxbMxd8937bWzPhPSQDDSqSzaASgZZc5A5v8g6ZQHizN9CljOmoQ4lTLm7noD6rOmR07tlZdrI5w";
//OneSignal.login(update, jwt);
refreshState();
}
}

@Override
public void onFailure() {

}
});
});
}

private void setupAliasLayout() {
setupAliasesRecyclerView();
addAliasButton.setOnClickListener(v -> dialog.createAddPairAlertDialog("Add Alias", ProfileUtil.FieldType.ALIAS, new AddPairAlertDialogCallback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum FieldType {
ALIAS("Alias"),
EMAIL("Email"),
SMS("SMS"),
JWT("JWT"),
EXTERNAL_USER_ID("External User Id"),

TAG("Tags"),
Expand Down Expand Up @@ -97,6 +98,10 @@ public static boolean isSMSValid(TextInputLayout smsTextInputLayout) {
return true;
}

private static boolean isJWTValid(TextInputLayout jwtTextInputLayout) {
return !jwtTextInputLayout.getEditText().toString().isEmpty();
}

private static boolean isExternalUserIdValid(TextInputLayout externalUserIdTextInputLayout) {
externalUserIdTextInputLayout.setErrorEnabled(false);
if (externalUserIdTextInputLayout.getEditText() != null) {
Expand Down Expand Up @@ -137,6 +142,8 @@ static boolean isContentValid(FieldType field, TextInputLayout alertDialogTextIn
return isEmailValid(alertDialogTextInputLayout);
case SMS:
return isSMSValid(alertDialogTextInputLayout);
case JWT:
return isJWTValid(alertDialogTextInputLayout);
case EXTERNAL_USER_ID:
return isExternalUserIdValid(alertDialogTextInputLayout);
case TAG:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static boolean exists(Context context, String key) {
}

public static String getOneSignalAppId(Context context) {
return getSharedPreference(context).getString(OS_APP_ID_SHARED_PREF, "77e32082-ea27-42e3-a898-c72e141824ef");
return getSharedPreference(context).getString(OS_APP_ID_SHARED_PREF, "0139bd6f-451f-438c-8886-4e0f0fe3a085");
}

public static boolean getUserPrivacyConsent(Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,20 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:gravity="start|center_vertical"
android:layout_marginBottom="4dp"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginBottom="4dp"
android:gravity="start|center_vertical"
android:text="@string/app"
android:textColor="@color/colorDarkText" />

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
app:cardCornerRadius="6dp"
app:cardElevation="4dp">

Expand Down Expand Up @@ -226,23 +226,23 @@
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginStart="12dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:orientation="vertical"
android:background="@color/colorPrimary">
android:background="@color/colorPrimary"
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/main_activity_app_revoke_consent_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ripple_selector_white_red"
android:text="@string/revoke_consent"
android:textSize="19sp"
android:textColor="@android:color/white"
android:background="@drawable/ripple_selector_white_red"
android:visibility="visible"/>
android:textSize="19sp"
android:visibility="visible" />

</LinearLayout>

Expand All @@ -263,11 +263,11 @@
android:id="@+id/main_activity_login_user_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ripple_selector_white_red"
android:text="@string/login_user"
android:textSize="19sp"
android:textColor="@android:color/white"
android:background="@drawable/ripple_selector_white_red"
android:visibility="visible"/>
android:textSize="19sp"
android:visibility="visible" />

</LinearLayout>

Expand All @@ -288,14 +288,97 @@
android:id="@+id/main_activity_logout_user_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ripple_selector_white_red"
android:text="@string/logout_user"
android:textSize="19sp"
android:textColor="@android:color/white"
android:background="@drawable/ripple_selector_white_red"
android:visibility="visible"/>
android:textSize="19sp"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>

<LinearLayout
android:id="@+id/main_activity_jwt_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical">

<TextView
android:id="@+id/main_activity_jwt_title_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginBottom="4dp"
android:gravity="start|center_vertical"
android:text="@string/JWT"
android:textColor="@color/colorDarkText" />

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
app:cardCornerRadius="6dp"
app:cardElevation="4dp">

<LinearLayout
android:id="@+id/main_activity_jwt_details_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/main_activity_invalidate_jwt_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ripple_selector_white_red"
android:text="@string/invalidate_JWT"
android:textColor="@android:color/white"
android:textSize="19sp"
android:visibility="visible" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/main_activity_update_jwt_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ripple_selector_white_red"
android:text="@string/update_JWT"
android:textColor="@android:color/white"
android:textSize="19sp"
android:visibility="visible" />
</LinearLayout>

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

<!-- Aliases -->
<LinearLayout
android:id="@+id/main_activity_aliases_linear_layout"
Expand Down
5 changes: 5 additions & 0 deletions Examples/OneSignalDemo/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<string name="login_user">Login User</string>
<string name="logout_user">Logout User</string>

<string name="JWT">JWT</string>
<string name="invalidate_JWT">Invalidate JWT</string>
<string name="update_JWT">Update JWT</string>
<string name="no_jwt_added">No JWT Added</string>

<string name="aliases">Aliases</string>
<string name="external_user_id_colon">EUID:</string>
<string name="no_aliases_added">No Aliases Added</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ interface IOneSignal {
*/
val isInitialized: Boolean

/**
* Whether the security feature to authenticate your external user ids is enabled
*/
val useIdentityVerification: Boolean

/**
* The user manager for accessing user-scoped
* management.
Expand Down Expand Up @@ -123,4 +128,16 @@ interface IOneSignal {
* data is not cleared.
*/
fun logout()

/**
* Update JWT token for a user
*/
fun updateUserJwt(
externalId: String,
token: String,
)

fun addUserJwtInvalidatedListener(listener: IUserJwtInvalidatedListener)

fun removeUserJwtInvalidatedListener(listener: IUserJwtInvalidatedListener)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.onesignal

/** TODO: complete the comment part for this listener
* Implement this interface and provide an instance to [OneSignal.addUserJwtInvalidatedListner]
* in order to receive control when the JWT for the current user is invalidated.
*
* @see [User JWT Invalidated Event | OneSignal Docs](https://documentation.onesignal.com/docs/)
*/
interface IUserJwtInvalidatedListener {
/**
* Called when the JWT is invalidated
*
* @param event The user JWT that expired.
*/
fun onUserJwtInvalidated(event: UserJwtInvalidatedEvent)
}
Loading