镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
Merged
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
11 changes: 10 additions & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,20 @@ 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/android
# -PciSingleAbi → arm64-v8a-only APK (~10 MB) instead of the
# ~30 MB universal APK. BrowserStack devices we test against
# are all arm64-v8a.
run: ./gradlew assembleRelease -PciSingleAbi --quiet --console=plain --warning-mode=summary
# FIREBASE_INSTALLATION_ID_ENABLED turns on Firebase Installation ID registration,
# which needs the google-services.json written above.
run: ./gradlew assembleRelease -PciSingleAbi -PFIREBASE_INSTALLATION_ID_ENABLED=true --quiet --console=plain --warning-mode=summary

- name: Upload APK
uses: actions/upload-artifact@v7
Expand Down Expand Up @@ -144,6 +152,7 @@ jobs:
app-artifact: demo-apk
app-filename: app-arm64-v8a-release.apk
sdk-type: react-native
bundle-id: com.onesignal.sdkdemo
build-name: react-native-android-${{ github.ref_name }}-${{ github.run_number }}

e2e-ios:
Expand Down
6 changes: 4 additions & 2 deletions examples/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ mv demo examples/demo
- Architecture: provider + hook (`OneSignalProvider` / `useOneSignal`), no repository class, no reducer
- Separate component files per section
- Support both Android and iOS
- Android `namespace` and `applicationId` in `android/app/build.gradle` are `com.onesignal.sdkdemo`, to match the shared `google-services.json`. The iOS bundle id stays `com.onesignal.example`.
- Android applies the Google Services plugin only when `android/app/google-services.json` exists (gitignored), and depends on `firebase-messaging` 25.1.2+ for Firebase Installation ID registration. The `firebase_messaging_installation_id_enabled` manifest flag is off unless Gradle gets `-PFIREBASE_INSTALLATION_ID_ENABLED=true`, which also requires `google-services.json`.

App bar logo: import SVG directly via `react-native-svg-transformer`:

Expand Down Expand Up @@ -55,7 +57,7 @@ Package scripts (`examples/demo/package.json`):
"android": "bash ../run-android.sh",
"ios": "bash ../run-ios.sh",
"update:pods": "(cd ios && pod update OneSignalXCFramework --no-repo-update)",
"clean:android": "rm -rf android/app/build android/app/.cxx android/build && adb uninstall com.onesignal.example >/dev/null 2>&1 || true",
"clean:android": "rm -rf android/app/build android/app/.cxx android/build && adb uninstall com.onesignal.sdkdemo >/dev/null 2>&1 || true",
"clean:ios": "rm -rf ios/build ios/Pods",
"start": "react-native start"
},
Expand Down Expand Up @@ -126,7 +128,7 @@ apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
- `ONESIGNAL_APP_ID` -- app id passed to `OneSignal.initialize(...)`.
- `ONESIGNAL_API_KEY` -- REST key used by `OneSignalApiService` for sends and Live Activity update/end.
- `ONESIGNAL_ANDROID_CHANNEL_ID` -- channel id sent with the WITH SOUND push (`android_channel_id`).
- `src/hooks/useOneSignal.ts` defines a hardcoded `DEFAULT_APP_ID` fallback used when `ONESIGNAL_APP_ID` is unset/empty.
- `ONESIGNAL_APP_ID` is required. `metro.config.js` fails the bundle when it's unset; there is no default app id.

---

