Skip to main content

Push Notifications

Push notifications are a powerful way to re-engage users, but they only produce useful analytics if every leg of the journey is tracked: the token must be registered against the right user, deliveries must be linked back to campaigns, and opens must be attributed to whichever notification triggered them. The SDK provides two methods that, together, cover the full push lifecycle.

The SDK does not generate push tokens itself. Token generation is the responsibility of the underlying provider — Firebase Cloud Messaging (FCM) on Android, Apple Push Notification Service (APNS) on iOS, or Huawei Mobile Services (HMS) on Huawei devices without Google Play. Once your application has obtained a token from one of these providers, you pass it to the SDK along with the provider type, and the SDK takes care of the rest.

Register a Token

Call registerPushToken() as soon as you have a token. This typically happens shortly after the user grants notification permission and the provider returns its first token. You should also call this method whenever the token refreshes — providers may rotate tokens at any time, and the SDK assumes the most recent value you supplied is the current one.

B2Metric.instance.registerPushToken(token, PushProvider.fcm);

The PushProvider enum has three values that map directly to the supported providers: PushProvider.fcm for Firebase Cloud Messaging, PushProvider.hms for Huawei Mobile Services, and PushProvider.apns for Apple Push Notification Service. Pick the one that matches the platform you obtained the token from.

Track Push Open

When a user taps a push notification and lands inside your application, call trackPushOpened() to mark the open. You can include any metadata you want to attribute the open to a specific campaign, audience segment, or deep link target. Common keys include campaign_id, notification_id, and deep_link, but the structure is entirely yours to define — pass whatever your campaign system already tracks.

B2Metric.instance.trackPushOpened({
'campaign_id': 'summer_sale',
'deep_link': 'app://offers/42',
});
WHERE TO CALL THIS

The right place to call trackPushOpened() depends on how you handle deep links. If you have a central notification handler (a single function that processes every incoming notification payload), put the call there. If you route opens through your navigator, call it inside the route handler that fires when the app opens from a notification. Either way, call it exactly once per open — calling it multiple times will inflate your open counts.