Push Notifications
Register the device with Segmentify and report views and clicks of push campaigns.
Segmentify sends push campaigns through Firebase Cloud Messaging (FCM) or APNs. Your app receives and displays the notifications; the SDK registers the device and reports what happens to each notification.
Set up
Section titled “Set up”Set the push data center next to config:
SegmentifyManager.config(appkey: "YOUR_API_KEY", dataCenterUrl: "https://gandalf-eu.segmentify.com", subDomain: "www.yourdomain.com")SegmentifyManager.setPushConfig(dataCenterUrlPush: "https://gimli-eu.segmentify.com") // your push data center URLWithout it the push methods print an error and send nothing.
Enable the Push Notifications capability for your target, request the user’s permission with UNUserNotificationCenter, and call registerForRemoteNotifications(). If you deliver through Firebase, add Firebase Messaging as described in the Firebase documentation. The SDK does not include Firebase.
Register the device token
Section titled “Register the device token”Send the token to Segmentify whenever you receive or refresh it.
// Firebase: MessagingDelegatefunc messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) { guard let fcmToken else { return } let model = NotificationModel() model.type = NotificationType.PERMISSION_INFO model.providerType = ProviderType.FIREBASE // use ProviderType.APNS with a raw APNs token model.deviceToken = fcmToken SegmentifyManager.sharedManager().sendNotification(segmentifyObject: model)}With APNs directly, send the hex string of the device token from application(_:didRegisterForRemoteNotificationsWithDeviceToken:) and set providerType to .APNS.
Without a registered token the user cannot be targeted by push campaigns.
Report views and clicks
Section titled “Report views and clicks”Build a SegmentifyPushPayload from the incoming message and hand it to the SDK.
extension AppDelegate: UNUserNotificationCenterDelegate { // shown while the app is in the foreground func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void ) { let payload = SegmentifyPushPayload(userInfo: notification.request.content.userInfo) SegmentifyManager.sharedManager().handlePushReceived(payload) completionHandler([.banner, .sound, .badge]) }
// the user tapped the notification func userNotificationCenter( _ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void ) { let payload = SegmentifyPushPayload(userInfo: response.notification.request.content.userInfo) SegmentifyManager.sharedManager().handlePushClicked(payload) if let target = payload.targetUrl, let url = URL(string: target) { DispatchQueue.main.async { UIApplication.shared.open(url) } } completionHandler() }}Set the delegate at launch: UNUserNotificationCenter.current().delegate = self.
handlePushReceived reports a view and handlePushClicked reports a click. Both need the payload’s instanceId; a message without one is not a Segmentify campaign and is ignored.
The payload
Section titled “The payload”| Property | Type | Description |
|---|---|---|
title, message |
String |
Text of the notification. Empty when not sent. |
image |
String? |
Image URL. |
deeplink, redirectUrl |
String? |
Where a tap should lead. |
targetUrl |
String? |
deeplink if present, otherwise redirectUrl. |
instanceId |
String? |
ID of the push campaign. Needed to report views and clicks. |
tag |
String? |
Notification tag. |
actions |
[SegmentifyPushAction] |
Action buttons, each with action, title, icon, redirectUrl, deeplink. |
providerType |
ProviderType |
.FIREBASE or .APNS, detected from the message. |
isV2 |
Bool |
Whether the message uses the current payload format. |
Attribute what happens next
Section titled “Attribute what happens next”After a click, the SDK remembers the campaign so you can attribute the visit that follows:
let utm = SegmentifyManager.sharedManager().getTrackingParameters()// utm.utm_source, utm.utm_medium, utm.utm_campaign, utm.utm_contentAdd these to the deep link or to your own analytics events if you track campaign sources.
Checklist
Section titled “Checklist”- The push data center URL is set.
- The token is sent on first launch and on every refresh.
- Views are reported when a notification arrives, and clicks when it is tapped.
- The notification opens
targetUrlwhen one is present.