SDK configuration
The SDK is built on a modular principle: the core (Core) is responsible for initialization, authorization and events, while the Push, In-App, Target and Profile modules are connected as separate npm packages. The @altcraft/react-native-sdk package contains the entire SDK API on top of the modules.
Prerequisites
- The React Native 0.75+ project runs on Android 7.1+ (API 25) and iOS 13.0+.
- The SDK packages are added to the project:
| Package | Module | Purpose |
|---|---|---|
@altcraft/react-native-sdk | — | Wrapper package with the entire SDK API on top of the modules |
@altcraft/core | Core | Initialization, authorization, events, data clearing |
@altcraft/push | Push | Subscription, push tokens, notification handling |
@altcraft/inapp | In-App | In-App campaigns, placements, animations |
@altcraft/target | Target | Mobile events, UTM tags |
@altcraft/profile | Profile | Profile field updates |
- Push notification provider SDKs are integrated into the application project.
- If API requests must be authorized via JWT, a JWT provider is implemented (see Passing the JWT token to the SDK).
Installing the wrapper package with the entire SDK:
yarn add @altcraft/react-native-sdk
# or
npm i @altcraft/react-native-sdk
Installing individual modules (the @altcraft/core package must be specified as a direct dependency — the other modules are built on it):
yarn add @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 android
# or
npm run ios
The AltcraftConfigBuilder.pushConfig() and AltcraftConfigBuilder.inAppConfig() methods are available only if the corresponding module package is imported into the file with the initialization. This mirrors the logic of native module linking.
Preparing the application
Passing the JWT token to the SDK (optional)
A JWT implementation is required if JWT authentication of API requests is used. The JWT confirms that user identifiers are authenticated by the application.
A JWT authentication implementation is mandatory if a matching type other than push data from the subscription is used (for example, the user identifier is an email or a phone number).
In the React Native SDK the JWT provider is registered with the setJwtProvider() method:
import { AltcraftSDK } from '@altcraft/react-native-sdk';
AltcraftSDK.setJwtProvider(async () => {
// Return the current JWT token of the user.
// The provider may be called on every request that requires authorization.
return await myBackend.getJwtToken();
});
Rules for working with the JWT provider:
- the provider is registered before the
initialize()call and before the first operation that requires authorization; - the native side requests the token from the application at the moment a request is executed, so the provider must return a current value rather than one cached for the entire lifetime of the application;
- if the provider returns
null, the SDK fails the request with an authorization error (the event "JWT token is null" / "JWT token is nil").
The authenticate() and logOut() methods are used to log the user in and out:
// Start the user session with the current JWT
AltcraftSDK.authenticate();
// End the session: the SDK clears the profile data and starts an anonymous session
AltcraftSDK.logOut();
// Check whether the user is authenticated
const isAuthenticated = await AltcraftSDK.isAuthenticated();
Preparing to work with push providers
Push providers are configured on the native side of the application.
Android:
iOS:
The provider priority is set in the Push module configuration (pushConfig({ providerPriority: [...] })), not in the common SDK parameters. Connecting dependencies, services and handlers is performed in the native parts of the project.
SDK initialization
Initialization parameters
Configuration parameters are passed through an object of the AltcraftConfig type. Common parameters can be passed directly, but for module settings use AltcraftConfigBuilder: common parameters are passed to the constructor, module parameters are added by extension methods.
import { AltcraftConfigBuilder } from '@altcraft/core';
import '@altcraft/inapp';
import '@altcraft/push';
const config = new AltcraftConfigBuilder({
apiUrl: 'https://pxl-example.altcraft.com',
rToken: 'your-role-token',
appInfo: {
appID: 'your-app-id',
appIID: 'your-installation-id',
appVer: '1.0.0',
},
enableLogging: true,
})
.pushConfig({
providerPriority: ['android-firebase', 'android-huawei', 'android-rustore'],
})
.inAppConfig({
maxPlacements: 500,
})
.build();
Common parameters (AltcraftConfigBuilder / AltcraftConfig)
apiUrl string
Required: Yes
Description: Altcraft API endpoint URL.
rToken string | null
Default: null
Required: No
Description: Altcraft role token (identifies the resource/database/account). Used when the only matching type is the device push token.
appInfo AppInfo | null
Default: null
Required: No
Description: Basic application metadata.
| Field | Type | Description |
|---|---|---|
appID | string | Application identifier. |
appIID | string | Application installation identifier. |
appVer | string | Application version. |
enableLogging boolean | null
Default: null
Required: No
Description: Enables/disables logging.
moduleConfigs string | null
Default: null
Required: No
Description: Serialized module configurations by identifier (push, inapp). Produced by AltcraftConfigBuilder; the core passes the value to the native side without interpreting its content.
Push module parameters (pushConfig)
| Field | Type | Platform | Description |
|---|---|---|---|
providerPriority | string[] | null | Android, iOS | Provider priority list (by index: 0 is the highest priority). |
icon | number | null | Android | drawable resource ID used as the notification icon. Takes precedence over iconName. |
iconName | string | null | Android | Notification icon resource name: looked up in drawable, then in mipmap. |
pushReceiverModules | string[] | null | Android | List of package names containing native AltcraftPushReceiver implementations. |
pushChannelName | string | null | Android | Base name of the push notification channel (visible in Android settings). |
pushChannelDescription | string | null | Android | Push channel description (visible in Android settings). |
Example for Android:
.pushConfig({
providerPriority: ['android-firebase', 'android-huawei', 'android-rustore'],
iconName: 'ic_notification',
pushChannelName: 'Altcraft',
pushChannelDescription: 'Marketing notifications',
})
Example for iOS:
.pushConfig({
providerPriority: ['ios-apns', 'ios-firebase', 'ios-huawei'],
})
In-App module parameters (inAppConfig)
| Field | Type | Default | Description |
|---|---|---|---|
inAppAutoRequest | boolean | true | Automatically request In-App placements. |
placementsRequestIntervalMs | number | 30 minutes | Interval between automatic placement requests in milliseconds. The native side applies a minimum value of 15 minutes. |
maxPlacements | number | 500 | Maximum number of placements in local storage. |
.inAppConfig({
inAppAutoRequest: true,
placementsRequestIntervalMs: 30 * 60 * 1000,
maxPlacements: 500,
})
Performing initialization
Use the initialize(config) function to initialize the SDK:
import { AltcraftSDK } from '@altcraft/react-native-sdk';
await AltcraftSDK.initialize({
apiUrl: 'https://pxl-example.altcraft.com',
rToken: null,
enableLogging: true,
appInfo: {
appID: 'your-app-id',
appIID: 'your-installation-id',
appVer: '1.0.0',
},
});
If module settings are needed (push providers, In-App parameters), pass the configuration assembled by the builder to initialize():
import { AltcraftConfigBuilder } from '@altcraft/core';
import { AltcraftSDK } from '@altcraft/react-native-sdk';
import '@altcraft/push';
await AltcraftSDK.initialize(
new AltcraftConfigBuilder({ apiUrl: 'https://pxl-example.altcraft.com' })
.pushConfig({ providerPriority: ['android-firebase'] })
.build()
);
Call order at application startup:
setJwtProvider()— if JWT authentication is used.initialize(config)— once per application run.requestNotificationPermission()— on Android 13+ and on iOS, before the first subscription.pushSubscribe()— subscription to push notifications.
Requesting notification permission
The permission is requested with the requestNotificationPermission() method:
import { AltcraftSDK } from '@altcraft/react-native-sdk';
const granted = await AltcraftSDK.requestNotificationPermission();
- 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).
The method returns true if the permission is granted.
Working with App Group and UserDefaults on iOS
For iOS the SDK provides methods for setting the App Group and writing string values to UserDefaults. On Android setAppGroup() is a no-op, and setUserDefaultsValue() stores the value in the SDK SharedPreferences.
import { Platform } from 'react-native';
import { AltcraftSDK } from '@altcraft/react-native-sdk';
if (Platform.OS === 'ios') {
const appGroup = 'group.altcraft.rn.example';
AltcraftSDK.setAppGroup(appGroup);
// Token for the Notification Service Extension
AltcraftSDK.setUserDefaultsValue(appGroup, 'altcraft_jwt', jwtToken);
}
| Method | Description |
|---|---|
setAppGroup(groupName: string | null) | Sets the App Group, the UserDefaults space shared between the application and the NSE. On iOS groupName must not be null. |
setUserDefaultsValue(suiteName: string | null, key: string, value: string | null) | Stores a string value. value = null deletes the key. On Android suiteName is ignored. |