Android uses a push to implement three-party application push function

Source: Internet
Author: User

PS: With a push. Feel the push function of a third-party application is still relatively simple. The official documentation is also very clear.

Learning content:

1. Push for third-party apps with a push.

All configurations I will finally give a source code, the internal configuration and documentation to help you complete the configuration, here is not introduced.

Feel the need to write something is not very much, because the official document has been written very clear, how to configure, import jar package,. So file, And the configuration of the Androidmanifest files is very clear. I'm going to give you a little bit of a brief introduction. Sign up for an account or something. I will not say that these basic things do not diminish the necessity.

I.pushmanager

Pushmanager is the core class that implements push, You can push controls, set tags, set aliases, set default time, and so on. All interfaces are called by this object. So we need to initialize the Pushmanager object.

Pushmanager.getinstance (). Initialize (context);

objects are created in a single column. So the object to get is unique. Here to get to the Pushmanager object at the same time to initialize a push service, the interface call after the push service background run, The CID is sent to the app by radio. The most important thing here is this broadcast, and the rest of the stuff has no focus. Just a little bit of attention.

II. Getuipushreceiver app receives CID's broadcast

 PackageCom.example.totem.getuidemo.receiver;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.os.Bundle;Importcom.igexin.sdk.PushConsts;ImportCom.igexin.sdk.PushManager;/*** Created by Totem on 2016/8/15. *@authorcode, such as wind*/ Public classGetuipushreceiverextendsBroadcastreceiver { Public StaticString key_client_id = "push_notification_client_id";  Public StaticString message;  Public Static voidinit (Context context) {/*** Initialize Object * Can push control, set label, set alias, set default time, etc. * all interfaces are called by this object **/pushmanager.getinstance (). Initialize (context); /*** Get ClientID **/String clientId=pushmanager.getinstance (). Getclientid (context); if(ClientId! =NULL) {key_client_id=clientId; }} @Override Public voidOnReceive (Context context, Intent Intent) {//Related processing    }    Private StaticOngetpushmessagelistener Ongetpushmessagelistener;  Public Static voidSetongetpushmessagelistener (Ongetpushmessagelistener ongetpushmessagelistener) {GeTuiPushReceiver.onGetPushM Essagelistener=Ongetpushmessagelistener; }    /*** External Exposed interface **/     Public Interfaceongetpushmessagelistener{voidgetostfmessage (String message); }}

This broadcast is the core class to receive push messages, in fact, the implementation is relatively simple, the first is to get Pushmanager object, and then initialize a push service, in the Onreceiver method to pass over the data to judge, here only the two states are judged, One is push notifications and the other is a pass-through message.

@Override Public voidOnReceive (Context context, Intent Intent) {Bundle bundle=Intent.getextras (); Switch(Bundle.getint (pushconsts.cmd_action)) {/*** Push Notifications **/             CasePushConsts.GET_CLIENTID:String CID= Bundle.getstring ("ClientID");  Break; /*** Through the message, a JSON string is passed. In general, client parsing is required. * */             CasePushConsts.GET_MSG_DATA:String AppID= Bundle.getstring ("AppID"); byte[] payload = Bundle.getbytearray ("payload"); String TaskID= Bundle.getstring ("TaskID"); String MessageID= Bundle.getstring ("MessageID"); if(Payload! =NULL){                    /*** If the data received is not empty. Then do the relevant processing **/message=NewString (payload);                Ongetpushmessagelistener.getostfmessage (message); }Else{message= ""; }                 Break; }    }

So through judgment, we can know, is the push notification, or through the message and so on. Finally wrote an interface to the external exposure, actually exposed to the activity interface, when we received in the broadcast data changes, or there is data to pass over, This interface is required to inform the data that a change has been made, requiring the main process to do the relevant processing, which is the purpose of exposing the interface. And then on the main page, we just need to implement this interface.

Getuipushreceiver.setongetpushmessagelistener (new  Getuipushreceiver.ongetpushmessagelistener () {        @Override          Public void getostfmessage (String message) {            if(message!=null) {                Messagedata.add (message);}}    );

III. Turn on push

Turnonpush (context) method, used to open the push, if we do not actively open the push, even if the background to send a push message, we are not received. In the default state is enabled.

= pushmanager.getinstance ();p Ushmanager.turnonpush (this);

Iv. Closing the Push service

Turnoffpush (context) method, which is used to turn off the push service. After this method is executed, you cannot receive the push service, and it is important to note that you must use the Turnonpush (context) method to receive the push again. Other methods are not valid.

= pushmanager.getinstance ();p Ushmanager.turnoffpush (this);

V. Stop the SDK Service.

StopService (context). After you stop the SDK service, the service does not terminate the operation, but instead terminates the push and networking features. Restarting requires calling the Initalize () method or the Turnonpush () method.

= pushmanager.getinstance ();p ushmanager.stopservice (this);

Basic things have been introduced, there are some settings tags, silent time, binding aliases and so on, if you are interested in the study, I am temporarily not used to. So we will not move these modules. Let's take a look at how to use a push-back table to get pushed.

First we need to create our own application, and then we can get the data we want in the application configuration, the effect is as follows.

Here are the data we want, appid,appkey, and so on, all of which need to be configured in the Androidmanifest file. After you have done your demo, you can send push through the background .

Both the title and the content are mandatory, and are always OK, so the message is pushed out. Open the push service on the app, You can receive a push. I won't go through with the news. One thing to note is that the pass-through message is a JSON-formatted string. A push gives us the JSON format tool.

Finally put a source code, this source code is unable to run, because here is the use of my own backstage, we also need to create an account, use their own background to create the application, and then put the Androidmanifest file in the Appid,appkey, Appsecret to make changes. And it provides a lot of documentation to help you integrate.

http://pan.baidu.com/s/1pLldyqv

Android uses a push to implement three-party application push function

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.