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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ If you run into any challenges or have concerns, please contact our support team

See the [Setup Guide](https://documentation.onesignal.com/docs/react-native-sdk-setup) for setup instructions.

#### Disable Location Module

By default, `react-native-onesignal` 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.

In your iOS `Podfile`, set this before React Native installs native modules:

```ruby
$OneSignalDisableLocation = true
```

In your Android `gradle.properties`, set:

```properties
OneSignal_disableLocation=true
```

When disabled, `OneSignal.Location.requestPermission()` and `OneSignal.Location.setShared()` no-op on native builds without the location module, and `OneSignal.Location.isShared()` resolves `false`.

#### Change Log

See this repository's [release tags](https://github.com/OneSignal/react-native-onesignal/releases) for a complete change log of every released version.
Expand Down
28 changes: 26 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

def safePropGet(prop, fallback) {
if (rootProject.ext.has(prop)) {
return rootProject.ext.get(prop)
}

def value = rootProject.findProperty(prop)
return value != null ? value : fallback
}

def oneSignalVersion = '5.9.3'
def oneSignalDisableLocation = safePropGet('OneSignal_disableLocation', false).toString().toBoolean()

android {
namespace "com.onesignal.rnonesignalandroid"
compileSdkVersion safeExtGet('compileSdkVersion', 34)
Expand Down Expand Up @@ -33,8 +45,20 @@ dependencies {
// Exclude OkHttp from OneSignal's transitive deps: the otel module pulls in OkHttp 5.x
// (via opentelemetry-exporter-sender-okhttp) which is binary-incompatible with React Native's
// networking stack (okhttp3.internal.Util removed in 5.x). React Native already provides OkHttp 4.x.
api('com.onesignal:OneSignal:5.9.3') {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
if (oneSignalDisableLocation) {
api("com.onesignal:core:${oneSignalVersion}") {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}
api("com.onesignal:notifications:${oneSignalVersion}") {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}
api("com.onesignal:in-app-messages:${oneSignalVersion}") {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}
} else {
api("com.onesignal:OneSignal:${oneSignalVersion}") {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}
}

testImplementation 'junit:junit:4.12'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public class RNOneSignal extends NativeOneSignalSpec
LifecycleEventListener,
INotificationLifecycleListener {
public static final String NAME = "OneSignal";
private static final String LOCATION_MODULE_NOT_AVAILABLE =
"OneSignal location module is not available. Add the location dependency to use OneSignal.Location.";

private boolean oneSignalInitDone;
private boolean hasSetPermissionObserver = false;
Expand Down Expand Up @@ -166,6 +168,10 @@ private void logJSONException(String eventName, JSONException exception) {
Logging.error("Failed to serialize payload for " + eventName, exception);
}

private void logLocationModuleNotAvailable(Exception exception) {
Logging.error(LOCATION_MODULE_NOT_AVAILABLE, exception);
}

private void removeObservers() {
if (!oneSignalInitDone) {
Logging.debug("OneSignal React-Native SDK not initialized yet. Could not remove observers.", null);
Expand Down Expand Up @@ -322,17 +328,30 @@ public void clearTriggers() {

@Override
public void requestLocationPermission() {
OneSignal.getLocation().requestPermission(Continue.none());
try {
OneSignal.getLocation().requestPermission(Continue.none());
} catch (Exception e) {
logLocationModuleNotAvailable(e);
}
}

@Override
public void isLocationShared(Promise promise) {
promise.resolve(OneSignal.getLocation().isShared());
try {
promise.resolve(OneSignal.getLocation().isShared());
} catch (Exception e) {
logLocationModuleNotAvailable(e);
promise.resolve(false);
}
}

@Override
public void setLocationShared(boolean shared) {
OneSignal.getLocation().setShared(shared);
try {
OneSignal.getLocation().setShared(shared);
} catch (Exception e) {
logLocationModuleNotAvailable(e);
}
}

@Override
Expand Down
1 change: 1 addition & 0 deletions examples/demo-no-location/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ONESIGNAL_APP_ID=your-onesignal-app-id
79 changes: 79 additions & 0 deletions examples/demo-no-location/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Environment
.env
.rn-demo-env.stamp

# OSX
#
.DS_Store

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
**/.xcode.env.local

# Android/IntelliJ
#
build/
.idea
.gradle
local.properties
*.iml
*.hprof
.cxx/
*.keystore
!debug.keystore
.kotlin/

# node.js
#
node_modules/
npm-debug.log
yarn-error.log

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/

**/fastlane/report.xml
**/fastlane/Preview.html
**/fastlane/screenshots
**/fastlane/test_output

# Bundle artifact
*.jsbundle

# Ruby / CocoaPods
**/Pods/
/vendor/bundle/

# Temporary files created by Metro to check the health of the file watcher
.metro-health-check*

# testing
/coverage

# Yarn
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
1 change: 1 addition & 0 deletions examples/demo-no-location/.watchmanconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Loading
Loading