Page updated Nov 8, 2023

Identify user to Amazon Pinpoint

This call identifies the current user (which could be unauthenticated or authenticated) to Amazon Pinpoint. The user ID can be any string which identifies the user in the context of your application.

Get the user ID from Amplify Auth

If the user is signed in through Amplify.Auth.signIn, then you can retrieve the current user's ID as shown below:

1Amplify.Auth.getCurrentUser(
2 authUser -> String userId = authUser.getUserId(),
3 error -> Log.e("MyAmplifyApp", "Error getting current user", error)
4);
1Amplify.Auth.getCurrentUser(
2 { authUser -> val userId = authUser.userId },
3 { Log.e("MyAmplifyApp", "Error getting current user", it) }
4)
1try {
2 val userId = Amplify.Auth.getCurrentUser().userId
3} catch (error: PushNotificationsException) {
4 Log.e("MyAmplifyApp", "Error getting current user", error)
5}
1RxAmplify.Auth.getCurrentUser().subscribe(
2 authUser -> String userId = authUser.getUserId(),
3 error -> Log.e("MyAmplifyApp", "Error getting current user", error)
4);

Identify the user to Amazon Pinpoint

Once you have a string that identifies the current user (either from the Auth category as shown above or through your own application logic), you can identify the user to Amazon Pinpoint with the following:

1Amplify.Notifications.Push.identifyUser(user,
2 () -> Log.i("MyAmplifyApp", "Identified user successfully"),
3 error -> Log.e("MyAmplifyApp", "Error identifying user", error)
4);
1Amplify.Notifications.Push.identifyUser(user,
2 { Log.i("MyAmplifyApp", "Identified user successfully") },
3 { Log.e("MyAmplifyApp", "Error identifying user", error) }
4)
1try {
2 Amplify.Notifications.Push.identifyUser(user)
3 Log.i("MyAmplifyApp", "Identified user successfully")
4} catch (error: PushNotificationsException) {
5 Log.e("MyAmplifyApp", "Error identifying user", error)
6}
1RxAmplify.Notifications.Push.identifyuser(user).subscribe(
2 () -> Log.i("MyAmplifyApp", "Identified user successfully"),
3 error -> Log.e("MyAmplifyApp", "Error identifyUser user", error)
4);

Alternatively, you can provide additional information about the user by passing a user profile:

1AWSNotificationsUserProfile profile = AWSNotificationsUserProfile.builder()
2 .name("Name")
3 .build()
4
5Amplify.Analytics.identifyUser(
6 user,
7 profile,
8 () -> Log.i("MyAmplifyApp", "Identified user successfully"),
9 error -> Log.e("MyAmplifyApp", "Error identifying user", error)
10);
1val profile = AWSNotificationsUserProfile.builder()
2 .name("Name")
3 .build()
4
5Amplify.Analytics.identifyUser(
6 user,
7 profile,
8 { Log.i("MyAmplifyApp", "Identified user successfully") },
9 { Log.e("MyAmplifyApp", "Error identifying user", error) }
10)
1val profile = AWSNotificationsUserProfile.builder()
2 .name("Name")
3 .build()
4
5try {
6 Amplify.Notifications.Push.identifyUser(user, profile)
7 Log.i("MyAmplifyApp", "Identified user successfully")
8} catch (error: PushNotificationsException) {
9 Log.e("MyAmplifyApp", "Error identifying user", error)
10}
1AWSNotificationsUserProfile profile = AWSNotificationsUserProfile.builder()
2 .name("Name")
3 .build()
4
5RxAmplify.Notifications.Push.identifyuser(user, profile).subscribe(
6 () -> Log.i("MyAmplifyApp", "Identified user successfully"),
7 error -> Log.e("MyAmplifyApp", "Error identifyUser user", error)
8);