Remove a device
Use removeDevice to de-register the current device so that Amazon Connect journeys stop delivering notifications to it.
import { removeDevice } from 'aws-amplify/push-notifications/customer-profiles';
await removeDevice();removeDevice takes no arguments. The library resolves the device identifier for you, and your backend permits removal only of a device that the calling identity owns.
Sign a user out safely
Handle a failed removal deliberately rather than letting it block sign-out. The following pattern signs the user out even when de-registration fails, which avoids trapping a user in a signed-in state because of a network error:
import { signOut } from 'aws-amplify/auth';import { removeDevice } from 'aws-amplify/push-notifications/customer-profiles';
const handleSignOut = async () => { try { await removeDevice(); } catch (error) { console.error('Failed to remove device before sign out', error); }
await signOut();};When to call removeDevice
Call removeDevice in these situations:
- Before signing a user out, as described above.
- When a user turns off notifications in your application's settings.
- Before deleting a user's account.
You do not need to call removeDevice when the push token is replaced. Registration is keyed on a device identifier that is stable for the installation, so registering with a new token updates the existing record. See Register a device.
Register the device again
The device identifier persists after removal, so a user who opts back in can be registered again with registerDevice. Removing a device does not reset the identifier or require your application to store one.
Handle errors
removeDevice rejects in the following cases:
- Push notifications are not initialized. Call
initializePushNotificationsbefore removing a device. - The request failed. The endpoint returned a non-success status, or the request could not complete.