Amplify CLI Notifications Plugin
The `@aws-amplify/amplify-category-notifications` package is a core plugin for the AWS Amplify Command Line Interface (CLI), enabling developers to manage notification channels, primarily through Amazon Pinpoint. It allows for the addition, removal, updating, and status checking of notification channels directly from the command line, and provides a shortcut to the Pinpoint console for detailed configuration. This package is part of the larger Amplify CLI monorepo, with its latest stable version being `2.26.42`. It receives frequent updates, typically alongside the rapid release cadence of the Amplify CLI itself, ensuring compatibility with the latest AWS services and addressing security concerns. Its key differentiator is its deep integration into the Amplify development workflow, abstracting complex AWS Pinpoint configurations into simple CLI commands. This package is primarily for internal use by the Amplify CLI and for advanced users extending the CLI functionality.
Common errors
-
Error: Amplify CLI is not installed or configured correctly.
cause The Amplify CLI is not globally installed or is not in the system's PATH, preventing the `amplify` command from being found.fixInstall the Amplify CLI globally: `npm install -g @aws-amplify/cli` or `yarn global add @aws-amplify/cli`. Ensure your system's PATH includes the global npm/yarn bin directory. -
The push notification channel to enable. (Use arrow keys)
cause This is a prompt from `amplify add notifications` indicating that an interactive selection for the notification channel (APNS, FCM) is required, but it's not being provided in a non-interactive context.fixRun `amplify add notifications` interactively, or provide options using flags for non-interactive execution, e.g., `amplify add notifications --channel APNS` and include necessary certificate/key paths using `--json-file-path` or `--p12-file-path` flags. -
AccessDeniedException: User: arn:aws:iam::ACCOUNT_ID:user/IAM_USER is not authorized to perform: mobiletargeting:UpdateApnsChannel
cause The AWS IAM user or role configured for Amplify CLI lacks the necessary permissions to manage Amazon Pinpoint notification channels.fixEnsure the IAM user/role used by the Amplify CLI (configured via `amplify configure`) has appropriate `mobiletargeting` permissions, including `UpdateApnsChannel`, `UpdateGcmChannel`, `GetChannel` actions for Amazon Pinpoint. Review and update the IAM policy attached to your Amplify CLI user/role. -
TypeError: Cannot read properties of undefined (reading 'onNotification')
cause This error typically occurs in client-side application code when attempting to use `@aws-amplify/pushnotification` without proper configuration or initialization, or if `Amplify.configure()` has not been called.fixEnsure `aws-amplify` and `@aws-amplify/pushnotification` are installed, imported, and `Amplify.configure(awsconfig)` is called early in your application's lifecycle, using the `aws-exports.js` file generated by the Amplify CLI. Also, confirm the push notifications category is correctly added to your backend via `amplify add notifications` and `amplify push`.
Warnings
- gotcha This package, `@aws-amplify/amplify-category-notifications`, is a plugin for the AWS Amplify CLI and is primarily intended for internal use by the CLI or for advanced users extending CLI functionality. Direct programmatic import into typical application code is not the standard use case. For application-level push notifications, use `@aws-amplify/notifications` or `@aws-amplify/pushnotification` from the main `aws-amplify` library.
- breaking A significant refactoring related to AWS SDK v3 migration was attempted and then reverted in Amplify CLI `v14.2.0` (which includes `amplify-category-notifications@2.26.38`). This indicates that future major versions of Amplify CLI will likely introduce breaking changes as the AWS SDK v3 migration is completed. Users should anticipate breaking changes related to AWS SDK interfaces and potentially the structure of how AWS service calls are made internally.
- gotcha When configuring push notifications, especially for iOS (APNS), the Amplify CLI requires paths to `.p12` certificate files or service account keys (for FCM). Incorrect paths or malformed certificate/key files are a common source of setup failures.
- breaking Security fixes were released in `v2.26.42` for handling errant certificate marks and unsanitized input in PEM file content. Older versions may be vulnerable to issues related to certificate parsing.
- gotcha Adding the notifications category via `amplify add notifications` automatically adds the Auth category and configures Amazon Cognito User Pools, which is required for authenticated access to Amazon Pinpoint endpoints. Modifications to Cognito after initial setup can impact notification functionality.
Install
-
npm install amplify-category-notifications -
yarn add amplify-category-notifications -
pnpm add amplify-category-notifications
Imports
- add
import { add } from '@aws-amplify/amplify-category-notifications'; - update
import { update } from '@aws-amplify/amplify-category-notifications'; - NotificationChannel
import { NotificationChannel } from '@aws-amplify/amplify-category-notifications';
Quickstart
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
async function setupAmplifyNotifications() {
try {
console.log('Initializing Amplify project (if not already initialized)...');
// Ensure Amplify project is initialized. This is a prerequisite.
await execAsync('amplify init --yes');
console.log('Adding notifications category via Amplify CLI...');
// This simulates running `amplify add notifications` interactively.
// In a real script, you'd handle prompts or use headless mode.
const { stdout, stderr } = await execAsync(
`amplify add notifications --channel APNS --json-file-path './path/to/your-apns-key.json' --no-override`,
{
env: {
...process.env,
// Example: Provide environment variables for non-interactive prompts if available
// AMH_APP_ID: process.env.AMH_APP_ID ?? '',
// AMH_REGION: process.env.AMH_REGION ?? ''
}
}
);
console.log('Amplify CLI Output (add notifications):', stdout);
if (stderr) console.error('Amplify CLI Error (add notifications):', stderr);
console.log('Pushing Amplify backend changes...');
await execAsync('amplify push --yes');
console.log('Amplify notifications setup complete. Review `amplify status` for details.');
} catch (error) {
console.error('Failed to set up Amplify notifications:', error);
process.exit(1);
}
}
// For an actual application, you'd then use @aws-amplify/pushnotification in your client-side code.
// import Amplify from 'aws-amplify';
// import PushNotification from '@aws-amplify/pushnotification';
// import awsconfig from './aws-exports';
// Amplify.configure(awsconfig);
// PushNotification.onNotification(notification => {
// console.log('Notification received:', notification);
// });
setupAmplifyNotifications();