User data

Tereza Juric edited this page Apr 13, 2018 · 55 revisions

You can provide additional user's data to the server, so that you will be able to send personalised targeted messages to exact user and other nice features. Library supports a set of predefined attributes as well as custom ones.

Predefined user data

It is possible to set following user details:

Notice

The User Data API is strict about data types and formatting. It is not allowed to change the type of an existing custom user data attribute. It is not allowed to provide invalid value format for some predefined attributes (i.e. email and MSISDN). Be ready to gracefully handle any argument error (the ones that have messageId attribute of value 9). However, the Mobile Messaging SDK is responsible for saving your user's data to the server and you don't have to worry about retrying in case of network connectivity issues (i.e. connection problems, server errors etc.).

Updating predefined user data

In order to set values to multiple user's attributes you set the values and call the save() method:

// Swift
MobileMessaging.currentUser?.firstName = "John"
MobileMessaging.currentUser?.middleName = "Jeffrey"
MobileMessaging.currentUser?.lastName = "Smith"
MobileMessaging.currentUser?.gender = UserGender.Male
MobileMessaging.currentUser?.birthdate = Date()
MobileMessaging.currentUser?.msisdn = "79091234567"
MobileMessaging.currentUser?.email = "user@mail.com"
MobileMessaging.currentUser?.save()
// Objective-C
[MobileMessaging currentUser].firstName = "John";
[MobileMessaging currentUser].middleName = "Jeffrey";
[MobileMessaging currentUser].lastName = "Smith";
[MobileMessaging currentUser].gender = UserGenderMale;
[MobileMessaging currentUser].birthdate = [NSDate date];
[MobileMessaging currentUser].msisdn = "79091234567";
[MobileMessaging currentUser].email = "user@mail.com";
[[MobileMessaging currentUser] save: nil];

For a single attribute, use the existing API:

// Swift
MobileMessaging.currentUser?.save(
	msisdn: <#for example "79091234567"#>,
	completion: { error in
		<#handle the error if needed#>
	}
)
// Objective-C
[[MobileMessaging currentUser] saveWithMsisdn: <#for example @"79091234567"#>
	completion: ^(NSError * _Nullable error) {
		<#handle the error if needed#>
	}
];

Fetching user data from the server

In order to fetch user data from the server you use the fetchFromServer(completion:) method. It fetches the data asynchronously and performs the completion block after finished. For example:

// Swift
MobileMessaging.currentUser?.fetchFromServer(
	{ error in
		<#handle the error if needed#>
		// if no error uccured, the MobileMessaging.currentUser object has been fetched/updated at this point
	}
)
// Objective-C
[[MobileMessaging currentUser] fetchFromServer: ^(NSError * _Nullable error)
	{
		<#handle the error if needed#>
		// if no error uccured, the MobileMessaging.currentUser object has been fetched/updated at this point
	}
];

External user ID

You can use an external user ID to uniquely target your user. External user ID is meant to be an ID of a user in an external (non-Infobip) service. It can be any string and can be shared across different instances of your app, so that a user with multiple devices can still be targeted as a single user of your app.

In order to set value not only to external user ID, but also to other user's attributes set the values and call the save() method:

// Swift
MobileMessaging.currentUser?.externalId = <#for example "123123123123"#>
MobileMessaging.currentUser?.save()
// Objective-C
[MobileMessaging currentUser].externalId = <#for example @"123123123123"#>;
[[MobileMessaging currentUser] save: nil];

For a single attribute, use either the existing API:

// Swift
MobileMessaging.currentUser?.save(
	externalId: <#for example "123123123123"#>,
	completion: { error in
		<#handle the error if needed#>
	}
)
// Objective-C
[[MobileMessaging currentUser] saveExternalId:<#for example @"123123123123"#>
	completion:^(NSError * _Nullable error) {
		<#handle the error if needed#>
	}
];

Custom user data

It is possible to set custom attributes as part of user data. The custom attributes may be of following types:

  • String (or NSString)
  • Double (or double)
  • Int (or NSInteger)
  • Date (or NSDate)
  • NSNull (use this type to remove the attribute)

In order to represent the custom attribute value, use CustomUserDataValue class, which wraps the aforementioned types. For example:

// Swift
MobileMessaging.currentUser?.set(customData: CustomUserDataValue(string: "pizza"), forKey: "favouriteMeal")
MobileMessaging.currentUser?.set(customData: CustomUserDataValue(double: 9), forKey: "bootSize")
MobileMessaging.currentUser?.set(customData: CustomUserDataValue(date: NSDate()), forKey: "installationDate")
MobileMessaging.currentUser?.save()
// Objective-C
CustomUserDataValue * pizza = [[CustomUserDataValue alloc] initWithString:@"pizza"];
[[MobileMessaging currentUser] setWithCustomData:pizza forKey:@"favouriteMeal"];

CustomUserDataValue * bootsize = [[CustomUserDataValue alloc] initWithDouble:9.5];
[[MobileMessaging currentUser] setWithCustomData:bootsize forKey:@"bootSize"];

CustomUserDataValue * installationDate = [[CustomUserDataValue alloc] initWithDate:[NSDate date]];
[[MobileMessaging currentUser] setWithCustomData:installationDate forKey:@"installationDate"];

[[MobileMessaging currentUser] save: nil];

and for a single attribute:

// Swift
MobileMessaging.currentUser?.save(
	customData: CustomUserDataValue(string: "pizza"),
	forKey: "favouriteMeal",
	completion: { error in
		<#handle the error if needed#>
	}
)
// Objective-C
CustomUserDataValue * pizza = [[CustomUserDataValue alloc] initWithString:@"pizza"];
[[MobileMessaging currentUser] saveWithCustomData:pizza
	forKey:@"favouriteMeal"
	completion:^(NSError * _Nullable error) {
		<#handle the error if needed#>
	}
];

Using custom user data for tagging and subscriptions

You can utilise this data to add support for tags:

// Swift
let tags = CustomUserDataValue(string: "tags")

MobileMessaging.currentUser?.set(customData: tags, forKey: "funny")
MobileMessaging.currentUser?.set(customData: tags, forKey: "cool")

MobileMessaging.currentUser?.save()
// Objective-C
CustomUserDataValue * tags = [[CustomUserDataValue alloc] initWithString:@"tags"];
	
[[MobileMessaging currentUser] setWithCustomData:tags forKey:@"funny"];
[[MobileMessaging currentUser] setWithCustomData:tags forKey:@"cool"];

[[MobileMessaging currentUser] save:nil];

...and subscriptions:

// Swift
let subscriptions = CustomUserDataValue(string: "subscriptions")

MobileMessaging.currentUser?.set(customData: subscriptions, forKey: "technology")
MobileMessaging.currentUser?.set(customData: subscriptions, forKey: "humour")

MobileMessaging.currentUser?.save()
// Objective-C
CustomUserDataValue * subscriptions = [[CustomUserDataValue alloc] initWithString:@"subscriptions"];

[[MobileMessaging currentUser] setWithCustomData:subscriptions forKey:@"technology"];
[[MobileMessaging currentUser] setWithCustomData:subscriptions forKey:@"humour"];

[[MobileMessaging currentUser] save:nil];

Then use Segments to send notifications to users who are subscribed to a desired channel. Example for "technology":

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.