The analysis and realization method of the _android of the Android handset's millet push demo

Source: Internet
Author: User

These last few months are in preparation for a job and looking for work, pay a lot, finally is a little harvest, so did not how to organize notes. Only recently have the time to put up their own notes, share my learning experience.

Push off
Due to the recent project to use the Android message push, there are many kinds of solutions for Android push, there are C2DM, polling, SMS,MQTT protocol, XMPP protocol and third party platform, after our consideration of the project requirements, we chose the third party platform to push the millet push, Below is the realization method of millet push.

Implementation preparation

Want to use millet to push the first to the Millet developer platform to register for the developer account, after 1-3 days of audit will pass, and then can apply for push service. Apply for an app push service, get AppID and Appkey can go to learn it to the demo to use, the following I mainly talk about my understanding of the demo.

Push instructions

Push the content can be sent by the backend server using Millet push the SDK to send, can also be in the Millet push platform to send, send the message has two kinds: notification message and transmission information.

Notification message:

Is the message that will be displayed on the Android notification bar and the user clicks on its response to implement it yourself or click Open app when sending it.

Transmission message:

That is, transparent transmission, that is, transport network regardless of the transmission business, only responsible for the transfer of the business needs to the destination node, while ensuring the quality of transmission, but not the transmission of the business processing. is to pass the message directly to the app, not automatically on the notification bar.

Official demo Show

The official demo inside simply shows us the various settings of millet push and how to achieve receive push. Demo first to the millet push the SDK into the library, and then there are 4 Java files, such as the figure

The 4th dialog can not be looked at, the key is to demomessagereceiver the broadcast receiver, receive all the information pushed and then processed. And that DemoApplication and mainactivity is used to realize the display of information received.

Then the interface is like this (demo doesn't do screen fit, so that's it):

A bunch of configuration about push, and a textview that shows log. Mainly for us to show the use of millet push process.

Demomessagereceiver Broadcast Receiver

To realize the mobile end of millet push to receive, the core is the above mentioned the main demomessagereceiver this broadcast receiver. This inherits from the Pushmessagereceiver class inside the Millet SDK, which can be overridden by rewriting the following 5 callback methods to implement push processing.

@Override public
  void Onreceivepassthroughmessage (context context, Mipushmessage mipushmessage) {
    // Used to receive a message sent by the server to the client, which is triggered by a message that is passed through.
  }

  @Override public
  void onnotificationmessageclicked (context context, Mipushmessage mipushmessage) {
    // Used to receive notification messages sent by the server to the client, which is triggered when the user clicks the notification manually.
  }


  @Override public
  void onnotificationmessagearrived (context context, Mipushmessage mipushmessage) {
    // Used to receive notification messages sent by the server to the client, which is triggered when the notification message arrives at the client. In addition, notification messages that do not eject notifications when applied to the foreground also trigger this callback
  function


  @Override public
  void Onreceiveregisterresult Mipushcommandmessage mipushcommandmessage) {
    //is used to receive response results from clients sending registration commands to the server
  }

  @Override
  public void Oncommandresult (Context context, Mipushcommandmessage mipushcommandmessage) {
    //is used to receive the results of a client's response to a command sent to the server. Here you receive the results of the various commands, such as registering the service, setting up the alias, and so on, where you can implement initialization errors and then restart.
  }

DemoApplication Inherit Application class

The main function of this application class is to set the app ID and key, and to register the push service in the OnCreate method.

