SDK functionality
You must first configure the SDK for your application. Detailed instructions are available here.
SDK operations are grouped by module. The AltcraftSDK wrapper class from the @altcraft/react-native-sdk package duplicates the module methods, so the examples below use both AltcraftSDK.pushSubscribe() and AltcraftPush.pushSubscribe() — this is the same call. If individual modules are connected to the project, call the methods of the module class (AltcraftPush, AltcraftInApp, AltcraftTarget, AltcraftProfile, AltcraftCore).
Working with subscription statuses
Changing the subscription status
AltcraftPush (@altcraft/push)
├─ static pushSubscribe(options?: PushSubscribeOptions): Promise<void> // status = subscribed
├─ static pushSuspend(options?: PushSubscribeOptions): Promise<void> // status = suspended
├─ static pushUnSubscribe(options?: PushSubscribeOptions): Promise<void> // status = unsubscribed
└─ static unSuspendPushSubscription(): Promise<ResponseWithHttpCode | null>
type PushSubscribeOptions = {
sync?: boolean | null;
profileFields?: Record<string, unknown> | null;
customFields?: Record<string, unknown> | null;
cats?: CategoryData[] | null;
replace?: boolean | null;
skipTriggers?: boolean | null;
};
pushSubscribe()— subscribes to push notifications;pushSuspend()— suspends the push subscription (notifications do not arrive, but no unsubscription event is created in the user profile);pushUnSubscribe()— cancels the push subscription;unSuspendPushSubscription()— used to buildLogInandLogOuttransitions.
The pushSubscribe(), pushSuspend() and pushUnSubscribe() functions have the same signature and accept a single options object.
sync: boolean | null
Default: null — the native side behavior (true)
Required: No
Description: Flag that sets whether the request is executed synchronously.
Successful request execution:
If the request succeeds, an SDK event of the event type is created with the message:
| Function | Success message |
|---|---|
pushSubscribe() | successful request: push/subscribe |
pushSuspend() | successful request: push/suspend |
pushUnSubscribe() | successful request: push/unsubscribe |
Ready-made message constants are available in the module catalog: AltcraftPush.moduleEvents.subscribeRequestSuccessful, AltcraftPush.moduleEvents.suspendRequestSuccessful, AltcraftPush.moduleEvents.unsubscribeRequestSuccessful.
The contents of event.value depend on the synchronization flag:
If sync == true
ResponseWithHttpCode
├─ httpCode: 200
└─ response
├─ error: 0
├─ errorText: ""
└─ profile
├─ id: "000000000000000000000000"
├─ status: "subscribed"
├─ isTest: false
└─ subscription
├─ subscriptionId: "provider-subscription-id"
├─ hashId: "7f31a9c4"
├─ provider: "android-firebase"
├─ status: "subscribed"
├─ fields
│ ├─ _os: "Android"
│ ├─ _os_ver: "14"
│ ├─ _device_type: "mob"
│ ├─ _device_model: "Pixel 7"
│ └─ _app_ver: "1.0.0"
└─ cats
└─ [ { name: "developer_news", active: true } ]
For a synchronous request, event.value exposes http_code and response with the error, error_text and profile fields under the response_with_http_code key. The JavaScript wrapper normalizes them into ResponseWithHttpCode with the httpCode, response.error, response.errorText and response.profile fields.
If sync == false
ResponseWithHttpCode
├─ httpCode: number | null
└─ response: Response | null
├─ error: number | null
├─ errorText: string | null
└─ profile: null // usually null for an asynchronous request
For an asynchronous request profile is always null — profile and subscription data are not returned in the event.
Failed request execution:
If the request fails, an event is created with the following messages:
Errors without an automatic retry on the SDK side — event type error:
failed request: push/subscribe— notification subscription;failed request: push/suspend— subscription suspension;failed request: push/unsubscribe— unsubscription.
Errors with an automatic retry on the SDK side — event type retryError:
push subscribe retry limit— the subscription retry limit is exhausted;push event retry limit— the push event sending retry limit is exhausted.
Event contents:
- only
http_codeif the Altcraft server was unavailable; erroranderror_textif the server returned an error.
Reading event values
import { AltcraftCore } from '@altcraft/core';
import { AltcraftPush } from '@altcraft/push';
const responseMessages = [
AltcraftPush.moduleEvents.subscribeRequestSuccessful,
AltcraftPush.moduleEvents.suspendRequestSuccessful,
AltcraftPush.moduleEvents.unsubscribeRequestSuccessful,
AltcraftPush.moduleEvents.subscribeRequestFailed,
AltcraftPush.moduleEvents.suspendRequestFailed,
AltcraftPush.moduleEvents.unsubscribeRequestFailed,
];
AltcraftCore.subscribeToEvents((event) => {
if (!responseMessages.includes(event.message)) return;
const raw = event.value?.response_with_http_code as
| Record<string, unknown>
| undefined;
if (raw == null) return;
const httpCode = raw.http_code ?? raw.httpCode;
const response = raw.response as Record<string, unknown> | null;
const profile = response?.profile as Record<string, unknown> | null;
const subscription = profile?.subscription as Record<string, unknown> | null;
console.log('httpCode=', httpCode, 'error=', response?.error);
console.log('subscriptionId=', subscription?.subscription_id);
});
profileFields: Record<string, unknown> | null
Default: null
Required: No
Description: Object containing profile fields.
The parameter accepts both system fields (for example, _fname for the first name or _lname for the last name) and optional fields (created manually in the platform interface in advance). Allowed structures (JSON-compatible):
- Scalar values:
string,boolean,number,null - Objects:
Record<string, unknown> - Arrays:
unknown[]
If an invalid optional field is passed, the request fails:
failed request: push/subscribe
http code: 400
error: 400
error_text: Platform profile processing error: with field "field_name": Incorrect field
AltcraftSDK.pushSubscribe({
sync: true,
profileFields: {
_fname: 'Ivan',
_lname: 'Petrov',
_email: 'ivan.petrov@example.com',
},
});
customFields: Record<string, unknown> | null
Default: null
Required: No
Description: Object containing subscription fields.
The parameter accepts both system fields (for example, _device_model for the device model or _os for the operating system) and optional fields (created manually in the platform interface in advance). Allowed value types (JSON-compatible, scalars only):
stringbooleannumbernull
If an invalid optional field is passed, the request fails:
failed request: push/subscribe
http code: 400
error: 400
error_text: Platform profile processing error: field "field_name" is not valid: failed convert custom field
AltcraftSDK.pushSubscribe({
customFields: {
source: 'rn_app',
build: '100',
},
});
Most system subscription fields are collected by the SDK automatically and added to push requests. These system fields are: "_os", "_os_tz", "_os_language", "_device_type", "_device_model", "_device_name", "_os_ver", "_ad_track", "_ad_id".
cats: CategoryData[] | null
Default: null
Required: No
Description: Subscription categories.
The category structure is defined by the CategoryData type:
type CategoryData = {
name?: string | null;
title?: string | null;
steady?: boolean | null;
active?: boolean | null;
};
When sending a push request with categories, use only the name and active fields. The title and steady fields are not used in request processing — they are filled when subscription information is received.
AltcraftSDK.pushSubscribe({
cats: [
{ name: 'developer_news', active: true },
{ name: 'product_updates', active: false },
],
});
The categories used in the request must be created and added to the resource in the Altcraft Platform in advance. If the request uses a category that is not added to the resource, the request fails:
failed request: push/subscribe
http code: 400
error: 400
error_text: Platform profile processing error: field "subscriptions.cats" is not valid: category not found in resource
replace: boolean | null
Default: null
Required: No
Description: When the flag is enabled, all subscriptions of other profiles with the same push token in the current database are set to the unsubscribed status after a successful request.
skipTriggers: boolean | null
Default: null
Required: No
Description: When the flag is enabled, the profile that contains this subscription is ignored in mailing and scenario triggers.
Request implementation examples
Example of a push subscription request
Minimal working configuration:
AltcraftSDK.pushSubscribe();
Passing all available parameters:
AltcraftSDK.pushSubscribe({
sync: true,
profileFields: { _fname: 'Ivan', _lname: 'Petrov' },
customFields: { source: 'rn_app' },
cats: [{ name: 'developer_news', active: true }],
replace: false,
skipTriggers: false,
});
For pushSubscribe, pushSuspend and pushUnSubscribe the SDK retries the request automatically when the HTTP response code is in the 500..599 range. The request is not retried when the response code is outside this range. When the retry limit is exhausted, an event of the retryError type arrives with the message push subscribe retry limit, and the request is removed from the local queue.
The unSuspendPushSubscription() function
The static unSuspendPushSubscription(): Promise<ResponseWithHttpCode | null> function is intended for LogIn and LogOut transitions. It works as follows:
- searches for subscriptions with the same push token as the current one that do not belong to the profile the current JWT token points to;
- changes the status of the found subscriptions from
subscribedtosuspended; - changes the status in the subscriptions of the profile the current JWT points to from
suspendedtosubscribed(if the profile the JWT points to exists and contains subscriptions); - returns
ResponseWithHttpCode | null, whereresponse.profileis the current profile the JWT points to (if the profile does not exist,nullis returned).
Recommended implementation of LogIn and LogOut transitions
LogIn transition
- An anonymous user opens the application. This user is assigned
JWT_1, which points to the #1Anonymous database; - A push subscription is created and the profile is created in the #1Anonymous database;
- The user signs up and is assigned
JWT_2, which points to the #2Registered database; - The
unSuspendPushSubscription()function is called: the anonymous user subscription in the #1Anonymous database is suspended; - A profile search is performed in the #2Registered database to restore the subscription;
- Since no subscription with this push token exists in the #2Registered database, the function returns
null; - After receiving
null, you can callpushSubscribe(), which creates a new profile in the #2Registered database.
LogOut transition
- The user logged out of the profile on the application side (
logOut()); - The user is assigned
JWT_1, which points to the #1Anonymous database; - The
unSuspendPushSubscription()function is called: it suspends the subscription in the #2Registered database and changes the subscription status in the #1Anonymous database tosubscribed; - The request returns the #1Anonymous != null profile: the subscription already exists, a new one is not required.
Implementation example:
import { AltcraftSDK } from '@altcraft/react-native-sdk';
async function unSuspend(logIn: boolean) {
if (logIn) {
AltcraftSDK.authenticate();
} else {
AltcraftSDK.logOut();
}
const result = await AltcraftSDK.unSuspendPushSubscription();
if (result == null) {
AltcraftSDK.pushSubscribe();
return;
}
const subscription = result.response?.profile?.subscription ?? null;
if (result.httpCode === 200 && subscription == null) {
AltcraftSDK.pushSubscribe();
}
}
Requesting the subscription status
AltcraftSDK