Ios10 push complete analysis and precautions

This article aims to make a complete analysis of IOS push. If you know nothing about push before, you will become a push veteran after reading the full text carefully, and you will have a full understanding of various details and principles. The following are some experiences of pikacode using IOS push. Welcome to communicate with each other and point out the mistakes and omissions.

Push service can be said to be the standard configuration of all apps. No matter what type of app, push determines the opening rate, utilization rate and survival rate of app to a great extent. Therefore, knowing and mastering push principles and methods is a necessary skill for every developer and is very important for every company that relies on app.

From the new usernotifications framework in IOS 10, we can find that Apple has integrated the original scattered APIs and added many powerful functions. From Apple's official point of view, it must attach great importance to the impact of push service on app and the long-term development of Apple IOS ecosystem.

Preparation

Tip 1: push notification must purchase an apple developer account and use a specific push certificate

You can't push with a free account. What if we use a third-party push service (hereinafter referred to as a third party)? Such as Aurora push. We must also purchase a developer account. Because all third parties will send push requests to APNs (Apple Push Notification Service), and all push requests are issued by APNs. How to register and correctly configure certificates.

Principles

Tip 2: push notification itself is the behavior of IOS system, Therefore, when the app is not running (neither in the foreground nor in the background): it can still push and receive (notification center notification, top banner, refresh the small dot in the upper right corner of the app, that is, badge [hereinafter referred to as corner sign] And so on will be controlled and displayed by the system). When you receive a push, you cannot get the notification content in the app code. Because of the sandbox mechanism, any code of the app cannot be executed at this time.

Tip 3: mobile phone registers push service with APNs 1 Register push service in Code:

2. When this code is triggered for the first time, there will be a system pop-up window asking whether you allow the app to push information to you. When you select allow, the system will package the app + mobile phone unique ID + certificate information and send it to the APNs server to register the push service. The APNs system will verify whether the app installed on the mobile phone has push permission. Therefore, you must join the mobile phone with apple deveice and use the push certificate of the corresponding app to successfully register.

