push technology

How To Generate an APNs Key (in 6 Simple Steps)

Angela Stringfellow Last updated on February 10, 2025

Key Takeaways

  • APNs Keys authenticate app servers for sending push notifications.
  • Proper key management is crucial for app security and functionality.
  • Developers must follow Apple's guidelines when implementing APNs.

APNs keys play a vital role in enabling push notifications for iOS apps. They allow developers to securely send messages to Apple devices through the Apple Push Notification service (APNs). An APNs key is a cryptographic token that authenticates your app server with Apple's servers, ensuring only authorized notifications reach users' devices.

Setting up APNs keys can seem complex at first, but it's an essential step for iOS developers who want to engage users with timely updates. This article will guide you through the process and help you understand the importance of APNs keys in mobile app development.

In this article:

What Is an APNs Key?

Person using a smartphone receiving push notifications

An APNS Key is a unique identifier that allows an app's server to connect with Apple's push notification service. It acts as a digital signature, proving that the server has permission to send notifications to the app's users. Without a valid APNS Key, an app cannot receive push notifications, which are crucial for user engagement and app functionality.

There are two types of APNs keys:

  1. Development keys
  2. Production keys

Development keys are used for testing, while production keys are for live apps.

Overview of Apple Push Notification Service

APNs stands for Apple Push Notification service. It's a system that lets app developers send notifications to iOS devices. It allows developers to send messages to users even when their apps aren't running. These notifications can include alerts, sounds, and badge updates.

APNs works by maintaining a constant connection between Apple's servers and iOS devices. When a notification is sent, it goes through Apple's servers and then to the user's device.

Push notifications can be used for various purposes:

  • News updates
  • Message alerts
  • Game invites
  • Reminders

They help keep users engaged with apps and provide timely information.

How To Generate an APNs Key

Developer generating an APNs key

APNs keys are essential for secure communication between your app and Apple's push notification servers. They provide a modern, more flexible alternative to traditional certificates. Proper configuration and management of APNS keys are crucial for implementing push notifications in iOS apps.

Step 1: Access Your Apple Developer Account

To create an APNS Key, developers must log into their Apple Developer account. This account is the central hub for managing all aspects of iOS app development.

Step 2: Navigate to Certificates, Identifiers & Profiles

In the developer portal, find the section for Certificates, Identifiers & Profiles. This area contains all the necessary tools for creating and managing app-related credentials.

Step 3: Create a New Key

Click on "Keys" in the sidebar and select the "+" button to add a new key. Choose "Apple Push Notifications service (APNs)" from the list of key services.

Step 4: Configure Key Settings

Name the key and select the app ID it will be associated with. This step ensures the key is linked to the correct application.

Step 5: Generate and Download

After confirming the settings, generate the key. After creating the key, you'll see its Key ID, and Apple will create a unique .p8 file.  Download this file immediately, as it can only be downloaded once for security reasons.

Step 6: Secure Your APNS Key File

Store the APNS key file in a secure location with limited access. Use encryption tools to protect the file when it's not in use. Implement strict access controls, allowing only authorized admins to handle the key.

Consider using a hardware security module (HSM) for added protection. HSMs provide a secure environment for storing sensitive cryptographic keys.

Integrating APNs Keys with Your Application

Apple Developer Notifications Console screenshot
Screenshot from Apple Developer

Integrating APNs keys into your app involves a few steps. First, add the key to your Xcode project by going to project settings and selecting your target.

Under the "Signing & Capabilities" tab, add the "Push Notifications" capability. This updates your app’s entitlements to support push notifications.

Next, generate a JSON Web Token (JWT) using your APNs key. This token authenticates your app with Apple's servers.

Include the following in your JWT:

  • Key ID
  • Team ID (from your developer account)
  • Bundle ID of your app

Use libraries like jwt-simple to create the token. Sign it with your private key from the downloaded key file.

Sending Push Notifications via APNs

Once your app is configured with APNs authentication, you can send push notifications by following these steps:

  1. Create a push notification payload – A JSON object containing the message content and any custom data.
  2. Connect to Apple's servers using HTTP/2 – Authenticate using your APNs key and JWT.
  3. Send the payload – Include the device token of the intended recipient so Apple can route the notification.

Keep these best practices in mind when sending push notifications via APNs: 

  • Use topic-based notifications for improved targeting.
  • Implement error handling to manage failed deliveries.
  • Respect user preferences and local regulations to avoid spam complaints. 
  • For web push notifications, use a tool like webpushtest.com to test them before sending.

Best Practices for Key Storage and Usage

To manage existing keys, return to the Keys section in your developer account. Here you can view key details, edit names, or revoke keys if needed. Follow these best practices to store your APNs keys securely:

  • Regularly audit access logs to monitor who has interacted with the key file. This helps detect any unauthorized access attempts quickly.
  • Implement robust security measures for your APNs key. Use strong, unique passwords for any systems storing the key. Enable two-factor authentication for added security.
  • Rotate your APNs keys periodically to reduce the risk of compromise. Apple recommends changing keys at least once a year.
  • Never share your APNs key through unsecured channels like email. Use secure file transfer protocols when moving the key between systems.
  • Keep your server software up-to-date to patch any security vulnerabilities. Regularly review and update your key management policies to adapt to new threats.

Troubleshooting Common APNS Issues

Connection problems are a frequent issue. Check your internet connection and firewall settings. Then, verify that your APNS key is valid and not expired.

Invalid device tokens can cause delivery failures. Regularly update your database to remove outdated tokens.

If notifications aren't received, ensure the app has permission to display them. Then, check the user's notification settings on their device.

Payload issues may prevent delivery. Verify your JSON format is correct and within size limits.

Lastly, monitor Apple's APNS status page for any system-wide problems affecting notification delivery. Also, use Apple's feedback service to identify and remove invalid device tokens, improving overall delivery rates.

Simplify Push Notifications with MagicBell

APNs keys play a vital role in enabling push notifications for iOS apps. They provide secure authentication between app servers and Apple's push notification service.

APNs keys unlock powerful capabilities for engaging users through timely notifications. They allow apps to send targeted messages and updates.

Developers looking to implement push notifications can benefit from push notification services. These simplify the process of managing keys and sending notifications at scale.

MagicBell handles the complexities of notification delivery across platforms, allowing you to deliver seamless, real-time notifications across channels. Sign up for your free MagicBell account today to get started. 

Frequently Asked Questions

What is the difference between APNs certificates and APNs keys?

APNs certificates are older and require yearly renewal. On the other hand, APNs keys are newer, don't expire, and can be used across multiple apps. Keys are easier to manage and more secure than certificates.

What are the steps to retrieve an APNs authentication key?

APNs keys can only be downloaded once when created. If you lose the key, you'll need to create a new one. Always store the key securely after downloading it from the Apple Developer portal.

How can I find my existing APNs key in the Apple Developer portal?

Log into the Apple Developer portal and go to Certificates, Identifiers & Profiles. Then, click on Keys in the sidebar. You'll see a list of your existing keys, including any APNs keys you've created.

Is there a limit to the number of APNs keys I can generate?

Apple allows developers to create up to two active APNs keys per account. If you need a new key and have reached the limit, you'll need to revoke an existing key first.

How do I obtain an APN token for sending notifications?

To get an APN token, use your APNs key to sign a JSON Web Token (JWT). This JWT serves as your authentication token when connecting to Apple's push notification service. The token is valid for one hour.