The principle of active push message on the mobile side

Source: Internet
Author: User

Ext.: https://www.zhihu.com/question/19628406/answer/77205019

First, the service-side proactive push message to the client process Chezefing Jingyong
Links: https://www.zhihu.com/question/24938934/answer/85359794
Source: Know
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Server-side proactive push to the client is how a process


At present, the service side to the client push, the common practice is that the client and the server to maintain a long connection, the client timed to the server to send heartbeat to maintain this long connection. When a new message comes in, the server detects the ID of the TCP channel corresponding to the message and finds the corresponding channel for the message to be issued.


This is only the most basic communication model, on this, there are derived from the publication/subscription model for the message, the client can subscribe to a topic, the service side according to topic find the corresponding channel for batch message issued. All of the clients implicitly subscribe to all of this opic, so "the mode like China Mobile sends messages to all the phones in the whole network" can also understand the "broadcast message", that is, send a message to all this topic.


On this basis, there are several open source agreements to help you define this thing, more famous such as the MQTT protocol (just these days I see the MQTT agreement of Chinese translation, share to everyone), GitHub search Mqtt can find the corresponding open source protocol implementation projects, interested in self-search.


How the app is now using message push


In fact, the mainstream mobile platform has a system-level push products, Android has Gcm,ios on the Apns,winphone have MPNs. But because of some of the reasons you understand, GCM is not available in the country, so the domestic mobile application in another way---run a service in the background, maintain the TCP long connection applied to the server to achieve the effect of real-time message delivery.


But how to maintain a long connection on the mobile side is a very complicated thing, said before, the client to maintain a long connection with the service side by sending the heartbeat signal (Heartbeat) regularly, but if the heartbeat frequency is too frequent, the mobile device power consumption increases, the heartbeat interval too long may cause the connection to be disconnected. And it is generally believed that mobile device is in a changeable network environment, WIFI,2G,4G switch, the base station switch will cause the network change, the heartbeat frequency in different network environment, and the re-connected action of network change, all need a large amount of data statistical analysis summary.


This is only the client's problem, in today's mobile applications with hundreds of millions of users, how to maintain so many long connections, if the large-scale message issued and subsequent to the message of the various statistical actions are technical difficulties.


Furthermore, now the application is generally full-platform, send a message, should be sent to Android,ios, winphone,android to go to the self-built TCP long connection channel, IOS and Winphone go home system push channel. That means you have to maintain the three set of push systems on your server.


Obviously for small teams, it is very difficult to set up a message push system on their own, so there are a lot of excellent push products in the market, helping developers to aggregate these push methods and provide a unified push interface. Foreign such as Urban Airship, parse, etc., domestic has jpush, Baidu Cloud push, homing pigeon, Leancloud and so on. (Unfortunately, the very good parse has been announced by Facebook to stop development and will be closed in 1 years)


Now in addition to the large volume of the company's self-built push system, the general General company is the use of third-party push services, all of the above third-party push services, the basic functions are free, if there are conditions, it is recommended to integrate a number of services, A/b test compared to push the effect, I engaged with a push company, In this will not evaluate the quality of the products.

Second, the server-side proactive push message to the client principle

Now the main platform of the mobile phone has its own push function, so that application developers can easily integrate the push capability into the application.

There is GCM on Android (Google Cloud Messaging)
APNs on IOS (Apple Push Notification Service)
There is a MPNs (Microsoft Push Notification Service) on the Windows Phone.

However, because the Windows Phone market share is not high, so generally no one will specifically do WP system push. As for Android GCM is basically not available in the country. The main reasons are the following two points:
One, most of the domestic Android phones do not have Google services, also can not use GCM, this is the main problem.
Second, in the domestic Google services are generally not very stable, the reason you understand.
So now when it comes to messaging, the Android platform uses servers and devices to pull a long connection, while the iOS platform uses Apple's own APNs service. In each of the two platform push principle, first answer the question of how the server first find the device, and then find the app. Each device has its own device number, and the app in the device has a unique package name. So the server only need to find the device number and package name can be located to an application of a device, and this device number and package name together constitute an identifier, called Device_token, so the problem is simplified to the Device_token and message content and other information to the server, The server sends the content to the unique device_token. It is as if you are in Shanghai to send a courier to a certain district of a room, so express son first will be mailed to shun Fung Company in Beijing's total site, and then according to the address of the cell delivery/routing to XXX room, such a shipping process even completed. Here, you want to send the Express is you want to send the "message", delivery room equivalent to the final "receive the message of the app", Shun Fung Company in Beijing's total site equivalent to the "equipment" mentioned here, the room is served to the room number is equivalent to the "package name" mentioned in this link.