Expand Down
4 changes: 2 additions & 2 deletions examples/demo-no-location/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ android {
buildToolsVersion rootProject.ext.buildToolsVersion
compileSdk rootProject.ext.compileSdkVersion

namespace "com.onesignal.example"
namespace "com.onesignal.sdkdemo"
defaultConfig {
applicationId "com.onesignal.example"
applicationId "com.onesignal.sdkdemo"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.onesignal.example
package com.onesignal.sdkdemo

import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.onesignal.example
package com.onesignal.sdkdemo

import android.app.Application
import com.facebook.react.PackageList
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-no-location/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"ios": "bash ../run-ios.sh",
"pods": "bundle install && (cd ios && ONESIGNAL_DISABLE_LOCATION=true bundle exec pod install)",
"update:pods": "(cd ios && ONESIGNAL_DISABLE_LOCATION=true pod update OneSignalXCFramework --no-repo-update)",
"clean:android": "rm -rf android/app/build android/app/.cxx android/build && adb uninstall com.onesignal.example >/dev/null 2>&1 || true",
"clean:android": "rm -rf android/app/build android/app/.cxx android/build && adb uninstall com.onesignal.sdkdemo >/dev/null 2>&1 || true",
"clean:ios": "rm -rf ios/build ios/Pods",
"start": "react-native start"
},
Expand Down
4 changes: 2 additions & 2 deletions examples/demo/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Default App ID (used if ONESIGNAL_APP_ID is empty): 77e32082-ea27-42e3-a898-c72e141824ef
ONESIGNAL_APP_ID=your-onesignal-app-id
# Required. The Metro bundle fails when this is unset.
ONESIGNAL_APP_ID=
ONESIGNAL_API_KEY=your-onesignal-api-key

# Optional: Android Notification Channel ID for the WITH SOUND test notification.
Expand Down
1 change: 1 addition & 0 deletions examples/demo/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Environment
.env
android/app/google-services.json
.rn-demo-env.stamp

# OSX
Expand Down
22 changes: 12 additions & 10 deletions examples/demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Reference app for `react-native-onesignal`. It showcases SDK features including push notifications, in-app messaging, user management, tags, triggers, outcomes, live activities, and more, using the SDK packed from this repository.

The demo uses package/bundle id `com.onesignal.example`.
The demo uses Android package `com.onesignal.sdkdemo` and iOS bundle id `com.onesignal.example`.

## Prerequisites

Expand All @@ -28,16 +28,18 @@ cp .env.example .env
Set your OneSignal credentials in `.env`:

```sh
ONESIGNAL_APP_ID=your-onesignal-app-id
ONESIGNAL_APP_ID=
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. `metro.config.js` fails the bundle when it's unset.
- `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.

To use Firebase on Android, put a `google-services.json` with a `com.onesignal.sdkdemo` client in `android/app/`. It's gitignored, and the Google Services plugin is applied only when it exists. Pass `-PFIREBASE_INSTALLATION_ID_ENABLED=true` to Gradle to turn on Firebase Installation ID registration; the build fails if the flag is on without the file.

For iOS, install CocoaPods on first run:

Expand All @@ -63,12 +65,12 @@ It then builds and launches the app with the React Native CLI, which starts Metr

## Other scripts

| Script | Purpose |
| ---------------------- | -------------------------------------------------------------------- |
| `vp run setup` | Build, pack, and install the SDK into the demo. |
| `vp run start` | Start Metro. |
| `vp run update:pods` | Run `pod update OneSignalXCFramework` after native SDK changes. |
| `vp run clean:android` | Wipe Android build outputs and uninstall the app from the device. |
| Script | Purpose |
| ---------------------- | --------------------------------------------------------------------- |
| `vp run setup` | Build, pack, and install the SDK into the demo. |
| `vp run start` | Start Metro. |
| `vp run update:pods` | Run `pod update OneSignalXCFramework` after native SDK changes. |
| `vp run clean:android` | Wipe Android build outputs and uninstall the app from the device. |
| `vp run clean:ios` | Wipe iOS build outputs and `ios/Pods`. Run `pod install` again after. |

## Testing iOS Action Button Icons
Expand Down
21 changes: 19 additions & 2 deletions examples/demo/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,29 @@ def enableProguardInReleaseBuilds = true
*/
def jscFlavor = 'io.github.react-native-community:jsc-android:2026004.+'

def hasGoogleServices = file("google-services.json").exists()

// Off by default so local builds keep the legacy token path. E2E opts in with
// `-PFIREBASE_INSTALLATION_ID_ENABLED=true`; the flag does nothing without the default FirebaseApp.
def firebaseInstallationIdEnabled =
(findProperty("FIREBASE_INSTALLATION_ID_ENABLED") ?: "false").toString().trim().toBoolean()
if (firebaseInstallationIdEnabled && !hasGoogleServices) {
throw new GradleException("FIREBASE_INSTALLATION_ID_ENABLED requires examples/demo/android/app/google-services.json")
}

android {
ndkVersion rootProject.ext.ndkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
compileSdk rootProject.ext.compileSdkVersion

namespace "com.onesignal.example"
namespace "com.onesignal.sdkdemo"
defaultConfig {
applicationId "com.onesignal.example"
applicationId "com.onesignal.sdkdemo"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
manifestPlaceholders.firebaseInstallationIdEnabled = firebaseInstallationIdEnabled.toString()
}
signingConfigs {
debug {
Expand Down Expand Up @@ -127,10 +138,16 @@ apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
dependencies {
// The version of react-native is set by the React Native Gradle Plugin
implementation("com.facebook.react:react-android")
// OneSignal resolves firebase-messaging 24.x; Installation ID registration needs 25.1.2+.
implementation("com.google.firebase:firebase-messaging:25.1.3")

if (hermesEnabled.toBoolean()) {
implementation("com.facebook.react:hermes-android")
} else {
implementation jscFlavor
}
}

if (hasGoogleServices) {
apply plugin: "com.google.gms.google-services"
}
4 changes: 4 additions & 0 deletions examples/demo/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
android:theme="@style/AppTheme"
android:usesCleartextTraffic="${usesCleartextTraffic}"
android:supportsRtl="true">
<meta-data
android:name="firebase_messaging_installation_id_enabled"
android:value="${firebaseInstallationIdEnabled}" />

<activity
android:name=".MainActivity"
android:label="@string/app_name"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.onesignal.example
package com.onesignal.sdkdemo

import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.onesignal.example
package com.onesignal.sdkdemo

import android.app.Application
import com.facebook.react.PackageList
Expand Down
1 change: 1 addition & 0 deletions examples/demo/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ buildscript {
classpath("com.android.tools.build:gradle")
classpath("com.facebook.react:react-native-gradle-plugin")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin")
classpath("com.google.gms:google-services:4.4.4")
}
}

Expand Down
14 changes: 12 additions & 2 deletions examples/demo/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
const crypto = require('node:crypto');
const fs = require('node:fs');
const path = require('node:path');
const { parseEnv } = require('node:util');

/**
* Metro configuration
Expand All @@ -12,10 +13,19 @@ const path = require('node:path');
const defaultConfig = getDefaultConfig(__dirname);
const { assetExts, sourceExts } = defaultConfig.resolver;
const envPath = path.join(__dirname, '.env');
const envHash = fs.existsSync(envPath)
? crypto.createHash('sha1').update(fs.readFileSync(envPath)).digest('hex')
const envContents = fs.existsSync(envPath) ? fs.readFileSync(envPath, 'utf8') : null;
const envHash = envContents
? crypto.createHash('sha1').update(envContents).digest('hex')
: 'missing';

// react-native-dotenv lets process.env override .env, so check both.
const appId = (
process.env.ONESIGNAL_APP_ID ?? parseEnv(envContents ?? '').ONESIGNAL_APP_ID
)?.trim();
if (!appId) {
throw new Error('ONESIGNAL_APP_ID is not set. Add it to examples/demo/.env.');
}

const config = {
// react-native-dotenv does not include .env in Metro's transform cache key.
cacheVersion: `env-${envHash}`,
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"android": "bash ../run-android.sh",
"ios": "bash ../run-ios.sh",
"update:pods": "(cd ios && pod update OneSignalXCFramework)",
"clean:android": "rm -rf android/app/build android/app/.cxx android/build && adb uninstall com.onesignal.example >/dev/null 2>&1 || true",
"clean:android": "rm -rf android/app/build android/app/.cxx android/build && adb uninstall com.onesignal.sdkdemo >/dev/null 2>&1 || true",
"clean:ios": "rm -rf ios/build ios/Pods",
"start": "react-native start"
},
Expand Down
5 changes: 2 additions & 3 deletions examples/demo/src/hooks/useOneSignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ import { log } from '../services/LogService';
import OneSignalApiService from '../services/OneSignalApiService';
import PreferencesService from '../services/PreferencesService';

const DEFAULT_APP_ID = '77e32082-ea27-42e3-a898-c72e141824ef';

// metro.config.js fails the bundle when this is unset.
function resolveAppId(): string {
return ONESIGNAL_APP_ID?.trim() || DEFAULT_APP_ID;
return ONESIGNAL_APP_ID.trim();
}

const apiService = OneSignalApiService.getInstance();
Expand Down
Loading