3. If the registration is successful, you can click appdelegate M obtains the devicetoken in the following method, which is a unique identification of the mobile phone + the app combination. When using remote push, you only need to send the push message to the specified devicetoken to transmit the push information to the specified app of the specified mobile phone. Therefore, if you use a third party, you need to pass the devicetoken to the third party in this method. (in IOS 9, in order to better protect users' privacy, there will be repeated deletion / installation of apps, resulting in the continuous change of devicetoken. Sometimes a push mobile phone will receive twice, which belongs to the IOS 9 system problem).

4. If the above steps are successful, you can get the device registration ID provided by the third party. Whether you can get this ID value can be used as a criterion to judge whether the device can be successfully pushed (see Tip 6 - Registration ID). When you get this value, you must:

The push certificate is configured correctly (you have the push permission). The device successfully registers with APNs and returns a devicetoken (APNs can identify your device). The returned devicetoken is passed to the third party, and a unique ID registration ID is successfully generated in the third party (the third party can pass your device information to APNs).

5. To sum up, the real machine must be used to register and receive push, and the network must be connected.

Tip 4: push notification from server -- > app code

1. Use your company or a third party's server to send push requests to APNs (please refer to the relevant materials of Apple APNs, or the third-party push provides a simpler rest API). 2. APNs receives and verifies the push request. 3. APNs finds the device and sends the push. 4. The mobile phone receives the push notification, and the system processes it according to the app status: received at the foreground: the system will send the notification content to didreceiveremotenotification. Received at the background: if rem is enabled ote Notification , The system will push to didreceiveremotenotification: fetchcompletionhandler: (see tip 5 - background push), otherwise the push will not be received in the code at this time. Display banner, notification center, sound and corner mark. Exit and receive: if you click push banner / notification center to start the app, the system will send the notice to didfinishlaunchingwithoptions. Display banner, notification center, sound and corner mark.

Push notification content

Tip 5: push notifications are divided into two types: local / remote:

For local notification, you can specify the push time, and the push notification will pop up on time at this time. Remote push notifications are divided into three types: normal push, background push and silent push. There is a delay problem (due to point 2 of tip 1, the instability of APNs and a large number of requests during peak hours). Ordinary push is the push notification we usually see on the mobile phone. It includes sound, banner, corner mark and custom fields. App: it is in the foreground and will not display a banner. You can use didreceiveremotenotification (IOS 7 before) Didreceiveremotenotification: fetchcompletionhandler: (IOS 7 after) get notification content (see here for the method of displaying banners in the foreground). In the background, banners will be displayed, and the notification content cannot be obtained. In exit, banners will be displayed, and the notification content cannot be obtained. Click the icon to start, and the notification content cannot be obtained. Click the notification banner to start, and the notification content can be obtained in didfinishlaunchingwithoptions. The notification content is similar to the following:

Background push

Various display effects are exactly the same as ordinary push. Must carry "content available" = 1; At least one field in alert, badge and sound must be carried. Supported only after IOS 7. This function must be enabled in the Xcode project targets - capabilities - background modes - remote notifications.

App:

At the front desk, Through didreceiveremotenotification (IOS 7 before), didreceiveremotenotification: fetchcompletionhandler: (IOS 7 after) get the notification content. In the background, you can get the notification content through didreceiveremotenotification: fetchcompletionhandler. / / the only difference between getting and ordinary push is that the IOS system allows developers to execute some code when the app is in the background. It provides a few minutes for secretly refreshing the UI and cutting Change pages, Download update packages, and so on. Unable to get notification content while exiting. Click the icon to start, unable to obtain the notification content. Click the push banner to start and get the notification content in didfinishlaunchingwithoptions.

The contents of the notice are similar as follows:

Silent push has no display effect. Must carry "content available" = 1;, So silence must be backstage. Alert, badge and sound must not be carried. Can carry custom fields. App: in the foreground, Through didreceiveremotenotification (IOS 7 before), didreceiveremotenotification: fetchcompletionhandler: (IOS 7 after) get the notification content. In the background, you can get the notification content through didreceiveremotenotification: fetchcompletionhandler. / / the only difference between getting and ordinary push is that the IOS system allows developers to execute some code when the app is in the background. It provides a few minutes for secretly refreshing the UI and cutting Change pages, Download update packages, and so on. Unable to get notification content while exiting.

The contents of the notice are similar as follows:

Push target

Alias, label and registration ID are functions provided by third parties to specify push targets more easily. Tip 6: push can be divided into:

The broadcast is sent to all users without difference.

Alias push

Functions provided by a third party. Only one alias (modifiable) can be set for an app of a mobile phone. It is recommended to take a different alias for each user to determine the unique user (or one alias for multiple users). During push, you can specify multiple aliases to distribute the same content. Only the users who specify alias can receive push.

Tag push

Functions provided by third parties. Multiple can be set, can be added, and can be cleared. It is used to specify various attributes, such as "1000" + "Daily" + "discount". It can be used to indicate users who spend more than 1K a month, like to buy daily necessities and prefer discounted goods. If you want to delete, you need to save the set tags to nsuserdefaults during the last setting. This time, remove the unnecessary tags and reset them. When pushing, you can specify multiple tags to distribute the same content. If the mobile phone is set to push any of the specified multiple tags, it can receive the push message. If you specify "1000" + "globe" + "original" (1000 yuan consumer, global purchase, original price), users who set "100" + "globe" + "discount" (100 yuan consumer, global purchase, discount price) can receive the push message.

Registration ID push

Functions provided by third parties. In step 3 of tip 3, after the devicetoken is provided to a third party, its server will automatically generate a unique ID pointing to the mobile phone. You can specify multiple IDs to send messages during push. It can be used to accurately push core users and flagship users.

In application message

Tip 7: in app messages (hereinafter referred to as message) is different from push notification. Message: Apple Push certificate is not required. It is distributed by a third-party server instead of APNs. Compared with notification, it is faster and has little delay. It can be used for instant delivery of IM messages. Offline messages can be retained for a long time and all historical message contents can be obtained. Messages are distributed through long connection technology, so: The phone must be started and connected to a third-party server. If the phone starts and switches to the background immediately, it is likely that the connection has not been established. The phone must be at the front desk to receive messages. When the mobile phone switches back to the foreground from the background, it will automatically re-establish the connection and receive an offline message. There is no display (banner, notification center, corner sign, sound), so you can: customize the fields to achieve UI effect. Process the internal logic of the app completely in silence. Use some functions that will not pass the app store audit, turn off the functions during audit, and turn on the relevant functions by receiving messages after they are on the shelf.

Combined moves

Tip 8: see tip 5 - Tag push for tips on combining tags. You can count and analyze user behavior on the server, and then send the specified tags to the mobile phone. After the mobile phone receives them, it will mark the corresponding tags for the user.

Tip 9: combination of notification and message skills first, let's look at the comparison of notification and message characteristics: there may be a few seconds delay in the delivery time of notification message. There is almost no delay in obtaining the opportunity. The content can be obtained in the foreground or background. Only the content can be obtained in the foreground. The offline content will be retained "for a period of time". It will be discarded when it expires, and the historical content cannot be queried. It will always be retained, All historical contents can be queried, and the system display will be displayed (silent push or app will not be displayed in the foreground)

Due to their differences in characteristics, the combination of the two is an inevitable choice to maximize app push performance: scenario 1: QQ / wechat chat. A group of notifications + messages will be sent at the same time. If the user does not start QQ, although there is a delay, he must be able to receive the notification first. After receiving the notification reminder, the user opens the app. At this time, he receives an offline message, updates the UI, and sends / receives messages with his friends in real time. (after receiving the notification, disconnect the network and start the app. You will find that the content just notified will not be displayed in the mobile phone at this time, because it depends on pulling messages to refresh the page, rather than unstable notifications). Scenario 2: (looking forward to your supplement...)

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>