Quick start
This article provides an example of a quick Altcraft mSDK integration for React Native.
Step 0. Prerequisites
- The React Native project is created and runs on iOS/Android.
- Native push providers are integrated into the project:
- The Altcraft mSDK is configured on the native side:
- push providers are registered;
- interception of incoming push notifications and passing them to the SDK is configured.
- Prepare your application backend to work with JWT if JWT authentication is used.
Step 1. Installing the package
Connect the wrapper package with the entire SDK:
npm i @altcraft/react-native-sdk
# or
yarn add @altcraft/react-native-sdk
Or only the required modules (the @altcraft/core package is mandatory as a direct dependency):
npm i @altcraft/core @altcraft/push @altcraft/inapp @altcraft/target @altcraft/profile
After installing the native modules, rebuild the application:
cd ios && pod install && cd ..
npm run ios
# or
npm run android
Step 2. SDK configuration and initialization
Import the SDK and perform initialization at application startup (for example, in App.tsx):
import { AltcraftSDK } from '@altcraft/react-native-sdk';
await AltcraftSDK.initialize({
apiUrl: 'https://pxl-your-instance.altcraft.com',
rToken: null,
appInfo: {
appID: 'your-app-id',
appIID: 'your-installation-id',
appVer: '1.0.0',
},
enableLogging: true,
});
If module settings are needed (push provider priority, In-App parameters), assemble the configuration through AltcraftConfigBuilder:
import { AltcraftConfigBuilder } from '@altcraft/core';
import { AltcraftSDK } from '@altcraft/react-native-sdk';
import '@altcraft/push';
await AltcraftSDK.initialize(
new AltcraftConfigBuilder({ apiUrl: 'https://pxl-your-instance.altcraft.com' })
.pushConfig({ providerPriority: ['android-firebase'] })
.build()
);
If JWT authentication is used, register the JWT provider before initialization:
AltcraftSDK.setJwtProvider(async () => await myBackend.getJwtToken());
Step 3. Subscribing to push notifications
Push subscription is performed by calling pushSubscribe(). All parameters are passed as an object:
import { AltcraftSDK } from '@altcraft/react-native-sdk';
AltcraftSDK.pushSubscribe({
sync: true,
profileFields: { _fname: 'user_first_name', _lname: 'user_last_name' },
customFields: { source: 'rn_app' },
cats: [{ name: 'developer_news', active: true }],
});
sync: true— executes the request synchronously; the result arrives through SDK events.sync: false— executes the request asynchronously.profileFields— profile fields.customFields— custom subscription fields.cats— subscription categories.
The method returns Promise<void>. Calling it without await is allowed (fire-and-forget), since the processing result is available through SDK events.
Step 4. Subscribing to SDK events
Use subscribeToEvents() to receive call results and internal SDK events:
import { AltcraftSDK, type SdkEvent } from '@altcraft/react-native-sdk';
const handler = (event: SdkEvent) => {
// event.function — name of the SDK function that raised the event
// event.type — 'event' | 'error' | 'retryError'
// event.message — event message (the main identifier)
// event.code — HTTP response code if the event is related to a request
// event.value — additional data (if any)
console.log('[AltcraftSDK event]', event.type, event.message);
};
AltcraftSDK.subscribeToEvents(handler);
To unsubscribe, pass the same handler:
AltcraftSDK.unsubscribeFromEvents(handler);
Event messages are conveniently compared with the module catalog constants, for example AltcraftPush.moduleEvents.subscribeRequestSuccessful (the value is successful request: push/subscribe).
Step 5. Requesting notification permission
Before the first subscription, request notification permission:
import { AltcraftSDK } from '@altcraft/react-native-sdk';
const granted = await AltcraftSDK.requestNotificationPermission();
if (granted) {
await AltcraftSDK.pushSubscribe({ sync: true });
}
- Android. The
POST_NOTIFICATIONSruntime permission is requested for Android 13+. On earlier versions the method returnstrueimmediately. - iOS. Authorization is requested through
UNUserNotificationCenter(.alert,.badge,.sound).