Skip to content

Commit 0ea2c7e

Browse files
chore: [SDK-4768] add no-location Capacitor demo (#30)
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 31a566d commit 0ea2c7e

85 files changed

Lines changed: 2104 additions & 7 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ Set `ONESIGNAL_DISABLE_LOCATION=true` in the environment before resolving or bui
4040
ONESIGNAL_DISABLE_LOCATION=true npx cap sync
4141
```
4242

43+
For day-to-day native builds and runs, keep the same environment variable set so Capacitor, Swift Package Manager, CocoaPods, and Gradle do not re-resolve with the location module included:
44+
45+
```bash
46+
ONESIGNAL_DISABLE_LOCATION=true npx cap run ios
47+
ONESIGNAL_DISABLE_LOCATION=true npx cap run android
48+
```
49+
4350
In GitHub Actions, set it once at the job or step level so Swift Package Manager, CocoaPods, and Gradle builds inherit it:
4451

4552
```yaml

android/src/main/kotlin/com/onesignal/capacitor/OneSignalCapacitorPlugin.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import com.onesignal.user.state.IUserStateObserver
2525
import com.onesignal.user.state.UserChangedState
2626
import com.onesignal.user.subscriptions.IPushSubscriptionObserver
2727
import com.onesignal.user.subscriptions.PushSubscriptionChangedState
28+
import kotlinx.coroutines.CancellationException
2829
import kotlinx.coroutines.MainScope
2930
import kotlinx.coroutines.cancel
3031
import kotlinx.coroutines.launch
@@ -45,8 +46,8 @@ class OneSignalCapacitorPlugin : Plugin(),
4546
// provisional (3), and ephemeral (4) states do not apply here.
4647
private const val PERMISSION_DENIED = 1
4748
private const val PERMISSION_AUTHORIZED = 2
48-
private const val LOCATION_MODULE_NOT_AVAILABLE =
49-
"OneSignal location module is not available. Add the location dependency to use OneSignal.Location."
49+
private const val LOCATION_CALL_FAILED =
50+
"OneSignal.Location call failed. The location module may not be included in this build."
5051
}
5152

5253
private val notificationWillDisplayCache = mutableMapOf<String, INotificationWillDisplayEvent>()
@@ -59,8 +60,8 @@ class OneSignalCapacitorPlugin : Plugin(),
5960
// call into the dead Capacitor bridge.
6061
private val pluginScope = MainScope()
6162

