Page updated Nov 14, 2023

Receive a device token

Push notifications are delivered to your user's devices through a device token which uniquely identifies your app. Although Amplify will automatically register this token with Amazon Pinpoint, it can still be useful to have access to this token for your app's use cases (e.g. to send direct notifications to a specific device).

onTokenReceived

Add onTokenReceived listeners to respond to a token being received by your app.

A token will be received by your app:

  • On every app launch, including the first install
  • When a token changes (this may happen if the service invalidates the token for any reason)
1import {
2 onTokenReceived,
3 OnTokenReceivedInput,
4 OnTokenReceivedOutput
5} from 'aws-amplify/push-notifications';
6
7const myTokenReceivedHandler: OnTokenReceivedInput = (token) => {
8 // Do something with the received token
9};
10
11const listener: OnTokenReceivedOutput = onTokenReceived(myTokenReceivedHandler);
12
13listener.remove(); // Remember to remove the listener when it is no longer needed
1import { onTokenReceived } from 'aws-amplify/push-notifications';
2
3const myTokenReceivedHandler = (token) => {
4 // Do something with the received token
5};
6
7const listener = onTokenReceived(myTokenReceivedHandler);
8
9listener.remove(); // Remember to remove the listener when it is no longer needed