Set up push notifications

Updated

Our iOS SDK supports push notifications over APN or FCM. This page can help you get started with either service.

Before you begin

This page explains how to register for push notifications using our SDK. But, before you can send push notifications, you need to add your push service credentials to Customer.io. See our push service certificates to learn more.

If you haven’t already, you’ll need to install the push package for the push service you use—APNs or FCM—and enable the Push Notifications capability in XCode. See our quick start guide page for installation instructions.

Register for push notifications

The instructions in this section set you up to receive simple push notifications with a body and title. After you follow these instructions, you’ll need to do a bit more work to support rich push notifications.

The SDK automatically handles push registration and push clicks for you. However, you’ll still need to identify users before you can send them push notifications.

1. Initialize the push service

After you initialize the SDK, initialize the push service that you use in your app. Your code changes slightly depending on the push service you use.

import CioDataPipelines
import CioMessagingPushAPN
import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
 var storage = DIGraphShared.shared.storage

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

     var cdpApiKey = YOUR_CDP_API_KEY
     var siteId = YOUR_SITE_ID
     
     let config = SDKConfigBuilder(cdpApiKey: cdpApiKey)
         .autoTrackDeviceAttributes(true)
         .autoTrackUIKitScreenViews()
         .migrationSiteId(siteId)

     CustomerIO.initialize(withConfig: config.build())

     // Initialize messaging features after initializing Customer.io SDK        
     MessagingPushAPN.initialize(
         withConfig: MessagingPushConfigBuilder()
             // optionally, configure the push module by calling functions on the builder. Such as: 
             .autoFetchDeviceToken(true)
             // See section below to find all the configuration options you can set. 
             .build()
     )

     return true
 }
} 
import CioDataPipelines
import CioMessagingPushFCM
import FirebaseCore
import FirebaseMessaging
import Foundation
import UIKit

class AppDelegate: NSObject, UIApplicationDelegate {
 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
     // FCM provides a device token to the app that
     // you send to the Customer.io SDK.

     // Initialize the Firebase SDK.
     FirebaseApp.configure()

     let siteId = YOUR_SITE_ID
     let cdpApiKey = YOUR_CDP_API_KEY

     // Configure and initialize the Customer.io SDK
     let config = SDKConfigBuilder(cdpApiKey: cdpApiKey)
         .migrationSiteId(siteId)
         .autoTrackUIKitScreenViews()
         .autoTrackDeviceAttributes(true)
  
     CustomerIO.initialize(withConfig: config.build())

     // Initialize messaging features after initializing Customer.io SDK        
     MessagingPushFCM.initialize(
         withConfig: MessagingPushConfigBuilder()
             // optionally, configure the push module by calling functions on the builder. Such as: 
             .autoFetchDeviceToken(true)
             // See section below to find all the configuration options you can set. 
             .build()
     )

     return true
 }
}

2. Identify your audience

Identify the person if you have not already. Even after you add a device token, you can’t use it until you associate it with a person.

CustomerIO.shared.identify(userId: "989388339", traits: ["first_name": firstName]) 

When you identify a person, you should see their device token in your workspace. You can send a simple push notification to test your implementation.

Note that when you identify a different person or stop identifying a person, the SDK automatically removes the device token from any previously identified profile. This ensures that a device token is only registered to the currently identified profile in the SDK and prevents you from sending duplicate messages messaging the wrong person.

Push configuration options

When you initialize your preferred push package (CioMessagingPushAPN or CioMessagingPushFCM), you can pass configuration options determining how the push package functions.

OptionTypeDefaultDescription
autoFetchDeviceTokenbooleantrueWhen true, the package automatically fetches the device token for push notifications.
autoTrackPushEventsbooleantrueAutomatically track opened and delivered metrics based on push notifications.
showPushAppInForegroundbooleantrueShow push notifications when the app is in the foreground.
Copied to clipboard!
  Contents
Current release
 3.4.1
Is this page helpful?