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

Source: Internet
Author: User

PS: Use a push. Feel the push function of implementing third party application is relatively simple. The official document is also very clear.

Learning content:

1. Use a push to implement the push of the third party application.

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

Feel the need to write something is not very much, because the official document has been written very clearly, how to configure, import jar package,. So file, As well as the configuration of the Androidmanifest files are very clear. I'll just give you a little bit of a brief introduction here. I will not say that these basic things do not diminish the need.

I.pushmanager

Pushmanager is the core of the implementation of push, you can push control, set the label, set the alias, set the default time and so on. All interfaces are called by this object. So we need to initialize the Pushmanager object.

Copy Code code as follows:
Pushmanager.getinstance (). Initialize (context);

Objects are created in a single column. So the object is also unique. This gets to the Pushmanager object at the same time to initialize a push service, the interface call after the push service background running, through the broadcast of the CID sent to the app. What's really important here is this broadcast, Everything else has no place to focus. Just a little attention.

II. Getuipushreceiver app receives CID broadcasts

Package com.example.totem.getuidemo.receiver;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;

Import Android.os.Bundle;
Import com.igexin.sdk.PushConsts;

Import Com.igexin.sdk.PushManager;
 /** * Created by Totem on 2016/8/15. * @author code, such as wind/public class Getuipushreceiver extends Broadcastreceiver {public static String key_client_id = "pus

 H_NOTIFICATION_CLIENT_ID ";

 public static String message; public static void init (context context) {/** * Initialization object * Can push control, set the label, set the alias, set the default time, etc. * all interfaces are called by the object * * */Pus
  Hmanager.getinstance (). Initialize (context);
  /** * Get clientId */String clientId = Pushmanager.getinstance (). Getclientid (context);
  if (clientId!= null) {key_client_id = ClientId; } @Override public void OnReceive (context, Intent Intent) {//related processing} private static Ongetpushmessageli

 Stener Ongetpushmessagelistener; public static void Setongetpushmessagelistener (OngetPushmessagelistener ongetpushmessagelistener) {Getuipushreceiver.ongetpushmessagelistener =
 Ongetpushmessagelistener;
 /** * External Exposure interface */public interface ongetpushmessagelistener{void Getostfmessage (String message);
 }

}

This broadcast is to receive the push message of the core class, in fact, is relatively simple to achieve, the first is to get the Pushmanager object, and then initialize a push service, in the Onreceiver method to pass over the data to judge, here only two states are judged, one is push notice, The other is through the message.

@Override public
 void OnReceive (context context, Intent Intent) {
  Bundle Bundle = Intent.getextras ();
  Switch (Bundle.getint (pushconsts.cmd_action)) {


   /**
    * PUSH Notification * */Case
   Pushconsts.get_clientid:
    String cid = bundle.getstring ("ClientID");
    break;

   /**
    * Transmission message, passed over is a JSON string. In general, clients are required to parse.
    * */Case
   Pushconsts.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 you get is not empty. Then do the relevant processing
      * * * * * * * * * *
     (payload);
     Ongetpushmessagelistener.getostfmessage (message);
    } else{Message
     = "";
    }
    break;
  }
 }

So by judging, we can learn that is a push notification, or a pass-through message, and so on. Finally, write an interface to the outside, in fact, exposed to the activity interface, when we received the data in the broadcast changed, or there is data transmission over the way, need to pass this interface, Informing the data has changed, requiring the main process to do 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. Open 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 can not receive. In the default state is turned on.

Copy Code code as follows:
Getuipushreceiver.init (Getapplicationcontext ());p Ushmanager = pushmanager.getinstance ();p Ushmanager.turnonpush ( this);

Iv. Turning off push services

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

Copy Code code as follows:
Getuipushreceiver.init (Getapplicationcontext ());p Ushmanager = pushmanager.getinstance ();p Ushmanager.turnoffpush ( this);

V. Stop the SDK Service.

StopService (context). After the SDK service is stopped, the service does not terminate, but it terminates the push and networking features. A reboot requires calling the Initalize () method or the Turnonpush () method.

Copy Code code as follows:
Getuipushreceiver.init (Getapplicationcontext ());p Ushmanager = pushmanager.getinstance ();p Ushmanager.stopservice ( this);

The basic things are all done, there are some settings tags, silent time, binding aliases, and so on, if you are interested in the study, anyway, I was temporarily not used to. So we don't move these modules anymore. Let's see how to use a push-back platform to implement the tweet.

First we need to create our own application, 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 do a demo, you can send a push through the background.

The title and content are required, and then it's OK to be sure, so the message is pushed out. Turn on the push service on the app, You can receive a push. I will not take a screenshot of the message. One thing to note is that the message is a JSON-formatted string. A push gives us the formatting tools for JSON.

Finally put a source code, this source code is unable to run, because this is the use of my own background, we also need to create accounts, with their own background to create applications, and then put the Androidmanifest file in the Appid,appkey, Appsecret. and provide a lot of documents inside, help everyone to integrate, click to download.

Related Article

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.