Skip to content
Open
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
10 changes: 6 additions & 4 deletions .github/workflows/create-release-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ jobs:

# Get versions from target branch (not the release branch)
CURRENT_VERSION=$(git show origin/${{ inputs.target_branch }}:examples/demo/Assets/OneSignal/VERSION | tr -d '\n\r' | xargs)
ANDROID_VERSION=$(git show origin/${{ inputs.target_branch }}:com.onesignal.unity.android/Editor/OneSignalAndroidDependencies.xml | grep -oE 'spec="com.onesignal:OneSignal:[0-9]+\.[0-9]+\.[0-9]+"' | sed -E 's/.*OneSignal:([0-9]+\.[0-9]+\.[0-9]+)".*/\1/' | head -1)
IOS_VERSION=$(git show origin/${{ inputs.target_branch }}:com.onesignal.unity.ios/Editor/OneSignaliOSDependencies.xml | grep -oE 'version="[0-9.]+"' | grep -oE '[0-9.]+' | head -1)
ANDROID_VERSION=$(git show origin/${{ inputs.target_branch }}:com.onesignal.unity.android/Editor/OneSignalAndroidDependencies.cs | grep -oE 'Version = "[0-9]+\.[0-9]+\.[0-9]+"' | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
IOS_VERSION=$(git show origin/${{ inputs.target_branch }}:com.onesignal.unity.ios/Editor/OneSignaliOSDependencies.cs | grep -oE 'Version = "[0-9]+\.[0-9]+\.[0-9]+"' | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
echo "unity_from=${CURRENT_VERSION}" >> $GITHUB_OUTPUT
echo "android_from=${ANDROID_VERSION}" >> $GITHUB_OUTPUT
echo "ios_from=${IOS_VERSION}" >> $GITHUB_OUTPUT
Expand All @@ -109,7 +109,8 @@ jobs:
sleep 30
done
if [ "$FOUND" != "true" ]; then echo "✗ Android SDK version ${VERSION} not found after 5 attempts"; exit 1; fi
sed -i -E "s/spec=\"com\.onesignal:OneSignal:[0-9][0-9.]*\"/spec=\"com.onesignal:OneSignal:$VERSION\"/" com.onesignal.unity.android/Editor/OneSignalAndroidDependencies.xml
sed -i -E "s/const string Version = \"[0-9][0-9.]*\"/const string Version = \"$VERSION\"/" com.onesignal.unity.android/Editor/OneSignalAndroidDependencies.cs
sed -i -E "s/<androidPackage spec=\"com\.onesignal:OneSignal:[0-9][0-9.]*\"/<androidPackage spec=\"com.onesignal:OneSignal:$VERSION\"/" examples/demo/Assets/OneSignal/Editor/OneSignalAndroidDependencies.xml
sed -i -E "s/'com\.onesignal:OneSignal:[0-9][0-9.]*'/'com.onesignal:OneSignal:$VERSION'/" examples/demo/Assets/Plugins/Android/mainTemplate.gradle
sed -i -E "s/<package>com\.onesignal:OneSignal:[0-9][0-9.]*<\/package>/<package>com.onesignal:OneSignal:$VERSION<\/package>/" examples/demo/ProjectSettings/AndroidResolverDependencies.xml

Expand All @@ -132,7 +133,8 @@ jobs:
sleep 30
done
if [ "$FOUND" != "true" ]; then echo "✗ iOS SDK version ${VERSION} not found after 5 attempts"; exit 1; fi
sed -i -E "s/version=\"[0-9][0-9.]*\"/version=\"$VERSION\"/" com.onesignal.unity.ios/Editor/OneSignaliOSDependencies.xml
sed -i -E "s/const string Version = \"[0-9][0-9.]*\"/const string Version = \"$VERSION\"/" com.onesignal.unity.ios/Editor/OneSignaliOSDependencies.cs
sed -i -E "s/version=\"[0-9][0-9.]*\"/version=\"$VERSION\"/" examples/demo/Assets/OneSignal/Editor/OneSignaliOSDependencies.xml

# Only commit if there are changes
git add -A
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ jobs:
- name: Resolve OneSignal Android SDK version
id: android-sdk-version
run: |
VERSION=$(grep -oE 'com\.onesignal:OneSignal:[^"]+' com.onesignal.unity.android/Editor/OneSignalAndroidDependencies.xml | head -n1 | cut -d: -f3)
VERSION=$(grep -oE 'Version = "[0-9]+\.[0-9]+\.[0-9]+"' com.onesignal.unity.android/Editor/OneSignalAndroidDependencies.cs | head -n1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')
if [ -z "$VERSION" ]; then
echo "::error::Could not parse OneSignal Android SDK version from OneSignalAndroidDependencies.xml"
echo "::error::Could not parse OneSignal Android SDK version from OneSignalAndroidDependencies.cs"
exit 1
fi
echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,23 @@ The only thing remaining is to setup your own notification icons. You can do thi
with your own. There is a complete guide for this [in the plugin's README](com.onesignal.unity.android/Editor/OneSignalConfig.androidlib/README.md). See our
[Customize Notification Icons](https://documentation.onesignal.com/docs/customize-notification-icons) page for additional details.

### Disable Location Module

By default, the OneSignal Unity SDK includes OneSignal's native location module so `OneSignal.Location` works without extra setup. If your app does not use location features, you can exclude the native location module from iOS and Android builds. There are two ways to opt out:

- **Editor toggle** (interactive): select **OneSignal > Disable Location Module** in the Unity Editor before resolving Android dependencies or building iOS. This is persisted per project in `ProjectSettings/OneSignalSettings.json`.
- **Environment variable** (CLI/CI): set `ONESIGNAL_DISABLE_LOCATION=true` (or `1`) in the environment before launching Unity, for example:

```sh
ONESIGNAL_DISABLE_LOCATION=true /path/to/Unity -batchmode -quit -projectPath . -buildTarget iOS -executeMethod BuildScript.BuildiOSSimulator
```

When set, the environment variable takes precedence over the Editor toggle, so headless builds can opt out without changing project settings.

With the location module disabled, Android resolves OneSignal's native modules without the location artifact and iOS uses OneSignal pods without `OneSignalLocation`. `OneSignal.Location.RequestPermission()` and `OneSignal.Location.IsShared = ...` no-op on native builds without the location module, and `OneSignal.Location.IsShared` returns `false`.

When toggling the flag, clear stale native outputs (the generated Xcode project, CocoaPods/Gradle caches, and prior `Build/` artifacts) so a previously linked location module isn't reused.

## Usage
You can find a complete implementation in our included [example MonoBehaviour](OneSignalExample/Assets/OneSignal/Example/OneSignalExampleBehaviour.cs). Additionally we have included a
[sample scene](OneSignalExample/Assets/OneSignal/Example/OneSignalExampleScene.unity) which you can run to test out the SDK.
Expand Down
103 changes: 103 additions & 0 deletions com.onesignal.unity.android/Editor/OneSignalAndroidDependencies.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Modified MIT License
*
* Copyright 2023 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.IO;
using UnityEditor;
using UnityDebug = UnityEngine.Debug;

namespace OneSignalSDK
{
[InitializeOnLoad]
internal static class OneSignalAndroidDependencies
{
static OneSignalAndroidDependencies()
{
OneSignalSDKSettings.Changed += WriteDependencies;
WriteDependencies();
}

internal static void WriteDependencies()
{
var contents = OneSignalSDKSettings.EffectiveDisableLocation
? DisabledLocationDependencies
: DefaultDependencies;

try
{
if (
File.Exists(_dependenciesPath)
&& File.ReadAllText(_dependenciesPath) == contents
)
return;

Directory.CreateDirectory(Path.GetDirectoryName(_dependenciesPath));
File.WriteAllText(_dependenciesPath, contents);
AssetDatabase.ImportAsset(_dependenciesPath);
}
catch (Exception exception)
{
UnityDebug.LogWarning(
$"Could not update OneSignal Android dependencies at {_dependenciesPath}: {exception.Message}"
);
}
}

internal const string Version = "5.9.5";

private static readonly string _dependenciesPath = Path.Combine(
"Assets",
"OneSignal",
"Editor",
"OneSignalAndroidDependencies.xml"
);

private static string DefaultDependencies =>
$@"<dependencies>
<androidPackages>
<repositories>
<repository>https://repo.maven.apache.org/maven2</repository>
</repositories>
<androidPackage spec=""com.onesignal:OneSignal:{Version}"" />
</androidPackages>
</dependencies>
";

private static string DisabledLocationDependencies =>
$@"<dependencies>
<androidPackages>
<repositories>
<repository>https://repo.maven.apache.org/maven2</repository>
</repositories>
<androidPackage spec=""com.onesignal:core:{Version}"" />
<androidPackage spec=""com.onesignal:notifications:{Version}"" />
<androidPackage spec=""com.onesignal:in-app-messages:{Version}"" />
</androidPackages>
</dependencies>
";
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 46 additions & 6 deletions com.onesignal.unity.android/Runtime/AndroidLocationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,72 @@
* THE SOFTWARE.
*/

using System.Threading.Tasks;
using System;
using OneSignalSDK.Android.Utilities;
using OneSignalSDK.Debug.Utilities;
using OneSignalSDK.Location;
using UnityEngine;

namespace OneSignalSDK.Android.Location
{
internal sealed class AndroidLocationManager : ILocationManager
{
private const string LocationModuleNotAvailable =
"OneSignal location module is not available. Add the location dependency to use OneSignal.Location.";

private readonly AndroidJavaObject _location;

public AndroidLocationManager(AndroidJavaClass sdkClass)
{
_location = sdkClass.CallStatic<AndroidJavaObject>("getLocation");
try
{
_location = sdkClass.CallStatic<AndroidJavaObject>("getLocation");
}
catch (Exception)
{
SDKDebug.Error(LocationModuleNotAvailable);
}
}

public bool IsShared
{
get => _location.Call<bool>("isShared");
set => _location.Call("setShared", value);
get
{
try
{
return _location != null && _location.Call<bool>("isShared");
}
catch (Exception)
{
SDKDebug.Error(LocationModuleNotAvailable);
return false;
}
}
set
{
try
{
if (_location != null)
_location.Call("setShared", value);
}
catch (Exception)
{
SDKDebug.Error(LocationModuleNotAvailable);
}
}
}

public void RequestPermission()
{
var continuation = new BoolContinuation();
_location.Call<AndroidJavaObject>("requestPermission", continuation.Proxy);
try
{
var continuation = new BoolContinuation();
_location.Call<AndroidJavaObject>("requestPermission", continuation.Proxy);
}
catch (Exception)
{
SDKDebug.Error(LocationModuleNotAvailable);
}
}
}
}
125 changes: 125 additions & 0 deletions com.onesignal.unity.core/Editor/OneSignalSDKSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Modified MIT License
*
* Copyright 2023 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace OneSignalSDK
{
public static class OneSignalSDKSettings
{
public const string DisableLocationEnvVar = "ONESIGNAL_DISABLE_LOCATION";

public static event Action Changed;

public static bool DisableLocation
{
get => _settings.disableLocation;
set
{
if (_settings.disableLocation == value)
return;

_settings.disableLocation = value;
Save();
Changed?.Invoke();
AssetDatabase.Refresh();
}
}

/// <summary>
/// Resolved value used by the build pipeline. The <see cref="DisableLocationEnvVar"/>
/// environment variable, when present, overrides the persisted Editor setting so CLI
/// and CI builds can opt out without mutating project settings.
/// </summary>
public static bool EffectiveDisableLocation
{
get
{
var environmentValue = Environment.GetEnvironmentVariable(DisableLocationEnvVar);
if (!string.IsNullOrEmpty(environmentValue))
{
var normalized = environmentValue.Trim();
return normalized.Equals("true", StringComparison.OrdinalIgnoreCase)
|| normalized == "1";
}

return _settings.disableLocation;
}
}

public static void Save()
{
Directory.CreateDirectory(Path.GetDirectoryName(_settingsPath));
File.WriteAllText(_settingsPath, JsonUtility.ToJson(_settings, true));
}

[MenuItem("OneSignal/Disable Location Module")]
private static void ToggleDisableLocation()
{
DisableLocation = !DisableLocation;
}

[MenuItem("OneSignal/Disable Location Module", true)]
private static bool ToggleDisableLocationValidate()
{
Menu.SetChecked("OneSignal/Disable Location Module", DisableLocation);
return true;
}

private static readonly string _settingsPath = Path.Combine(
"ProjectSettings",
"OneSignalSettings.json"
);

private static Settings _settings = Load();

private static Settings Load()
{
if (!File.Exists(_settingsPath))
return new Settings();

try
{
return JsonUtility.FromJson<Settings>(File.ReadAllText(_settingsPath))
?? new Settings();
}
catch
{
return new Settings();
}
}

[Serializable]
private sealed class Settings
{
public bool disableLocation;
}
}
}
11 changes: 11 additions & 0 deletions com.onesignal.unity.core/Editor/OneSignalSDKSettings.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading