Capture Push Metrics
UpdatedIf you’ve already set up rich push capabilities with the Flutter SDK, you’re ready to go. For more advanced use cases, see this document.
Upgrade iOS SDK!
Beginning in version 2.11 of the native iOS SDK, the SDK automatically handles push notifications from Customer.io and tracks opened
and delivered
metrics for you. We recommend that you upgrade to simplify your code!
Automatic push handling
Customer.io supports device-side metrics that help you determine the efficacy of your push notifications: delivered
when a push notification is received by the app and opened
when a push notification is clicked.
Beginning in version 2.11
of our native iOS SDK, the SDK automatically tracks opened
and delivered
events for push notifications originating from Customer.io after you configure your app to receive push notifications. No more code is required for your app to track opened
push metrics or launch deep links!
Do you use multiple push services in your app?
The Customer.io SDK only handles push notifications that originate from Customer.io. Push notifications that were sent from other push services or displayed locally on device are not handled by the Customer.io SDK. You must add custom handling logic to your app to handle those push events.
Read the sections below to see how you can add (optional) custom handling to various push events.
Choose whether to show push while your app is in the foreground
If your app is in the foreground and the device receives a Customer.io push notification, your app gets to choose whether or not to display the push.
To configure this behavior, add the following highlighted line of code in your AppDelegate.swift
file:
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
...
MessagingPushFCM.initialize { config in
config.showPushAppInForeground = true // `true` will display the push when app in foreground
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
If the push did not come from Customer.io, you’ll need to perform custom handling to determine whether to display the push or not.
Custom handling when users click a push
You might need to perform custom handling when a user clicks a push notification—like when you want to process custom fields in your push notification payload.
For now, the Flutter SDK does not provide callbacks when your audience clicks a push notification. But you can use one of the many popular Flutter push notification SDKs to receive a callback.
For example, the code below receives callbacks when users click a push using FlutterFire. Be sure to follow the documentation for the push notification SDK you choose to use to receive callbacks with.
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
FirebaseMessaging.instance.getInitialMessage().then((initialMessage) {
// Handle push notification that was clicked, when app was in the killed state
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
// Handle push notification that was clicked, when app was in the background state
});
Do you use deep links?
If you’re performing custom push click handling on push notifications originating from Customer.io, we recommend that you don’t launch a deep link URL yourself. Instead, let our SDK launch deep links to avoid unexpected behaviors.
Custom handling when getting a push while the app is foregrounded
If your app is in the foreground and you get a push notification, your app gets to choose whether or not to display the push. For push notifications originating from Customer.io, your SDK configuration determines if you show the notification. But you can add custom logic to your app when this kind of thing happens.
For now, the Flutter SDK does not provide callbacks when a push notification is received and your app is in the foreground. But you can use one of the many popular Flutter push notification SDKs to receive a callback.
For example, the code below receives a callback using FlutterFire. Be sure to follow the documentation for the push notification SDK you choose to use to receive callbacks with.
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
// Handle push notification received while app in foreground
});
Manually track push metrics
Avoid duplicate push metrics
If you manually track your own metrics, you should disable automatic push tracking to avoid duplicate push metrics.
You can manually parse a push notification payload and send opened
or delivered
events to the SDK:
const deliveryID = '123'; // extracted from payload received in the push notification.
const deviceToken = 'abc'; // extracted from payload received in the push notification
const event = MetricEvent.opened; // or MetricEvent.delivered
CustomerIO.trackMetric(
deliveryID: deliveryID, deviceToken: deviceToken, event: event);
Disabling automatic push tracking
After you set up push notifications in your app, modify your AppDelegate.swift
file to disable automatic push notification tracking:
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
CustomerIO.initialize(siteId: "YourSiteID", apiKey: "YourAPIKey", region: Region.US) { config in
config.autoTrackPushEvents = false
}
}