Dec 7th, 2020

🛎 APNs Environments and Push Notification Delivery

Sending push notifications (or remote user notifications) to your app's users can help to deliver relevant information in real-time, without your app being in the foreground.

Once you enable push notifications for your application by adding the Push Notifications capability, you'll be able to request authorization, which prompts the user to decide whether they want to receive or deny notifications. They will only be asked once, after which notification preferences can only be changed through the system settings.

After permission is granted, your app can register for remote notifications, sending a request to the underlying system to communicate with the Apple Push Notification service (APNs), which is the service managing deliveries to your user's devices. If everything succeeds, you'll receive a device token, which uniquely identifies the app installation on the current device.

Keep in mind that your users might use multiple devices, each of which receives a different device token. These tokens might also change at any point in time, so be sure to register for remote notifications whenever you're certain that the user has enabled notifications, for example at every launch after the initial onboarding.

Having added the related capability earlier, you will also notice that APS Environment was added to your app entitlements. This is really important, so pay close attention. Based on your current provisioning profile, meaning the task you're currently working on, Xcode will set this entitlement to development when debugging and developing on your devices or production when distributing your application via the App Store or TestFlight.

The APS Environment entitlement tells your app whether to use the sandbox or production APNs environment, which are completely separate. Device tokens created for applications running in the development APS environment will only work for the sandbox, and tokens generated from the production environment may only be used for production.

This becomes important when you plan to send notifications to your users, as your backend has to match the environment your users are in. When you are developing or debugging in Xcode, you'll receive a development device token, which your backend will use to send notifications via the sandbox APNs environment.

In production, or for users who installed your app via the App Store or TestFlight, your device tokens are only valid for production.

If your application provides authentication, you should also make sure that once a user signs out the device token associated with their current session is removed in the backend, as you want to prevent sending notifications if they're not signed in anymore.

While you can theoretically build your own backend to manage sending notifications to APNs, which forwards them to your users, you can also use managed services such as AWS SNS. If you're interested in the latter, my good friend Tim wrote an excellent post on adding push notifications to your iOS application with AWS SNS.