Skip to content
Draft
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 @@ -70,7 +70,7 @@ internal static void WriteDependencies()
}
}

internal const string Version = "5.10.2";
internal const string Version = "5.11.0";

private static readonly string _dependenciesPath = Path.Combine(
"Assets",
Expand Down
64 changes: 64 additions & 0 deletions com.onesignal.unity.android/Runtime/OneSignalAndroid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@
* THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OneSignalSDK.Android.Debug;
using OneSignalSDK.Android.InAppMessages;
using OneSignalSDK.Android.LiveActivities;
using OneSignalSDK.Android.Location;
using OneSignalSDK.Android.Notifications;
using OneSignalSDK.Android.Session;
using OneSignalSDK.Android.User;
using OneSignalSDK.Android.Utilities;
using OneSignalSDK.Debug;
using OneSignalSDK.Debug.Utilities;
using OneSignalSDK.InAppMessages;
Expand Down Expand Up @@ -206,6 +209,67 @@ public override void Login(string externalId, string jwtBearerToken = null)
_sdkClass.CallStatic("login", externalId, jwtBearerToken);
}

public override Task<LoginResult> LoginWithProfileAsync(
string externalId,
OneSignalUserProfile profile,
string jwtBearerToken = null
)
{
var invalid = LoginResult.ValidateInputs(externalId, profile);
if (invalid != null)
return Task.FromResult(invalid);

LoginResultContinuation continuation;
try
{
continuation = new LoginResultContinuation();
}
catch (Exception e)
{
SDKDebug.Error("loginWithProfile failed: " + e.Message);
return Task.FromResult(LoginResult.Failure("UNKNOWN", e.Message));
}

try
{
// Resolved before the call so nothing after it can fail and mask a native result.
using var resultClass = new AndroidJavaClass(SDKPackage + ".OneSignalResult");
using var tags = (profile.Tags ?? new Dictionary<string, string>()).ToMap();
using var aliases = (profile.Aliases ?? new Dictionary<string, string>()).ToMap();
using var profileClass = new AndroidJavaClass(SDKPackage + ".OneSignalUserProfile");
using var builder = profileClass.CallStatic<AndroidJavaObject>("builder");
if (profile.Email != null)
builder.Call<AndroidJavaObject>("setEmail", profile.Email)?.Dispose();
if (profile.Sms != null)
builder.Call<AndroidJavaObject>("setSms", profile.Sms)?.Dispose();
builder.Call<AndroidJavaObject>("setTags", tags)?.Dispose();
builder.Call<AndroidJavaObject>("setAliases", aliases)?.Dispose();
using var nativeProfile = builder.Call<AndroidJavaObject>("build");
using var proxy = continuation.CreateProxy();
using var returned = _sdkClass.CallStatic<AndroidJavaObject>(
"login",
externalId,
nativeProfile,
jwtBearerToken,
proxy
);

// A suspend call returns either COROUTINE_SUSPENDED or its result. Testing for the
// result type survives R8 renaming of the kotlin stdlib marker class.
if (
returned != null
&& AndroidJNI.IsInstanceOf(returned.GetRawObject(), resultClass.GetRawClass())
)
continuation.Resolve(returned);
}
catch (Exception e)
{
continuation.Fail(e.Message);
}

return continuation.Result;
}

public override void Logout()
{
_sdkClass.CallStatic("logout");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Laters;
using OneSignalSDK.Debug.Utilities;
using OneSignalSDK.User;
using UnityEngine;

namespace OneSignalSDK.Android.Utilities
Expand Down Expand Up @@ -66,6 +68,74 @@ public void accept(AndroidJavaObject obj)
}
}

