Custom user ID

Batch can automatically tie several push tokens from different OS' to a single user ID. This allows you to use the Custom Data API. This is also useful if you are planning to use the Transactional API and don't want to manage devices' tokens on your side.

Custom user IDs

This custom user identifier can be:

  • The unique ID you are using in your login system.
  • A stable ID used in your data store (e.g. Firebase, etc).
  • Any stable information that can help you to identify a user (e.g. hashed email address, etc).

Pushing a custom user identifier will send a notifcation to every installation with that identifier set.

If you want to only push a specific installation (and thus a specific device), you should set a installation-specific identifier or use Batch installation ID.

Setting up a custom user ID

The API is straightforward:

  • Swift
  • Objective-C
// Get a new editor instance.
// You need to save this in a local variable until you call save
// Editor instances don't share changes, and calling save on an empty editor will do nothing
let editor = BatchUser.editor()
editor.setIdentifier("john.doe")
editor.save()

// This is how you retrieve your custom user identifier. Default value is nil.
let userId = BatchUser.identifier()

You should remove the identifier anytime the identified user logs out.

User identifiers are case and whitespace sensitive. If you want to avoid potential issues, you may want to call lowercaseString and stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] on the identifier string.

User identifiers must be shorter than 512 characters.

IMPORTANT
Make sure the identifier is unique, otherwise users might get pushes that are not meant for them. You should never associate a custom ID to more than one user.

Choosing a unique identifier

If you are wondering which ID you should use as a custom user identifier, here are a few examples:

Login system

If you have a login system, you probably have a unique user ID associated to the user's account. Simply set this ID as your user identifier, and don't forget to reset it (by setting it to nil) when the user logs out.

While email addresses are an appropriate identifier, we suggest you hash them before setting them as your identifier, for privacy reasons.

PaaS datastore

Your datastore probably has a stable ID associated to your installation. You just need to make sure it is unique between installations, and you are easily able to retreive it from your backend code.

Specific backend and database

Anything that can identify a user uniquely will work. Email address, phone number, etc. We suggest you hash this information before setting it, and use the same hash on your backend.

Otherwise, you will need to create a stable identifier for your app installation and send it to your backend. The most common way to do that is to generate a RFC4122 UUID.