In this demo, it also has a handler in it to allow the broadcast receiver to use it to send toast and let mainactivity print log on TextView.

 User your AppID the key.
  private static final String app_id = "1000270";
  User your AppID the key.

  private static final String App_key = "670100056270"; This tag retrieves the information it needs in the ADB logcat and simply enters the ADB logcat at the command line terminal |
  grep//Com.xiaomi.mipushdemo public static final String TAG = "Com.xiaomi.mipushdemo";
  private static Demohandler Shandler = null;

  private static mainactivity smainactivity = null;

    @Override public void OnCreate () {super.oncreate (); Register a push service and send a broadcast to Demomessagereceiver after successful registration You can obtain registration information from the Demomessagereceiver Oncommandresult method mipushcommandmessage Object Parameters if (Shouldinit ()) {Mipushclient.regist
    Erpush (This, app_id, App_key);
        Loggerinterface Newlogger = new Loggerinterface () {@Override public void Settag (String tag) {
      Ignore} @Override public void log (String content, Throwable t) {log.d (TAG, content, T);
    @Override public void log (String content) {    LOG.D (TAG, content);
    }
    };
    Logger.setlogger (this, newlogger);
    if (Shandler = = null) {Shandler = new Demohandler (Getapplicationcontext ()); }//This is the way to detect if a process is in the foreground private Boolean shouldinit () {Activitymanager am = (activitymanager) getsystemservice (
    Context.activity_service));
    list<runningappprocessinfo> Processinfos = am.getrunningappprocesses ();
    String mainprocessname = Getpackagename ();
    int mypid = Process.mypid (); for (Runningappprocessinfo Info:processinfos) {if (Info.pid = = Mypid && mainprocessname.equals (info.proce
      Ssname)) {return true;
  return false;

 }

Mainactivity

Demo inside the mainactivity is mainly to implement the interface of those button processing, to tell us how to set the local push scheme, the specific settings have

    • Set, Undo alias: Alias (alias) is a user identity other than Regid (auto-generated) and UserAccount, which can be passed Mipushclient.setalias () method and Mipushclient.unsetalias () To set and Undo.
    • Set, REVOKE user account (Useraccoun): Can be set up and revoked by Mipushclient.setuseraccount () method and Mipushclient.unsetuseraccount () method.
    • Subscribe, Undo Tags: developers can combine their own business features to give users a different label (TOPIC). When a message is pushed, the developer can combine the content of each message with the target user, select the corresponding label for each message, and push the message to all users who have hit the label. You can subscribe to and cancel by Mipushclient.subscribe () and Mipushclient.unsubscribe ().
    • Pause, resume push, set push time: Set push time Demo uses a homemade dialog class Timeintervaldialog to let the user set (forcibly increase the amount of code), can be set by Mipushclient.setaccepttime () , pauses and restores are set through Pausepush () and Resumepush ().
    • But pause and restore on the bottom of the implementation is actually still call set push time for 00:00-00:00 and 00:00-23:59 just, but also pay attention is not in the receiving period is temporarily do not receive push message just, once to receive the message will still be pushed to come.

Configuration of Androidmanifest files

First millet push support the lowest Android version is 2.2, so to

<USES-SDK android:minsdkversion= "8"/>

The following permissions are required:

 <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> & Lt;uses-permission android:name= "Android.permission.INTERNET"/> <uses-permission android:name= " Android.permission.ACCESS_NETWORK_STATE "/> <uses-permission android:name=" android.permission.ACCESS_WIFI_ State "/> <uses-permission android:name=" Android.permission.READ_PHONE_STATE "/> <uses-permission Android:name= "Android.permission.GET_TASKS"/> <uses-permission android:name= "Android.permission.VIBRATE"/ > <permission android:name= "Com.xiaomi.mipushdemo.permission.MIPUSH_RECEIVE" android:protectionlevel= " Signature "/> <!--here Com.xiaomi.mipushdemo changed to app's package name--> <uses-permission android:name=" Com.xiaomi.mipushdemo.permission.MIPUSH_RECEIVE "/> <!--here Com.xiaomi.mipushdemo changed to app's package name--> 

Then is to register a bunch of broadcastreceiver (are static registration, because to long-term in the background running, and inherited Pushmessagereceiver Demomessagereceiver also need to register do not forget) and service, Altogether are 3 broadcast receivers and 4 services.

<service android:enabled= "true" android:process= ":p ushservice" Android:name= "Com.xiaomi.push.service.XM
     Pushservice "/> <service android:name=" Com.xiaomi.push.service.XMJobService "true" Android:exported= "false" android:permission= "Android.permission.BIND_JOB_SERVICE" android:process= ":p Ushservic E "/> <!--Note: This service must be added--> <service android:enabled=" true "after the 3.0.1 version (including 3.0.1) android:exp
     Orted= "true" android:name= "Com.xiaomi.mipush.sdk.PushMessageHandler"/> <service android:enabled= "true"
    Android:name= "Com.xiaomi.mipush.sdk.MessageHandleService"/> <!--Note: This service must be added after version 2.2.5 (including 2.2.5)--> <receiver android:exported= "true" Android:name= "Com.xiaomi.push.service.receivers.NetworkStatusReceiver" > <intent-filter> <action android:name= "Android.net.conn.CONNECTIVITY_CHANGE"/> <categ Ory Android:name= "Android. Intent.category.DEFAULT "/> </intent-filter> </receiver> <receiver android:exported=" False "android:process=":p ushservice android:name= "Com.xiaomi.push.service.receivers.PingReceiver" > &L t;intent-filter> <action android:name= "Com.xiaomi.push.PING_TIMER"/> </intent-filter> </ receiver> <receiver android:name= "Com.xiaomi.mipushdemo.DemoMessageReceiver" android:exported= "true" > <intent-filter> <action android:name= "Com.xiaomi.mipush.RECEIVE_MESSAGE"/> </inte
      nt-filter> <intent-filter> <action android:name= "Com.xiaomi.mipush.MESSAGE_ARRIVED"/> </intent-filter> <intent-filter> <action android:name= "Com.xiaomi.mipush.ERROR"/> &
 Lt;/intent-filter> </receiver>

As long as the demo AppID and Appkey changed to their own (remember is the corresponding package name and application name), and then can receive the corresponding push.

Mobile Port porting Implementation

Probably understand this demo, it can be transferred to the millet into their own app to achieve their needs, the transplant time to note:

    1. First want to good application of the package name, from the millet push platform where registration, access to AppID and Appkey.
    2. However in the millet push the SDK added to the storage.
    3. Androidmanifest files are configured with permissions (permissions within the Android6.0 may need to be dynamically acquired), registering those service and Broadcastreceiver
    4. At the beginning of the application remember to initialize the registration of Millet push service.
    5. Finally, you can inherit a pushmessagereceiver and do whatever you want in it.
    6. Finally provide a simplified after I wrote the demo, understand the above can refer to my simplified transplant, after all, the above is too comprehensive, we actually see the need to achieve on the line.

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.