The purpose of this article is to make a complete analysis of IOS push, if you know nothing about push, then after you read the full text will become a push veteran, you will have a full understanding of the various details and principles. The following are some of the experiences that Pikacode used to push with IOS, welcome to communicate with each other and point out the mistakes.
Push service can be said to be the standard of all apps, regardless of the type of app, the push is to a large extent determine the app's open rate, usage, survival. Therefore, knowing and mastering the push principles and methods is an essential skill for every developer, and is critical to every company that relies on the App.
The new usernotifications Framework from IOS 10 shows that Apple consolidates the original fragmented API and adds a number of powerful features. In Apple's official view, the impact of push services on apps and the long-term development of Apple's IOS ecosystem must also be given considerable weight.
Prepare an article
Tip 1: push Notification must purchase an Apple developer account and use a specific push certificate
Use of a free account cannot be pushed.
What if we're using a third-party push service (third party, hereinafter)? Like "Aurora Push". You must also purchase a developer account. Because all the third parties will send the push request to APNs (Apple push Notification service), all push is sent by APNs.
How to register and correct the configuration certificate.
Principle article
Tip 2: The push notification itself is the behavior of the IOS system, so the APP is not running (not in the foreground or backstage):
Still able to push and receive (Notification Center notice, top banner, refresh App right corner of the small dots that badge [hereinafter referred to as the corner], etc. will be controlled by the system and display).
When you receive a push, you cannot get the notification content in the APP's code. Because of the sandbox mechanism, no code for the APP can be executed at this time.
Tip 3: mobile phone to APNs registration push service
1. Register the Push service in code:
#ifdef __IPHONE_8_0 if ([[UIApplication sharedapplication] Respondstoselector: @selecto R (registerusernotificationsettings:)]) {uiusernotificationsettings *settings = [uiusernotificationsettings settingsfortypes:uiusernotificationtypebadge| uiusernotificationtypesound|
Uiusernotificationtypealert Categories:nil];
[[UIApplication sharedapplication] registerusernotificationsettings:settings]; else {uiremotenotificationtype Mytypes = Uiremotenotificationtypebadge | Uiremotenotificationtypealert |
Uiremotenotificationtypesound;
[[UIApplication sharedapplication] registerforremotenotificationtypes:mytypes]; } #else uiremotenotificationtype mytypes = Uiremotenotificationtypebadge | Uiremotenotificationtypealert |
Uiremotenotificationtypesound;
[[UIApplication sharedapplication] registerforremotenotificationtypes:mytypes]; #endif
2. The first time the code is triggered, there is a system window that asks if you want to allow the APP to push the information to you. When you choose to allow, the system will be packaged app+ mobile phone Unique identification + certificate information sent to the APNS server Registration push service, APNS system will be installed on the phone that the APP has push permission to authenticate, so must be joined the Apple deveice mobile phone, using the corresponding APP push card The book can be successfully registered.
3. If the registration is successful, you can obtain the Devicetoken in the following method of APPDELEGATE.M, which is a unique identification of the Mobile + the app combination, when using remote push, just send the push message to the specified Devicetoken The push information can be communicated to the designated APP on the designated mobile phone. So if you use a third party, you need to pass the Devicetoken to a third party in this method. (in IOS 9 in order to better protect user privacy, there will be repeated deletion/installation of the APP cause devicetoken changing situation.) There are times when a push phone can receive 2 of questions that are part of the IOS 9 system problem.
-(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken {
[jpushservice registerdevicetoken:devicetoken];//passes Devicetoken to Aurora push
}
4. If the above steps are successful, you will be able to access the device registration ID provided by a third party at this time. The ability to take this ID value can be used as a criterion for determining whether a device can be pushed successfully (see Tip 6-registration ID). Because when you get that value, you're bound to:
The push certificate is configured correctly (you have push permission).
The device was successfully registered in the APNs and returned to Devicetoken (APNs can identify your device).
The returned Devicetoken is passed to a third party, successfully generating a unique identity registration ID in a third party (a third party can pass your device information to APNs).
5. In comprehensive, registration and receive push must use the real machine, must be connected to the net.
Tip 4: The process of pushing notifications--> APP code from the server side
1. Use your company or third party services to send push requests to APNs (refer to the Apple APNs, or a third party push provides a simpler REST API).
2.APNs receives and verifies push requests.
3.APNs find equipment to send a push.
4. The mobile phone receives the push notification, the system according to the APP state carries on the processing:
Reception Received:
the system will upload the notification content to Didreceiveremotenotification
Background Received:
If you open the Remote Notification, the system will be pushed to the Didreceiveremotenotification:fetchcompletionhandler: (see TIP 5-background push), or at this time the code is not received push.
Display banners, notification centers, sounds, and corner marks.
Exit Received:
If you click on the push banner/notification center and start the APP, the system will send the notification to didfinishlaunchingwithoptions.
Display banners, notification centers, sounds, and corner marks.
Push notification content article
TIP 5: Push notifications are divided into local/remote 2 types:
Local Notification , which specifies the push time at which push notifications are ejected on time. The
remote push notification is divided into 3 types: General push/background push/drop down. There is a latency problem (due to Tip 1 2nd, APNs instability and huge requests during peak periods).
General push
is the push notice we see on our phone. The
contains sounds, banners, corners, and custom fields.
App:
at the front desk, will not display banners, you can pass Didreceiveremotenotification (iOS 7 before) Didreceiveremotenotification:fetchcompletionhandler: (iOS 7 After) get the notification content (see here for the way the foreground displays the banner).
is in the background, displaying banners and unable to get notification content.
is exiting, displaying banners and unable to get notification content.
Click the icon to start, unable to get notification content.
Click the notification banner to start and get the notification content in didfinishlaunchingwithoptions. The
notification content resembles the following:
{
"_j_msgid" = 200806057;//third party with ID, for statistics click
aps = {
alert = "Display content";
badge = 1; APP Corner, you can push N, +n,-N to achieve the fixed, increase, reduce
sound = default;//push sound, defaults system three tones, if you want to use your own voice, you need to drag the sound files & copy to the Xcode engineering directory anywhere, and specify its filename on push
;
Key1 = value1; Custom fields to set up multiple groups for handling internal logic
Key2 = value2;
Background push
Various display effects are identical to the normal push.
Must carry "content-available" = 1;
You must carry at least 1 fields in alert, badge, and sound.
Only IOS 7 is supported later.
The function must be targets-capabilities-background modes-remote notifications in the Xcode project.
APP:
At the front desk, available via Didreceiveremotenotification (iOS 7 before) Didreceiveremotenotification:fetchcompletionhandler: (iOS 7 After) gets the notification content.
In the background, through Didreceiveremotenotification:fetchcompletionhandler: Get the Notification content//acquisition situation and the only difference between the general push, at this time the IOS system allows developers in the APP is in the background, Execute some code, probably provide a few minutes of time, can be used to secretly refresh the UI, switch pages, download the update package and so on.
The notification content cannot be obtained while exiting.
Click on the icon to start, unable to get notification content.
Click the push banner to start and get the notification content in didfinishlaunchingwithoptions.
The notification content is similar to the following:
{
"_j_msgid" = 2090737306;
APS = {
alert = "Display content";
badge = 1;
" Content-available "= 1; Required field
sound = default;
Key1 = value1;
}
Silent push
without any display effect.
Must carry "content-available" = 1, so silence is necessarily backstage.
Must not carry alert, badge, sound.
You can bring custom fields.
APP:
at the front desk, available via Didreceiveremotenotification (IOS 7 before) Didreceiveremotenotification:fetchcompletionhandler: (IOS 7 after) gets the notification content.
In the background, through the Didreceiveremotenotification:fetchcompletionhandler: Get the Notification content//acquisition situation with the general push the only difference, at this time the IOS system allows developers in the APP In the background, the execution of some code, probably provide a few minutes of time, can be used to secretly refresh the UI, switch pages, download the update package and so on.
The notification content cannot be obtained while exiting .
The notification content is similar to the following:
{
"_j_msgid" = 3938587719;
APS = {
alert = "";
" Content-available "= 1; Required field
};
Key1 = value1;
}
Push target article
aliases, tags, and registration IDs are the features provided by third parties to make it easier to specify push targets.
TIP 6: push can be divided according to the different goals:
Broadcasting
No differences are sent to all users.
Aliases alias push
Functions provided by third parties
A mobile phone app can only set an alias (can be modified).
It is recommended that you take a different alias for each user to determine a unique user (or 1 aliases for multiple users).
You can specify multiple alias to send the same content when pushing.
Users who specify alias only can receive a push.
Tag tag push
Functions provided by third parties.
You can set multiple, add, and empty.
Used to specify a variety of properties, such as "1000" + "daily" + "discount" can be used to express the monthly consumption of more than 1k, like to buy groceries, preference discount products users.
If you want to delete, you need to set the tags in the last setting to save to Nsuserdefaults, this time remove unwanted tag, and then reset.
You can assign multiple tags to send the same content when pushing.
Mobile phone If you set a push to specify the number of tags in any tag, you can receive a push message. If the designation "1000" + "Globe" + "original" (Thousand-level consumers, global purchase, the original price), then set the "100" + "globe" + "discount" (hundred-level consumers, global purchase, discount prices) users can receive the push message.
Registration ID Push
Functions provided by third parties.
After the 3rd step of Tip 3 gives Devicetoken to a third party, its server automatically generates a unique ID pointing to that phone.
You can specify multiple IDs at push time to send messages.
Can be used for the core users, the flagship user accurate push.
Application of information in the text
Tip 7: The difference between a message in application (hereinafter referred to as a message) and a push notification:
The Apple push certificate is not required.
Issued by a third party server, rather than APNs.
Faster and almost no latency than notifications can be used for instant delivery of IM messages.
The ability to keep offline messages for long periods of time to get all the historical message content.
Messages are sent through long connection technology, so:
The phone must be started and connected to a third party server.
If the phone is started immediately cut to the background, it is likely that the connection has not been established.
The phone must be at the front desk to receive the message.
Mobile phone from the back to the foreground, will automatically re-establish the connection, and received offline messages.
There is no display (banner, Notification Center, corner mark, sound), so you can:
Custom fields implement UI effects.
Handle the internal logic of the App completely in a quiet situation.
Use some App Store audit will not pass the function, in the audit shutdown function, after the shelves by receiving messages, open the relevant functions.
Combination of big strokes
Tip 8:tags of the combination of skills
See Tip 5-label tag push.
Can be in the service side to statistical analysis of user behavior, and then the designated tags sent to the mobile phone, after receiving the phone for the user to play the corresponding tags.
Tip 9:Combination tips for notification + messages
First look at the comparison of notification and message characteristics:
Notice Message
Delivery time may exist for a few seconds with almost no delay
Get the right time at the front desk or backstage to get content only at the front desk to get content
Offline content retained for "a period of time", expired will be discarded, unable to query history content is always retained, can query all historical content
System Show (silent push or app is not displayed at the front desk) not shown
Because of the differences in their characteristics, the combination of the two is the inevitable choice to maximize the APP's push performance:
Scenario One:
qq/micro-letter chat. will be issued a group of notice + message, if the user did not start QQ, although there is a delay, but must be able to receive the notice, after receiving the notification, the user opened the App, at this time received offline messages, real-time update UI, and friends instant Send/Receive messages. (after receiving the notification, break the network, and then start the APP, you will find that the mobile phone does not display the content just notified, because it relies on pull messages to refresh the page, rather than not stable notice).
Scenario Two: (looking forward to your supplementary ...) )
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.