diff --git a/.github/actions/create-demo-env/action.yml b/.github/actions/create-demo-env/action.yml index adc9a6a9..143c7920 100644 --- a/.github/actions/create-demo-env/action.yml +++ b/.github/actions/create-demo-env/action.yml @@ -14,6 +14,10 @@ runs: working-directory: examples/demo shell: bash run: | + if [ -z "${{ inputs.onesignal-app-id }}" ]; then + echo "::error::onesignal-app-id is empty; the demo app throws at startup without it" + exit 1 + fi { echo "ONESIGNAL_APP_ID=${{ inputs.onesignal-app-id }}" echo "ONESIGNAL_API_KEY=${{ inputs.onesignal-api-key }}" diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 47f966da..739b8dad 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -48,9 +48,15 @@ jobs: onesignal-app-id: ${{ vars.APPIUM_ONESIGNAL_APP_ID }} onesignal-api-key: ${{ secrets.APPIUM_ONESIGNAL_API_KEY }} + - name: Write demo google-services.json + uses: OneSignal/sdk-shared/.github/actions/setup-google-services@main + with: + google-services-json: ${{ secrets.APPIUM_GOOGLE_SERVICES_JSON }} + path: examples/demo/android/app/google-services.json + - name: Build release APK working-directory: examples/demo - run: flutter build apk --release + run: flutter build apk --release -PFIREBASE_INSTALLATION_ID_ENABLED=true - name: Upload APK uses: actions/upload-artifact@v7 @@ -138,6 +144,7 @@ jobs: platform: android app-artifact: demo-apk app-filename: app-release.apk + bundle-id: com.onesignal.sdkdemo sdk-type: flutter build-name: flutter-android-${{ github.ref_name }}-${{ github.run_number }} diff --git a/examples/build.md b/examples/build.md index 30de8e93..c84edad7 100644 --- a/examples/build.md +++ b/examples/build.md @@ -65,7 +65,7 @@ flutter_launcher_icons: ### Environment variables - `flutter_dotenv` loads `.env` from the `pubspec.yaml` `assets:` block (`.env` is listed alongside `assets/onesignal_logo.svg`). -- `ONESIGNAL_APP_ID` -- OneSignal app id. Falls back to the hard-coded `_defaultAppId` in `main.dart` when unset or empty. +- `ONESIGNAL_APP_ID` -- OneSignal app id. Required; `main.dart` throws a `StateError` when unset or empty. - `ONESIGNAL_API_KEY` -- REST API key used by `OneSignalApiService` for Live Activity update/end and notification sends. - `ONESIGNAL_ANDROID_CHANNEL_ID` -- optional Android notification channel id used by the REST send paths. @@ -125,7 +125,7 @@ OneSignal.Location.setShared(prefs.locationShared); - IAM: `addWillDisplayListener`, `addDidDisplayListener`, `addWillDismissListener`, `addDidDismissListener`, `addClickListener` - Notifications: `addClickListener`, `addForegroundWillDisplayListener` (calls `event.notification.display()`) - `TooltipHelper().init()` fetches tooltip content in the background. -- `appId` falls back to a hard-coded `_defaultAppId` constant when `ONESIGNAL_APP_ID` is not set in `.env`. +- `appId` comes from `ONESIGNAL_APP_ID` in `.env`; there is no default. `PreferencesService` is the source of truth for restored UI state. In `AppViewModel.loadInitialState(appId)`, read UI state from `_prefs` (not from the SDK): ```dart diff --git a/examples/demo-no-location-pods/android/app/build.gradle.kts b/examples/demo-no-location-pods/android/app/build.gradle.kts index 386a0a3e..e8600850 100644 --- a/examples/demo-no-location-pods/android/app/build.gradle.kts +++ b/examples/demo-no-location-pods/android/app/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } android { - namespace = "com.onesignal.example" + namespace = "com.onesignal.sdkdemo" compileSdk = flutter.compileSdkVersion ndkVersion = flutter.ndkVersion @@ -15,7 +15,7 @@ android { } defaultConfig { - applicationId = "com.onesignal.example" + applicationId = "com.onesignal.sdkdemo" // You can update the following values to match your application needs. // For more information, see: https://flutter.dev/to/review-gradle-config. minSdk = flutter.minSdkVersion diff --git a/examples/demo-no-location-pods/android/app/src/main/kotlin/com/onesignal/example/MainActivity.kt b/examples/demo-no-location-pods/android/app/src/main/kotlin/com/onesignal/sdkdemo/MainActivity.kt similarity index 75% rename from examples/demo-no-location-pods/android/app/src/main/kotlin/com/onesignal/example/MainActivity.kt rename to examples/demo-no-location-pods/android/app/src/main/kotlin/com/onesignal/sdkdemo/MainActivity.kt index df66b4f0..86870d73 100644 --- a/examples/demo-no-location-pods/android/app/src/main/kotlin/com/onesignal/example/MainActivity.kt +++ b/examples/demo-no-location-pods/android/app/src/main/kotlin/com/onesignal/sdkdemo/MainActivity.kt @@ -1,4 +1,4 @@ -package com.onesignal.example +package com.onesignal.sdkdemo import io.flutter.embedding.android.FlutterActivity diff --git a/examples/demo-no-location/android/app/build.gradle.kts b/examples/demo-no-location/android/app/build.gradle.kts index 386a0a3e..e8600850 100644 --- a/examples/demo-no-location/android/app/build.gradle.kts +++ b/examples/demo-no-location/android/app/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } android { - namespace = "com.onesignal.example" + namespace = "com.onesignal.sdkdemo" compileSdk = flutter.compileSdkVersion ndkVersion = flutter.ndkVersion @@ -15,7 +15,7 @@ android { } defaultConfig { - applicationId = "com.onesignal.example" + applicationId = "com.onesignal.sdkdemo" // You can update the following values to match your application needs. // For more information, see: https://flutter.dev/to/review-gradle-config. minSdk = flutter.minSdkVersion diff --git a/examples/demo-no-location/android/app/src/main/kotlin/com/onesignal/example/MainActivity.kt b/examples/demo-no-location/android/app/src/main/kotlin/com/onesignal/sdkdemo/MainActivity.kt similarity index 75% rename from examples/demo-no-location/android/app/src/main/kotlin/com/onesignal/example/MainActivity.kt rename to examples/demo-no-location/android/app/src/main/kotlin/com/onesignal/sdkdemo/MainActivity.kt index df66b4f0..86870d73 100644 --- a/examples/demo-no-location/android/app/src/main/kotlin/com/onesignal/example/MainActivity.kt +++ b/examples/demo-no-location/android/app/src/main/kotlin/com/onesignal/sdkdemo/MainActivity.kt @@ -1,4 +1,4 @@ -package com.onesignal.example +package com.onesignal.sdkdemo import io.flutter.embedding.android.FlutterActivity diff --git a/examples/demo-pods/.env.example b/examples/demo-pods/.env.example index 1edf9e67..d789fef7 100644 --- a/examples/demo-pods/.env.example +++ b/examples/demo-pods/.env.example @@ -1,4 +1,4 @@ -ONESIGNAL_APP_ID=your-onesignal-app-id # default app id: 77e32082-ea27-42e3-a898-c72e141824ef if empty +ONESIGNAL_APP_ID=your-onesignal-app-id ONESIGNAL_API_KEY=your-onesignal-api-key # Optional: Android Notification Channel ID for the WITH SOUND test notification. diff --git a/examples/demo-pods/README.md b/examples/demo-pods/README.md index 08675401..02f3dc06 100644 --- a/examples/demo-pods/README.md +++ b/examples/demo-pods/README.md @@ -46,7 +46,7 @@ ONESIGNAL_APP_ID=your-onesignal-app-id ONESIGNAL_API_KEY=your-onesignal-api-key ``` -The `.env` file must exist before the first build because Flutter bundles it as an app asset. If `ONESIGNAL_APP_ID` is empty, the app falls back to a built-in default App ID. +The `.env` file must exist before the first build because Flutter bundles it as an app asset. The app throws at startup when `ONESIGNAL_APP_ID` is empty. ## Build Guide diff --git a/examples/demo-pods/android/app/build.gradle.kts b/examples/demo-pods/android/app/build.gradle.kts index e6d82095..9bb102fc 100644 --- a/examples/demo-pods/android/app/build.gradle.kts +++ b/examples/demo-pods/android/app/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } android { - namespace = "com.onesignal.example" + namespace = "com.onesignal.sdkdemo" compileSdk = flutter.compileSdkVersion ndkVersion = flutter.ndkVersion @@ -16,7 +16,7 @@ android { defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). - applicationId = "com.onesignal.example" + applicationId = "com.onesignal.sdkdemo" // You can update the following values to match your application needs. // For more information, see: https://flutter.dev/to/review-gradle-config. minSdk = flutter.minSdkVersion diff --git a/examples/demo-pods/android/app/src/main/kotlin/com/onesignal/example/MainActivity.kt b/examples/demo-pods/android/app/src/main/kotlin/com/onesignal/sdkdemo/MainActivity.kt similarity index 75% rename from examples/demo-pods/android/app/src/main/kotlin/com/onesignal/example/MainActivity.kt rename to examples/demo-pods/android/app/src/main/kotlin/com/onesignal/sdkdemo/MainActivity.kt index df66b4f0..86870d73 100644 --- a/examples/demo-pods/android/app/src/main/kotlin/com/onesignal/example/MainActivity.kt +++ b/examples/demo-pods/android/app/src/main/kotlin/com/onesignal/sdkdemo/MainActivity.kt @@ -1,4 +1,4 @@ -package com.onesignal.example +package com.onesignal.sdkdemo import io.flutter.embedding.android.FlutterActivity diff --git a/examples/demo-pods/lib/main.dart b/examples/demo-pods/lib/main.dart index 9075802c..806acb55 100644 --- a/examples/demo-pods/lib/main.dart +++ b/examples/demo-pods/lib/main.dart @@ -11,22 +11,25 @@ import 'services/tooltip_helper.dart'; import 'theme.dart'; import 'viewmodels/app_viewmodel.dart'; -const String _defaultAppId = '77e32082-ea27-42e3-a898-c72e141824ef'; - Future main() async { WidgetsFlutterBinding.ensureInitialized(); try { await dotenv.load(fileName: '.env'); } catch (_) { - debugPrint('.env file not found, using defaults'); + debugPrint('.env file not found'); } final prefs = PreferencesService(); await prefs.init(); final envAppId = dotenv.env['ONESIGNAL_APP_ID']; - final appId = (envAppId != null && envAppId.isNotEmpty) ? envAppId : _defaultAppId; + final appId = envAppId?.trim() ?? ''; + if (appId.isEmpty) { + throw StateError( + 'ONESIGNAL_APP_ID is not set. Add it to examples/demo-pods/.env.', + ); + } // Initialize OneSignal SDK OneSignal.Debug.setLogLevel(OSLogLevel.verbose); diff --git a/examples/demo/README.md b/examples/demo/README.md index 73d97315..888c257c 100644 --- a/examples/demo/README.md +++ b/examples/demo/README.md @@ -24,11 +24,11 @@ ONESIGNAL_API_KEY=your-onesignal-api-key ONESIGNAL_ANDROID_CHANNEL_ID= ``` -- `ONESIGNAL_APP_ID`: falls back to a built-in default App ID when empty. +- `ONESIGNAL_APP_ID`: required. The app throws at startup when it is empty. - `ONESIGNAL_API_KEY`: REST API key, used to update and end live activities. Sending notifications from the demo doesn't need it. - `ONESIGNAL_ANDROID_CHANNEL_ID`: optional Android notification channel for the sound test notification. Falls back to a built-in default channel. -The OneSignal app needs FCM credentials for Android and APNs credentials for the `com.onesignal.example` bundle ID on iOS. +The OneSignal app needs FCM credentials for the `com.onesignal.sdkdemo` Android package and APNs credentials for the `com.onesignal.example` bundle ID on iOS. Then fetch dependencies: @@ -36,6 +36,16 @@ Then fetch dependencies: flutter pub get ``` +### Firebase Installation IDs (optional) + +E2E builds register with Firebase Installation IDs. To match locally, put a `google-services.json` for `com.onesignal.sdkdemo` in `android/app/` (it is gitignored) and build with: + +```bash +flutter build apk -PFIREBASE_INSTALLATION_ID_ENABLED=true +``` + +The flag is off by default, and the build fails if it is on without `google-services.json`. + ## Run ```bash diff --git a/examples/demo/android/.gitignore b/examples/demo/android/.gitignore index be3943c9..86cace77 100644 --- a/examples/demo/android/.gitignore +++ b/examples/demo/android/.gitignore @@ -12,3 +12,4 @@ GeneratedPluginRegistrant.java key.properties **/*.keystore **/*.jks +app/google-services.json diff --git a/examples/demo/android/app/build.gradle.kts b/examples/demo/android/app/build.gradle.kts index e6d82095..117f5ad1 100644 --- a/examples/demo/android/app/build.gradle.kts +++ b/examples/demo/android/app/build.gradle.kts @@ -4,8 +4,18 @@ plugins { id("dev.flutter.flutter-gradle-plugin") } +val hasGoogleServices = file("google-services.json").exists() + +// E2E builds pass `-PFIREBASE_INSTALLATION_ID_ENABLED=true` (`flutter build apk -P...`). +// The flag does nothing without the default FirebaseApp from google-services.json. +val firebaseInstallationIdEnabled = + (findProperty("FIREBASE_INSTALLATION_ID_ENABLED") as? String)?.trim()?.toBoolean() ?: false +if (firebaseInstallationIdEnabled && !hasGoogleServices) { + throw GradleException("FIREBASE_INSTALLATION_ID_ENABLED requires examples/demo/android/app/google-services.json") +} + android { - namespace = "com.onesignal.example" + namespace = "com.onesignal.sdkdemo" compileSdk = flutter.compileSdkVersion ndkVersion = flutter.ndkVersion @@ -15,14 +25,14 @@ android { } defaultConfig { - // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). - applicationId = "com.onesignal.example" + applicationId = "com.onesignal.sdkdemo" // You can update the following values to match your application needs. // For more information, see: https://flutter.dev/to/review-gradle-config. minSdk = flutter.minSdkVersion targetSdk = flutter.targetSdkVersion versionCode = flutter.versionCode versionName = flutter.versionName + manifestPlaceholders["firebaseInstallationIdEnabled"] = firebaseInstallationIdEnabled.toString() } buildTypes { @@ -43,3 +53,12 @@ kotlin { flutter { source = "../.." } + +dependencies { + // OneSignal resolves firebase-messaging 24.x; Installation ID registration needs 25.1.2+. + implementation("com.google.firebase:firebase-messaging:25.1.3") +} + +if (hasGoogleServices) { + apply(plugin = "com.google.gms.google-services") +} diff --git a/examples/demo/android/app/src/main/AndroidManifest.xml b/examples/demo/android/app/src/main/AndroidManifest.xml index dca75d49..d16e1840 100644 --- a/examples/demo/android/app/src/main/AndroidManifest.xml +++ b/examples/demo/android/app/src/main/AndroidManifest.xml @@ -5,6 +5,9 @@ android:label="OneSignal Demo" android:name="${applicationName}" android:icon="@mipmap/ic_launcher"> + main() async { WidgetsFlutterBinding.ensureInitialized(); try { await dotenv.load(fileName: '.env'); } catch (_) { - debugPrint('.env file not found, using defaults'); + debugPrint('.env file not found'); } final prefs = PreferencesService(); await prefs.init(); final envAppId = dotenv.env['ONESIGNAL_APP_ID']; - final appId = - (envAppId != null && envAppId.isNotEmpty) ? envAppId : _defaultAppId; + final appId = envAppId?.trim() ?? ''; + if (appId.isEmpty) { + throw StateError( + 'ONESIGNAL_APP_ID is not set. Add it to examples/demo/.env.', + ); + } // Initialize OneSignal SDK OneSignal.Debug.setLogLevel(OSLogLevel.verbose); diff --git a/examples/flutter-local-notif/README.md b/examples/flutter-local-notif/README.md index 75d4903b..53eb1576 100644 --- a/examples/flutter-local-notif/README.md +++ b/examples/flutter-local-notif/README.md @@ -10,8 +10,8 @@ for notifications created by the app. Its layout and test flow mirror the ## Setup 1. Create or select an app in the [OneSignal dashboard](https://dashboard.onesignal.com/). -2. Configure its Android and iOS platforms using bundle ID - `com.onesignal.example`. +2. Configure its Android platform with package `com.onesignal.sdkdemo` and its + iOS platform with bundle ID `com.onesignal.example`. 3. On iOS, select the `Runner` target in Xcode and choose your development team. The Push Notifications entitlement and remote notification background mode are already present. Add a OneSignal Notification Service Extension if you diff --git a/examples/flutter-local-notif/android/app/build.gradle.kts b/examples/flutter-local-notif/android/app/build.gradle.kts index 5b5788df..ed5134ee 100644 --- a/examples/flutter-local-notif/android/app/build.gradle.kts +++ b/examples/flutter-local-notif/android/app/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } android { - namespace = "com.onesignal.example" + namespace = "com.onesignal.sdkdemo" compileSdk = flutter.compileSdkVersion ndkVersion = flutter.ndkVersion @@ -16,7 +16,7 @@ android { } defaultConfig { - applicationId = "com.onesignal.example" + applicationId = "com.onesignal.sdkdemo" minSdk = flutter.minSdkVersion targetSdk = flutter.targetSdkVersion versionCode = flutter.versionCode diff --git a/examples/flutter-local-notif/android/app/src/main/kotlin/com/onesignal/example/MainActivity.kt b/examples/flutter-local-notif/android/app/src/main/kotlin/com/onesignal/example/MainActivity.kt deleted file mode 100644 index df66b4f0..00000000 --- a/examples/flutter-local-notif/android/app/src/main/kotlin/com/onesignal/example/MainActivity.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.onesignal.example - -import io.flutter.embedding.android.FlutterActivity - -class MainActivity : FlutterActivity() diff --git a/examples/flutter-local-notif/android/app/src/main/kotlin/com/onesignal/sdkdemo/MainActivity.kt b/examples/flutter-local-notif/android/app/src/main/kotlin/com/onesignal/sdkdemo/MainActivity.kt new file mode 100644 index 00000000..86870d73 --- /dev/null +++ b/examples/flutter-local-notif/android/app/src/main/kotlin/com/onesignal/sdkdemo/MainActivity.kt @@ -0,0 +1,5 @@ +package com.onesignal.sdkdemo + +import io.flutter.embedding.android.FlutterActivity + +class MainActivity : FlutterActivity()