iOS development: understanding of the remote push principle of Apple APNs

This article is an understanding of the principle of APNs push. I hope that after reading it, you can master a knowledge point.

APNsIt is Apple Push Notification Servicethe abbreviation of Apple's push server.
Please add image description
The delivery of remote notifications involves several key components:

  • Your company's server or a third-party service provider, called a provider server
  • Apple Push Notification Service (APNs)
  • user's device
  • Your app runs on the user's device

We want to implement a message push, which is roughly divided into two steps: registering for push and pushing the message .

Register for push

Please add image description

  1. When launching an APP, it usually requests push permission. When the user clicks Allow, the phone will send the device identification and push certificate to Apple. After APNs服务器Apple receives this information, it will verify whether it is based on the push certificate, device identification, and APP information. With push permission, if there is push permission, Apple will put the device information into the push list.
  2. After Apple passes the permission verification, it will generate one based on the device ID and APP ID deviceToken, and deviceTokenreturn this to the APP.
  3. deviceTokenAfter it is delivered to the mobile phone, it will be deviceTokenpassed directly to your own server; if it is connected to a third-party SDK, it will be passed to the back end of the third-party service provider, and subsequent push will be completed by the third party.

forward news

Please add image description

  1. When the server has a message that needs to be pushed to the APP, the server will carry the push certificate and the pushed message body to notifyAPNs
  2. APNsAfter receiving the message that needs to be pushed, the certificate carried will be authenticated. If it passes, APNsthe message will be sent to the mobile phone according to the push rules.
  3. When the mobile phone receives a push message from Apple, it will first determine whether the developer has implemented push message interception before displaying it. If not, it will be displayed directly; if interception has been implemented, it will be handed over to the developer for processing.

The above is the general push process. Next, let’s sort out the details: How does the mobile phone APNsestablish a connection? How do you APNsestablish a connection with the server? deviceTokenWill it expire?

Device registers application with APNs

Communicates with the Apple Push Notification Service (APNs) and receives a unique device token to identify your app.
Prerequisites for device and APNs communication:

  • App is configured with remote push function
  • Code registers for remote notificationsUIApplication.shared.registerForRemoteNotifications()
  • The user authorized and agreed to remote push

Create a secure connection between provider servers and APNs

There are two types of connections between provider servers and APNs, token-based connections and certificate-based connections .

Establishing token-based connections to APNs

Token-based authentication provides a APNsstateless way of communicating with . Stateless communication is faster than certificate-based communication because it does not require APNslooking up certificates or other information about your provider server.
There are other advantages to using token-based authentication:

  • You can use the same token from multiple provider servers.
  • You can use one token to distribute notifications to all your company's applications.
  • Token-based requests are slightly larger than certificate-based requests because each request includes a token.
  • Your token must be updated and encrypted using the provider token signing key provided to you by Apple at least once every hour (Apple requires a maximum of 20 minutes and a minimum of 60 minutes)

Token-based connections can send notifications to multiple Apps (Apps under one developer account). The communication is faster, but the notification message size is larger and your token needs to be refreshed regularly. Third-party providers generally use this method
. Such as Aurora push, etc.

Establish certificate-based connections to APNs

With certificate-based authentication, you use provider certificates (push certificates) to establish a secure connection between your provider server and APNs. You can obtain this certificate from Apple through your developer account.

Please add image description

  • Because trust is established at the server level, individual notification requests contain only your payload and device token. They do not contain authentication tokens, which slightly reduces the size of each notification request.
  • You must create separate certificates for each application, and you must also manage separate APNs connections for each application's notifications

Certificate-based connections are relatively simple, the notification message body is slightly smaller, and a push certificate needs to be created for each App.

Characteristics of device token deviceToken

Register your app with APNs and receive a globally unique device token, which is essentially your app's address on the current device. Your provider server must have this token to send notifications to the device.

A device token from one application cannot be used with another application, even if both applications are installed on the same device. Both applications must request their own unique device token and forward it to your provider server. (The device token is not the same for all Apps on this device. Many blogs have explained this incorrectly. The device token is the address of the App on this device)

So does the device token expire? Will it change?

Officially, APNs issues a new token when the user restores the device from backup, when the user installs your app on a new device, and when the user reinstalls the operating system.

When the APP is uninstalled, will the APP server and APNS still send messages to the APP?

the answer is negative. Because APNsthere is a feedback service. When APNsthe server pushes a message to our device, but the device cannot find the APP to send it to, it will APNsreturn a feedback message to the server, recording that the device has uninstalled the APP and cannot receive the push message, and then it will not send it to that device again. The device pushed the message.

Reference: developer.apple.com

Guess you like

Origin blog.csdn.net/wujakf/article/details/128788078