Push Notifications
The SDK does not handle push notification delivery or display — that remains the responsibility of your existing notification stack (typically Firebase Messaging for FCM/HMS and APNS via React Native's own push APIs for iOS). What the SDK provides is the link between a device's push token and B2Metric, so that B2Metric campaigns can target the device, and a method to report when a notification leads to an app open.
6.1 Register a Push Token
After your notification library obtains a token from the provider, call registerPushToken with the token string and the provider name. The SDK stores the token on the device profile and uses it for future campaign delivery.
import messaging from '@react-native-firebase/messaging'
import { B2Metric } from '@b2metric/react-native-sdk'
const token = await messaging().getToken()
B2Metric.registerPushToken(token, 'fcm')
The provider argument is one of:
'fcm'— Firebase Cloud Messaging (Android and cross-platform).'hms'— Huawei Mobile Services (for Huawei devices without Google Services).'apns'— Apple Push Notification Service (iOS).
If the token is refreshed by the provider (which can happen on app reinstall, OS update, or token rotation), call registerPushToken again with the new value. The SDK will update the stored token and continue using the latest one.
6.2 Report a Push Open
When the user taps a notification and your notification handler opens the app, call trackPushOpened with the relevant attributes from the notification payload. Typical values include the campaign identifier, a deep link, or any custom data attached to the message.
B2Metric.trackPushOpened({
campaign_id: 'summer_sale',
deep_link: 'app://offers/42',
})
The argument is a flat string-to-string map. Pass whatever your campaign configuration includes — there is no fixed schema, but if your notification payload includes a campaign_id field, including it makes attribution reports significantly more useful.
The SDK does not request push permissions, register for remote notifications with the OS, or display a notification UI. Those steps must be handled by your notification library before B2Metric.registerPushToken is called. If the token is empty or invalid, the call is recorded but no campaigns will be delivered to that device.