### Install Go SDK
Source: https://documentation.onesignal.com/docs/en/server-sdk-reference
Install the Go SDK using go get. Check the package registry for the latest version.
```bash
go get github.com/OneSignal/onesignal-go-api/v5
```
--------------------------------
### Example Initialization with Custom Code Setup
Source: https://documentation.onesignal.com/docs/en/web-sdk-reference
Example of initializing the OneSignal Web SDK with custom code, including configuration for the notify button and prompt options.
```javascript
```
--------------------------------
### Setup and Start Default Live Activity
Source: https://documentation.onesignal.com/docs/en/mobile-sdk-reference
Configure Live Activities for push-to-start or launch them directly from the app. Ensure the activity ID is unique and does not contain '/' characters.
```csharp
using OneSignalSDK;
//Push To Start
OneSignal.LiveActivities.SetupDefault();
//Launching the Live Activity from within the app (not needed for push to start)
string activityId = "my_activity_id";
OneSignal.LiveActivities.StartDefault(
activityId,
new Dictionary() {
{ "title", "Welcome!" }
},
new Dictionary() {
{ "message", new Dictionary() {
{ "en", "Hello World!"}
}},
});
```
```javascript
import { OneSignal } from 'react-native-onesignal'
//Push To Start
OneSignal.LiveActivities.setupDefault()
//Launching the Live Activity from within the app (not needed for push to start)
const activityId = "my_activity_id"
const attributes = { title: "Sample Title" } ;
const content = { message: { en: "message" } };
OneSignal.LiveActivities.startDefault(activityId, attributes, content);
```
```dart
import 'package:onesignal_flutter/onesignal_flutter.dart';
OneSignal.LiveActivities.setupDefault()
//Launching the Live Activity from within the app (not needed for push to start)
const String activityId = "my_activity_id";
OneSignal.LiveActivities.startDefault(activityId!, {
"title": "Welcome!"
}, {
"message": {"en": "Hello World!"},
});
```
```javascript
//Ionic
import OneSignal from "onesignal-cordova-plugin";
//Push To Start
OneSignal.LiveActivities.setupDefault();
//Launching the Live Activity from within the app (not needed for push to start)
const activityId = "my_activity_id";
const attributes = { title: "Sample Title" };
const content = { message: { en: "message" } };
OneSignal.LiveActivities.startDefault(activityId, attributes, content);
//Cordova
//Push To Start
window.plugins.OneSignal.LiveActivities.setupDefault();
//Launching the Live Activity from within the app (not needed for push to start)
const activityId = "my_activity_id";
const attributes = { title: "Sample Title" };
const content = { message: { en: "message" } };
window.plugins.OneSignal.LiveActivities.startDefault(
activityId,
attributes,
content
);
```
--------------------------------
### Install C# (.NET) SDK
Source: https://documentation.onesignal.com/docs/en/server-sdk-reference
Install the C# (.NET) SDK using the dotnet CLI. Check the package registry for the latest version.
```bash
dotnet add package OneSignalApi
```
--------------------------------
### Install Node.js SDK
Source: https://documentation.onesignal.com/docs/en/server-sdk-reference
Install the Node.js SDK using npm. Check the package registry for the latest version.
```bash
npm install @onesignal/node-onesignal
```
--------------------------------
### Install Python SDK
Source: https://documentation.onesignal.com/docs/en/server-sdk-reference
Install the Python SDK using pip. Check the package registry for the latest version.
```bash
pip install onesignal-python-api
```
--------------------------------
### Example iOS Podfile Configuration
Source: https://documentation.onesignal.com/docs/en/cordova-sdk-setup
An example of a complete ios/Podfile, demonstrating how to include the OneSignal pod alongside React Native dependencies and post-install hooks.
```ruby
target 'MyApp' do
config = use_native_modules!
use_react_native!(
:path => config[:reactNativePath],
# An absolute path to your application root.
:app_path => "#{Pod::Config.instance.installation_root}/.."
)
post_install do |installer|
# https://github.com/facebook/react-native/blob/main/packages/react-native/scripts/react_native_pods.rb#L197-L202
react_native_post_install(
installer,
config[:reactNativePath],
:mac_catalyst_enabled => false,
# :ccache_enabled => true
)
end
end
target 'OneSignalNotificationServiceExtension' do
pod 'OneSignalXCFramework', '>= 5.0.0', '< 6.0'
end
```
--------------------------------
### Install Ruby SDK
Source: https://documentation.onesignal.com/docs/en/server-sdk-reference
Add the Ruby SDK to your Gemfile and run bundle install. Check the package registry for the latest version.
```ruby
gem 'onesignal', '~> 5.3'
```
--------------------------------
### Install React Native OneSignal SDK
Source: https://documentation.onesignal.com/docs/en/react-native-sdk-setup
Run this command to install the OneSignal SDK if it cannot be found.
```bash
npm install react-native-onesignal
```
--------------------------------
### setup() - Live Activities
Source: https://documentation.onesignal.com/docs/en/mobile-sdk-reference
Enables OneSignal to manage the lifecycle of Live Activities within your application. This includes handling token updates for starting and updating Live Activities, which must be initiated when your application is in the foreground.
```APIDOC
## Live Activity
Applications should allow users to opt-in to Live Activities. For example, your app gives the user the option to start the Live Activity within your US using a button or presenting an IAM. You may start and update a Live Activity via any method without an explicit prompt, unlike Notification Permission or Location Permission. Live Activities appear with the iOS Provisional Authorization UI. Live Activities must be started when your application is in the foreground. We recommend reading [Apple’s developer guidelines](https://developer.apple.com/design/human-interface-guidelines/live-activities) to learn more about Live Activities.
### `setup()`
Allows OneSignal to manage the lifecycle of a LiveActivity on behalf of the application. This includes listening for both pushToStart token updates and pushToUpdate token updates.
### Swift
```swift
//... your app's code
OneSignal.LiveActivities.setup(MyWidgetAttributes.self);
```
```
--------------------------------
### Install OneSignal Capacitor Plugin
Source: https://documentation.onesignal.com/docs/en/capacitor-sdk-setup
Install the `@onesignal/capacitor-plugin` package and sync native dependencies.
```shell
npm install @onesignal/capacitor-plugin
npx cap sync
```
--------------------------------
### Complete Example: Button Click to Tag User
Source: https://documentation.onesignal.com/docs/en/in-app-message-api
An example demonstrating how to attach an event listener to a button to tag the user with custom data when clicked.
```HTML
```
--------------------------------
### Start Backend Server
Source: https://documentation.onesignal.com/docs/en/identity-verification
Run your Node.js server to handle JWT generation requests. This command starts the server that will be used to test JWT generation.
```bash
node server.js
```
--------------------------------
### Start a Live Notification on Android
Source: https://documentation.onesignal.com/docs/en/android-live-notifications
Send the 'start' event to create a live notification. Initialize both static and dynamic data. Ensure the `collapse_id` is unique for this notification instance.
```bash
curl -X POST "https://api.onesignal.com/notifications" \
-H "Authorization: Key YOUR_APP_API_KEY" \
-H "Content-Type: application/json; charset=utf-8" \
-d '{
"app_id": "YOUR_APP_ID",
"isAndroid": true,
"collapse_id": "UNIQUE_LIVE_NOTIFICATION_ID",
"data": {
"live_notification": {
"key": "progress",
"event": "start",
"event_attributes": {},
"event_updates": {
"current_progress": 0
}
}
},
"headings": { "en": "Start" },
"contents": { "en": "Starting Live Notification" },
"include_aliases": { "external_id": ["EID1", "EID2"] },
"target_channel": "push"
}'
```
--------------------------------
### Start Live Notification
Source: https://documentation.onesignal.com/docs/en/android-live-notifications
Initiates a live notification by sending an 'start' event. This call initializes both static and dynamic data for the notification.
```APIDOC
## POST /notifications
### Description
Send an 'start' event to create a live notification. Initialize static (`event_attributes`) and dynamic (`event_updates`) data.
### Method
POST
### Endpoint
https://api.onesignal.com/notifications
### Request Body
- **app_id** (string) - Required - Your OneSignal App ID.
- **isAndroid** (boolean) - Required - Set to `true` to restrict delivery to Android push subscriptions.
- **collapse_id** (string) - Required - A unique ID for this live notification instance to enable collapsing.
- **data** (object) - Required - Contains live notification specific data.
- **live_notification** (object) - Required - Defines the live notification.
- **key** (string) - Required - A key identifying the type of live notification (e.g., 'progress').
- **event** (string) - Required - Must be set to 'start'.
- **event_attributes** (object) - Optional - Static data for the notification.
- **event_updates** (object) - Optional - Initial dynamic data for the notification.
- **headings** (object) - Required - Notification headings in different languages.
- **contents** (object) - Required - Notification content in different languages.
- **include_aliases** (object) - Required - User identifiers to target.
- **external_id** (array) - Required - Array of external IDs to send the notification to.
- **target_channel** (string) - Required - Set to 'push' for push notifications.
### Request Example
```json
{
"app_id": "YOUR_APP_ID",
"isAndroid": true,
"collapse_id": "UNIQUE_LIVE_NOTIFICATION_ID",
"data": {
"live_notification": {
"key": "progress",
"event": "start",
"event_attributes": {},
"event_updates": {
"current_progress": 0
}
}
},
"headings": { "en": "Start" },
"contents": { "en": "Starting Live Notification" },
"include_aliases": { "external_id": ["EID1", "EID2"] },
"target_channel": "push"
}
```
```
--------------------------------
### Example os_sdk payload
Source: https://documentation.onesignal.com/docs/en/mobile-sdk-reference
An example of the automatically included app-specific data under the reserved 'os_sdk' key in the properties payload for custom events.
```json
{
"os_sdk": {
"sdk": "050213",
"device_os": "18.5",
"type": "iOSPush",
"device_model": "iPhone14,4",
"device_type": "ios",
"app_version": "5.4.0"
}
}
```
--------------------------------
### setupDefault() and startDefault()
Source: https://documentation.onesignal.com/docs/en/mobile-sdk-reference
Allows cross-platform SDKs to manage the lifecycle of a Live Activity. `setupDefault()` initializes the Live Activity management, and `startDefault()` launches the Live Activity from within the app.
```APIDOC
## `setupDefault()` and `startDefault()`
### Description
Allows cross platform SDK's to manage the lifecycle of a LiveActivity by eliminating the need for a customer app to define and manage their own ActivityAttributes. See [Cross-platform setup](./cross-platform-live-activity-setup) for further details.
### Usage
**Push To Start:**
```csharp
OneSignal.LiveActivities.SetupDefault();
```
**Launching the Live Activity from within the app (not needed for push to start):**
```csharp
string activityId = "my_activity_id";
OneSignal.LiveActivities.StartDefault(
activityId,
new Dictionary() {
{ "title", "Welcome!" }
},
new Dictionary() {
{ "message", new Dictionary() {
{ "en", "Hello World!"}
}},
});
```
### React Native Example
```javascript
import { OneSignal } from 'react-native-onesignal'
//Push To Start
OneSignal.LiveActivities.setupDefault()
//Launching the Live Activity from within the app (not needed for push to start)
const activityId = "my_activity_id"
const attributes = { title: "Sample Title" } ;
const content = { message: { en: "message" } };
OneSignal.LiveActivities.startDefault(activityId, attributes, content);
```
### Flutter Example
```dart
import 'package:onesignal_flutter/onesignal_flutter.dart';
OneSignal.LiveActivities.setupDefault()
//Launching the Live Activity from within the app (not needed for push to start)
const String activityId = "my_activity_id";
OneSignal.LiveActivities.startDefault(activityId!, {
"title": "Welcome!"
},
{
"message": {"en": "Hello World!"},
});
```
### Cordova/Ionic Example
```javascript
//Ionic
import OneSignal from "onesignal-cordova-plugin";
//Push To Start
OneSignal.LiveActivities.setupDefault();
//Launching the Live Activity from within the app (not needed for push to start)
const activityId = "my_activity_id";
const attributes = { title: "Sample Title" };
const content = { message: { en: "message" } };
OneSignal.LiveActivities.startDefault(activityId, attributes, content);
//Cordova
//Push To Start
window.plugins.OneSignal.LiveActivities.setupDefault();
//Launching the Live Activity from within the app (not needed for push to start)
const activityId = "my_activity_id";
const attributes = { title: "Sample Title" };
const content = { message: { en: "message" } };
window.plugins.OneSignal.LiveActivities.startDefault(
activityId,
attributes,
content
);
```
```
--------------------------------
### Configure Go SDK
Source: https://documentation.onesignal.com/docs/en/server-sdk-reference
Initialize the Go SDK with your API keys using context values. This setup is necessary before making any API calls.
```go
import onesignal "github.com/OneSignal/onesignal-go-api"
restAuth := context.WithValue(
context.Background(),
onesignal.RestApiKey,
"YOUR_REST_API_KEY",
)
orgAuth := context.WithValue(
restAuth,
onesignal.OrganizationApiKey,
"YOUR_ORGANIZATION_API_KEY",
)
apiClient := onesignal.NewAPIClient(onesignal.NewConfiguration())
```
--------------------------------
### Setup Live Activities
Source: https://documentation.onesignal.com/docs/en/mobile-sdk-reference
Allows OneSignal to manage the lifecycle of a Live Activity. This includes listening for pushToStart and pushToUpdate token updates. Ensure your application is in the foreground when starting a Live Activity.
```swift
//... your app's code
OneSignal.LiveActivities.setup(MyWidgetAttributes.self);
```
--------------------------------
### Huawei Push Token Error Example
Source: https://documentation.onesignal.com/docs/en/huawei-flutter-sdk-setup
This log snippet indicates an issue with obtaining the Huawei push token. Check your Proguard or R8 rules for proper setup, or consider disabling them temporarily to diagnose the problem.
```log
E/OneSignal: HMS ApiException getting Huawei push token!
com.huawei.hms.common.ApiException: -5: Core error
```
--------------------------------
### Store and Retrieve Badge Count using UserDefaults
Source: https://documentation.onesignal.com/docs/en/badges
Since iOS does not provide a system method to get the current badge count, you must manage it yourself. This example shows how to use UserDefaults to store and retrieve the badge count.
```swift
// Example: Store and retrieve badge count using UserDefaults
let badgeCount = UserDefaults.standard.integer(forKey: "badgeCount")
// Update badge count as needed
UserDefaults.standard.set(badgeCount, forKey: "badgeCount")
```
--------------------------------
### Initialize and Control Onboarding Carousel
Source: https://documentation.onesignal.com/docs/en/in-app-onboarding-carousel-tutorial
This JavaScript code initializes the carousel, handles card transitions, and manages the 'Next' and 'Get Started' button actions. It also includes logic to dismiss the in-app message using the OneSignal IAM API.
```javascript
document.getElementById("card-1").className = i === 1 ? "card active" : "card";
// Update progress dots
var dots = document.getElementById("dots").children;
dots[0].classList.toggle("active", i === 0);
dots[1].classList.toggle("active", i === 1);
}
// Button: Next (card 0 → card 1)
document.getElementById("next-0").addEventListener("click", function () {
setActive(1);
});
// Button: Get Started (dismisses the In-App Message)
document.getElementById("done").addEventListener("click", function (e) {
// Check if OneSignal IAM API is available
if (window.OneSignalIamApi && OneSignalIamApi.close) {
OneSignalIamApi.close(e); // Dismiss the message
}
});
})();