Set the Contact Key

To set the contact key, add the appropriate request to your app. In these code examples, replace user@example.com with the contact key you want to use.

To send messages to anonymous users only, skip this step. The contact key for an anonymous user is a random GUID.

Note

If you use version 10 or later of the SDK, use this code.

1SFMCSdk.requestSdk { sfmcSdk ->
2  // Set contact key/profileId for all modules
3  sfmcSdk.identity.edit {
4      profileId = "uniquelyIdentifyingKey"
5  }
6  // Retrieve the contact key
7  val profileId = sfmcSdk.identity.profileId
8}

For version 10 or later of the iOS SDK, use this code.

1// Set contact key for mobile push module
2SFMCSdk.identity.edit { [self] model in
3    model.profileId = "user@example.com"
4    return model
5}
6// Retrieve the contact key
7SFMCSdk.identity.get()?.profileId

For earlier versions of the Android SDK, use this code.

1SFMCSdk.requestSdk { sfmcSdk ->
2  // Set contact key for all modules
3  sfmcSdk.identity.setProfileId("uniquelyIdentifyingKey")
4  // OR set contact key only for the Mobile Push module
5  sfmcSdk.identity.setProfileId("uniquelyIdentifyingKey", ModuleIdentifier.PUSH)
6  // Retrieve the contact key
7  sfmcSdk.mp {
8    val contactKey = it.moduleIdentity.profileId
9  }
10}

For earlier versions of the iOS SDK, use this code.

1// Set contact key for all modules
2SFMCSdk.identity.setProfileId("user@example.com")
3// OR set contact key only for the Mobile Push module
4SFMCSdk.identity.setProfileId([ModuleName.push: "user@example.com"])
5// Retrieve the contact key
6SFMCSdk.requestPushSdk { mp in
7    let contactKey = mp.contactKey()
8}