62-
private fun logLocationModuleNotAvailable(throwable: Throwable) {
63-
Logging.error(LOCATION_MODULE_NOT_AVAILABLE, throwable)
63+
private fun logLocationCallFailed(throwable: Throwable) {
64+
Logging.error(LOCATION_CALL_FAILED, throwable)
6465
}
6566

6667
private val permissionObserver = object : IPermissionObserver {
@@ -649,8 +650,10 @@ class OneSignalCapacitorPlugin : Plugin(),
649650
pluginScope.launch {
650651
try {
651652
OneSignal.Location.requestPermission()
653+
} catch (e: CancellationException) {
654+
throw e
652655
} catch (t: Throwable) {
653-
logLocationModuleNotAvailable(t)
656+
logLocationCallFailed(t)
654657
}
655658
call.resolve()
656659
}
@@ -662,7 +665,7 @@ class OneSignalCapacitorPlugin : Plugin(),
662665
try {
663666
OneSignal.Location.isShared = shared
664667
} catch (t: Throwable) {
665-
logLocationModuleNotAvailable(t)
668+
logLocationCallFailed(t)
666669
}
667670
call.resolve()
668671
}
@@ -673,7 +676,7 @@ class OneSignalCapacitorPlugin : Plugin(),
673676
try {
674677
ret.put("shared", OneSignal.Location.isShared)
675678
} catch (t: Throwable) {
676-
logLocationModuleNotAvailable(t)
679+
logLocationCallFailed(t)
677680
ret.put("shared", false)
678681
}
679682
call.resolve(ret)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VITE_ONESIGNAL_APP_ID=your-onesignal-app-id
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# OneSignal Capacitor No-Location Demo
2+
3+
This lightweight runnable example shows the native build flag for apps that use OneSignal push and in-app messaging, but do not use `OneSignal.Location`.
4+
5+
Run it with:
6+
7+
```sh
8+
vp run ios
9+
vp run android
10+
```
11+
12+
## Setup
13+
14+
Copy `.env.example` to `.env` and set your OneSignal app ID:
15+
16+
```sh
17+
cp .env.example .env
18+
```
19+
20+
Then edit `.env`:
21+
22+
```sh
23+
VITE_ONESIGNAL_APP_ID=your-onesignal-app-id
24+
```
25+
26+
The `setup` script exports `ONESIGNAL_DISABLE_LOCATION=true` before packing the local plugin and running Capacitor sync, so Android Gradle and iOS Swift Package Manager resolve OneSignal without the location module.
27+
28+
## iOS
29+
30+
The `ios` script runs `setup`, which syncs Capacitor with:
31+
32+
```sh
33+
ONESIGNAL_DISABLE_LOCATION=true
34+
```
35+
36+
If you run `vpx cap sync ios`, Xcode, or Swift Package Manager manually, set `ONESIGNAL_DISABLE_LOCATION=true` in that environment too.
37+
38+
## Android
39+
40+
The `android` script runs `setup`, which syncs Capacitor with:
41+
42+
```sh
43+
ONESIGNAL_DISABLE_LOCATION=true
44+
```
45+
46+
If you build Android another way, such as Android Studio or a raw `./gradlew` invocation, set `ONESIGNAL_DISABLE_LOCATION=true` in that environment too.
47+
48+
## App Code
49+
50+
`src/App.tsx` initializes OneSignal and requests notification permission without calling the `OneSignal.Location` namespace during normal app flow. The optional location test button calls `OneSignal.Location.requestPermission()` to confirm the no-location bridge resolves safely.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Using Android gitignore template: https://github.com/github/gitignore/blob/HEAD/Android.gitignore
2+
3+
# Built application files
4+
*.apk
5+
*.aar
6+
*.ap_
7+
*.aab
8+
9+
# Files for the ART/Dalvik VM
10+
*.dex
11+
12+
# Java class files
13+
*.class
14+
15+
# Generated files
16+
bin/
17+
gen/
18+
out/
19+
# Uncomment the following line in case you need and you don't have the release build type files in your app
20+
# release/
21+
22+
# Gradle files
23+
.gradle/
24+
build/
25+
26+
# Local configuration file (sdk path, etc)
27+
local.properties
28+
29+
# Proguard folder generated by Eclipse
30+
proguard/
31+
32+
# Log Files
33+
*.log
34+
35+
# Android Studio Navigation editor temp files
36+
.navigation/
37+
38+
# Android Studio captures folder
39+
captures/
40+
41+
# IntelliJ
42+
*.iml
43+
.idea/workspace.xml
44+
.idea/tasks.xml
45+
.idea/gradle.xml
46+
.idea/assetWizardSettings.xml
47+
.idea/dictionaries
48+
.idea/libraries
49+
# Android Studio 3 in .gitignore file.
50+
.idea/caches
51+
.idea/modules.xml
52+
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
53+
.idea/navEditor.xml
54+
55+
# Keystore files
56+
# Uncomment the following lines if you do not want to check your keystore files in.
57+
#*.jks
58+
#*.keystore
59+
60+
# External native build folder generated in Android Studio 2.2 and later
61+
.externalNativeBuild
62+
.cxx/
63+
64+
# Google Services (e.g. APIs or Firebase)
65+
# google-services.json
66+
67+
# Freeline
68+
freeline.py
69+
freeline/
70+
freeline_project_description.json
71+
72+
# fastlane
73+
fastlane/report.xml
74+
fastlane/Preview.html
75+
fastlane/screenshots
76+
fastlane/test_output
77+
fastlane/readme.md
78+
79+
# Version control
80+
vcs.xml
81+
82+
# lint
83+
lint/intermediates/
84+
lint/generated/
85+
lint/outputs/
86+
lint/tmp/
87+
# lint/reports/
88+
89+
# Android Profiling
90+
*.hprof
91+
92+
# Cordova plugins for Capacitor
93+
capacitor-cordova-android-plugins
94+
95+
# Copied web assets
96+
app/src/main/assets/public
97+
98+
# Generated Config files
99+
app/src/main/assets/capacitor.config.json
100+
app/src/main/assets/capacitor.plugins.json
101+
app/src/main/res/xml/config.xml
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/build/*
2+
!/build/.npmkeep
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
namespace = "com.onesignal.example"
5+
compileSdk = rootProject.ext.compileSdkVersion
6+
defaultConfig {
7+
applicationId "com.onesignal.example"
8+
minSdkVersion rootProject.ext.minSdkVersion
9+
targetSdkVersion rootProject.ext.targetSdkVersion
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
13+
aaptOptions {
14+
// Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.
15+
// Default: https://android.googlesource.com/platform/frameworks/base/+/282e181b58cf72b6ca770dc7ca5f91f135444502/tools/aapt/AaptAssets.cpp#61
16+
ignoreAssetsPattern = '!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~'
17+
}
18+
}
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
}
26+
27+
repositories {
28+
flatDir{
29+
dirs '../capacitor-cordova-android-plugins/src/main/libs', 'libs'
30+
}
31+
}
32+
33+
dependencies {
34+
implementation fileTree(include: ['*.jar'], dir: 'libs')
35+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
36+
implementation "androidx.coordinatorlayout:coordinatorlayout:$androidxCoordinatorLayoutVersion"
37+
implementation "androidx.core:core-splashscreen:$coreSplashScreenVersion"
38+
implementation project(':capacitor-android')
39+
testImplementation "junit:junit:$junitVersion"
40+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
41+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
42+
implementation project(':capacitor-cordova-android-plugins')
43+
}
44+
45+
apply from: 'capacitor.build.gradle'
46+
47+
try {
48+
def servicesJSON = file('google-services.json')
49+
if (servicesJSON.text) {
50+
apply plugin: 'com.google.gms.google-services'
51+
}
52+
} catch(Exception e) {
53+
logger.info("google-services.json not found, google-services plugin not applied. Push Notifications won't work")
54+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<application
5+
android:allowBackup="true"
6+
android:icon="@mipmap/ic_launcher"
7+
android:label="@string/app_name"
8+
android:roundIcon="@mipmap/ic_launcher_round"
9+
android:supportsRtl="true"
10+
android:theme="@style/AppTheme">
11+
12+
<activity
13+
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode|navigation|density"
14+
android:name=".MainActivity"
15+
android:label="@string/title_activity_main"
16+
android:theme="@style/AppTheme.NoActionBarLaunch"
17+
android:launchMode="singleTask"
18+
android:exported="true">
19+
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
<category android:name="android.intent.category.LAUNCHER" />
23+
</intent-filter>
24+
25+
</activity>
26+
27+
<provider
28+
android:name="androidx.core.content.FileProvider"
29+
android:authorities="${applicationId}.fileprovider"
30+
android:exported="false"
31+
android:grantUriPermissions="true">
32+
<meta-data
33+
android:name="android.support.FILE_PROVIDER_PATHS"
34+
android:resource="@xml/file_paths"></meta-data>
35+
</provider>
36+
</application>
37+
38+
<!-- Permissions -->
39+
40+
<uses-permission android:name="android.permission.INTERNET" />
41+
</manifest>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.onesignal.example;
2+
3+
import com.getcapacitor.BridgeActivity;
4+
5+
public class MainActivity extends BridgeActivity {}

0 commit comments

Comments
 (0)