/// <summary>
/// Completes with the native OneSignalResult, whether it arrives through the continuation or
/// is returned synchronously by the suspend call. Completes exactly once and never throws.
/// </summary>
internal sealed class LoginResultContinuation : AndroidJavaProxy
{
private readonly TaskCompletionSource<LoginResult> _completion =
new TaskCompletionSource<LoginResult>(
TaskCreationOptions.RunContinuationsAsynchronously
);

public Task<LoginResult> Result => _completion.Task;

public LoginResultContinuation()
: base("java.util.function.Consumer") { }

public AndroidJavaObject CreateProxy()
{
using var continuation = new AndroidJavaClass("com.onesignal.Continue");
return continuation.CallStatic<AndroidJavaObject>("with", this);
}

public void accept(AndroidJavaObject continueResult)
{
try
{
if (continueResult.Call<bool>("isSuccess"))
{
using var result = continueResult.Call<AndroidJavaObject>("getData");
Resolve(result);
}
else
{
using var throwable = continueResult.Call<AndroidJavaObject>("getThrowable");
Fail(
throwable?.Call<string>("getMessage")
?? throwable?.Call<string>("toString")
?? "loginWithProfile failed"
);
}
}
catch (Exception e)
{
Fail(e.Message);
}
}

public void Resolve(AndroidJavaObject oneSignalResult)
{
try
{
using var map = oneSignalResult.Call<AndroidJavaObject>("toMap");
using var json = new AndroidJavaObject("org.json.JSONObject", map);
_completion.TrySetResult(LoginResult.FromJson(json.Call<string>("toString")));
}
catch (Exception e)
{
Fail(e.Message);
}
}

public void Fail(string message)
{
if (_completion.TrySetResult(LoginResult.Failure("UNKNOWN", message)))
SDKDebug.Error("loginWithProfile failed: " + message);
}
}

public sealed class BoolContinuation
{
public AndroidJavaObject Proxy { get; }
Expand Down
6 changes: 6 additions & 0 deletions com.onesignal.unity.core/Editor/Platform/OneSignalNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ public override void Initialize(string appId)

public override void Login(string externalId, string jwtBearerToken = null) { }

public override Task<LoginResult> LoginWithProfileAsync(
string externalId,
OneSignalUserProfile profile,
string jwtBearerToken = null
) => Task.FromResult(LoginResult.Unsupported());

public override void Logout() { }
}
}
14 changes: 14 additions & 0 deletions com.onesignal.unity.core/Runtime/OneSignal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/

using System.Collections.Generic;
using System.Threading.Tasks;
using OneSignalSDK.Debug;
using OneSignalSDK.InAppMessages;
using OneSignalSDK.LiveActivities;
Expand Down Expand Up @@ -175,6 +176,19 @@ public static void Login(string externalId, string jwtBearerToken = null)
OneSignal.Default.Login(externalId, jwtBearerToken);
}

/// <summary>
/// Logs in [externalId] and applies [profile] in one operation. Android only for now:
/// on other platforms the task completes with a failed result and the native SDK is not called.
/// </summary>
public static Task<LoginResult> LoginWithProfileAsync(
string externalId,
OneSignalUserProfile profile,
string jwtBearerToken = null
)
{
return OneSignal.Default.LoginWithProfileAsync(externalId, profile, jwtBearerToken);
}

/// <summary>
/// Logout the user previously logged in via [login]. The [user] property now references
/// a new device-scoped user. A device-scoped user has no user identity that can later
Expand Down
11 changes: 11 additions & 0 deletions com.onesignal.unity.core/Runtime/OneSignalPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using OneSignalSDK.Debug;
using OneSignalSDK.InAppMessages;
using OneSignalSDK.LiveActivities;
Expand Down Expand Up @@ -147,6 +148,16 @@ protected static void _init(string appId)
/// </param>
public abstract void Login(string externalId, string jwtBearerToken = null);

/// <summary>
/// Logs in [externalId] and applies [profile] in one operation. Android only for now:
/// on other platforms the task completes with a failed result and the native SDK is not called.
/// </summary>
public abstract Task<LoginResult> LoginWithProfileAsync(
string externalId,
OneSignalUserProfile profile,
string jwtBearerToken = null
);

/// <summary>
/// Logout the user previously logged in via [login]. The [user] property now references
/// a new device-scoped user. A device-scoped user has no user identity that can later
Expand Down
Loading
Loading