Page updated Nov 14, 2023

Request permissions

Depending on your users' platform and operating system version, it is likely that you will need to request their permission to display push notifications.

To learn more about the platform-specific guidances for requesting permissions, you can visit the respective documentations for iOS and Android. To best aid you in giving your users a good permission experience with platform idiomatic flows, Amplify provides the functionality below.

Get permission status

The first step to request permissions from your user is to understand the current status of permissions. Your app may behave differently in response to these possible statuses below.

  • Should Request - No permissions have been requested yet. It is idiomatic at this time to simply request for permissions from the user.
  • Should Explain Then Request - It is recommended at this time to provide some context or rationale to the user explaining why you want to send them push notifications before requesting for permissions.
  • Granted - Permissions have been granted by the user. No further actions are needed and their app is ready to display notifications.
  • Denied - Permissions have been denied by the user. Further attempts to request permissions will no longer trigger a permission dialog. Your app should now either degrade gracefully or prompt your user to grant the permissions needed in their device settings.
1import {
2 getPermissionStatus,
3 GetPermissionStatusOutput
4} from 'aws-amplify/push-notifications';
5
6const status: GetPermissionStatusOutput = await getPermissionStatus();
7// 'shouldRequest' | 'shouldExplainThenRequest' | 'granted' | 'denied'
1import { getPermissionStatus } from 'aws-amplify/push-notifications';
2
3const status = await getPermissionStatus();
4// 'shouldRequest' | 'shouldExplainThenRequest' | 'granted' | 'denied'

Request permissions

Once you have determined if the current permission status requires you to request permissions from the user, you can call requestPermissions() to make that request.

Amplify requests all supported notification permissions by default. But you can also choose not to request specific permissions.

It is recommended that you specify these permissions if needed but it is important to note that they are ignored by Android

  • Alert: When set to true, requests the ability to display notifications to the user.
  • Sound: When set to true, requests the ability to play a sound in response to notifications.
  • Badge: When set to true, requests the ability to update the app's badge.
1import {
2 requestPermissions,
3 RequestPermissionsInput
4} from 'aws-amplify/push-notifications';
5
6const permissions: RequestPermissionsInput = {
7 // permissions are true by default
8 // alert: true
9 sound: false,
10 badge: false
11};
12
13const arePermissionsGranted = await requestPermissions(permissions);
14// true if granted (or already granted), false otherwise
1import { requestPermissions } from 'aws-amplify/push-notifications';
2
3const permissions = {
4 // permissions are true by default
5 // alert: true
6 sound: false,
7 badge: false
8};
9
10const arePermissionsGranted = await requestPermissions(permissions);
11// true if granted (or already granted), false otherwise

Sample permissions flow

Use getPermissionStatus() and requestPermissions() together to handle permission request flows. Below is a sample implementation of the expected logic.

1async function handlePermissions() {
2 const status = await getPermissionStatus();
3 if (status === 'granted') {
4 // no further action is required, user has already granted permissions
5 return;
6 }
7 if (status === 'denied') {
8 // further attempts to request permissions will no longer do anything
9 myFunctionToGracefullyDegradeMyApp();
10 return;
11 }
12 if (status === 'shouldRequest') {
13 // go ahead and request permissions from the user
14 await requestPermissions();
15 }
16 if (status === 'shouldExplainThenRequest') {
17 // you should display some explanation to your user before requesting permissions
18 await myFunctionExplainingPermissionsRequest();
19 // then request permissions
20 await requestPermissions();
21 }
22}