// Initialize SDK during app bootstrapping
package com.onesignal.sample.android
import android.app.Application
import com.onesignal.OneSignal
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
OneSignal.initWithContext(this, "APP_ID")
}
}
Build for Android
import SwiftUI
import DotEnv
import OneSignalFramework
@main
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class AppDelegate: UIResponder, UIApplicationDelegate, OSInAppMessageLifecycleListener {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
let env = DotEnv(withFile: ".env")
env.load()
OneSignal.initialize(env.get("ONESIGNAL_APP_ID"), withLaunchOptions: launchOptions)
return true
}
}
Build for iOS
import 'package:flutter/material.dart';
import 'package:onesignal_flutter/onesignal_flutter.dart';
import 'package:flutter_application_la/live_actiivties_manager.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
LiveActivitiesManager.register();
OneSignal.initialize("<<YOUR_APP_ID>>");
OneSignal.Notifications.requestPermission(true);
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
Build with Flutter
import * as React from 'react';
import { APP_ID } from '@env';
import { OneSignal} from 'react-native-onesignal';
class OS extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { };
}
async componentDidMount() {
OneSignal.initialize(APP_ID);
}
}
Build with React Native
using System;
using UnityEngine;
using UnityEngine.UI;
using OneSignalSDK;
public class ExampleBehaviour : MonoBehaviour
{
public OSConfigData configData;
public string externalId;
private void Start()
{
OneSignal.Initialize(configData.appId);
}
private void _userStateChanged(object sender, UserStateChangedEventArgs e) {
// OneSignal ID and External ID are available in event state
// e.g., `e.state.Current.OneSignalId`
}
public void LoginUser() {
OneSignal.Login(externalId);
}
public void SetExternalIdString(string newVal) => externalId = newVal;
}
Build with Unity