The next step is to briefly talk about the implementation of these two platforms to push the principle.
The first is the iOS platform, the iOS push is through Apple's own APNs service, the user needs to Device_token and message content such as push information to APNs server, the rest is done by Apple itself. However, if the provided Device_token is invalid (the app is uninstalled, the system version upgrade causes Device_token changes, etc.) then the push process will be interrupted, frequent disconnection will even be APNs considered to be a Dos attack. For details, please refer to why Apple push, two times between pushes, the second push will be slow? -Desert Answer Android platform push principle: Android platform in the case of no GCM need to have their own server or third-party push service provider's server and the device to establish a long connection, through a long connection to push. However, it is not recommended to set up the server to implement push function, one is because the cost is too high (development costs, maintenance costs), the server itself, whether stability or speed than the third-party push service provider effect. The other is that because of their small amount of data, using third-party push service providers can push with their dimensions for precise push. Friends of the league push is to do better, can be based on user clustering, regional, language and other multidimensional degrees to push, the greatest degree of reduction of interference to users, only the message to the relevant users. What are the advantages of the friend Alliance push? -Jingyong's answer
is simple for Android platform message push.
<img src= "Https://pic1.zhimg.com/9f93b6c802028443177b31ade2ddd550_b.png" Data-rawwidth= "731" data-rawheight= "578" class= "Origin_image zh-lightbox-thumb" width= "731" data-original= "https:// Pic1.zhimg.com/9f93b6c802028443177b31ade2ddd550_r.png ">
The developer sends the information directly to the required device via a third-party push service provider, and the third-party push service provider establishes a long connection channel with the device and routes the message to the app (device 1 and device 2 in the figure), for devices that have no network connectivity like device 3 or that have not successfully established a long connection channel. Automatically receives a message pushed by a third-party push service provider when device 3 is connected and the push message does not expire, ensuring that the message is not lost. The push push principle for iOS: iOS app push is largely dependent on Apple's eco-provided APNs (Apple push Notification service).
The bottom of the two pictures to briefly explain the principle of its push


First, as the device identification of the Device-token is issued by APNs, the app developer or third-party push platform (pictured in the provider) do the job is to collect this device-token, APNs push is required to be pushed based on Device-token issued by APNs. Only the correct device-token will be accepted by APNs, if it is a wrong, or invalid Device-token (such as the app has been uninstalled), APNs will not accept.

Then the developer uses a third-party push platform (provider in the figure) after the push content and scope is selected to push, the third-party push platform to submit information to APNs, the rest of the operation is done by APNs, the whole process of the third-party push platform can not be controlled.

To whether push send have more want to know something can pay attention to the Friends of the Alliance push the official Friends of the alliance message push |app Push and forumhttp/bbs.umeng.com/forum-push-1.html Practical tips for iOS-private push implementation method

In the development of enterprise apps, sometimes security-based considerations do not allow devices to connect to the extranet. There is a problem, that is, iOS push function is not working, because the push function of iOS is cured in the system, must connect to Apple's APNs server to work, in order to let this kind of only work in the Intranet app can also have push function, we need to implement the push function.

The implementation of autonomous push is the ability to use the iOS VoIP class app to reside in the background. This kind of app, the system is launched when the device is booted, the app can delegate its own socket to the system, when the socket has data arrives, the system will wake up the app, give it a short CPU time to process the data, Plus UIApplication's KeepAliveTimeout handler (minimum 10 minute interval), you can reestablish the connection every once in a while to achieve the need to keep the socket long connection.

Note: Because VoIP apps can keep apps in the background and maintain a long socket connection, Apple has a rigorous review of such apps, and all apps that aren't real VoIP will be rejected! Remember!

1. Set up apps for VoIP apps

Open a <app>-info.plist file and add the following key

Required background modes, add app provides Voice over IP services

2. Set the socket to asynchronous mode and set the socket to the VoIP class so that the system can host it.

CFStreamCreatePairWithSocket(NULL, (CFSocketNativeHandle)(mosq->sock), &readStream, NULL);//保证不关闭原来的socketCFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanFalse);//设置成voip socketCFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);

3. Install KeepAliveTimeout handler to allow the system to reestablish connections every time

    UIApplication *application = [UIApplication sharedApplication];    [application setKeepAliveTimeout:600 handler:^{        [self reconnect];    }];

I refer to Jmsnil's Mqttexample project, which is an example of using MQTT, and I have made some changes to it so that I can run the receive push in the background.

Original Project Address:

Https://github.com/jmesnil/MQTTExample

I modified the example project address:

Https://github.com/Guou/Demo-mqtt-push

Open Terminal, enter the following command to get notifications

curl -X PUT --data-binary "1"  http://eclipse.ttbridge.com/%2FMQTTExample%2Ftestcnpush

This example can only work on the real machine, if you are below the iOS7, please delete the code of the Application:didfinishlaunchingwithoptions: Local notification permission request.

--------------------

I feel all the intranet environment to do push message idea or forget it, too much trouble, go straight to SMS and e-mail notice got

(turn) The principle of active push message on the mobile side

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.