Objective
Recently these months are in preparation for job search and job search, pay a lot, finally is a little harvest, so no how to organize notes. Until recently, I had no time to get my notes sorted out and share my learning experience.
Push
Due to the recent project to use Android message push, about Android push solution There are many kinds, there are C2DM, polling, SMS,MQTT protocol, XMPP protocol and third-party platform, after our consideration of the project needs, we chose the third-party platform push the Xiaomi push send, The following is the implementation of Xiaomi push delivery method.
Prepare for implementation
Want to use Xiaomi to push the first to go to the Xiaomi developer Platform Registration application developer account, after 1-3 days of review 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 use the demo, below I mainly talk about my understanding of the demo.
Look at the time combined with the demo code to see a lot clearer, here is the SDK and demo
Push instructions
Push content can be sent by the backend server using Xiaomi Push SDK, also can be sent on the Xiaomi push platform, send the message there are two kinds: notification message and pass information.
Notification message:
Is the message that will be displayed on the Android system notification bar, the user clicks on its response event to implement it himself or when it is sent, click to open the app.
Pass-through message:
That is, transparent transmission, that is, transmission network regardless of the transmission business, is only responsible for the transfer of the business to the destination node, while ensuring the quality of the transmission, and not the transmission of the business processing. is to pass the message directly to the app, not automatically displayed on the notification bar.
Official Demo Instructions
The official demo gives us a simple demonstration of the various settings for Xiaomi push and how to implement receive push. The demo first adds Xiaomi's SDK to the library and then contains 4 Java files.
The 4th dialog can not be seen, the key is to demomessagereceiver this broadcast receiver, receive push to the various information and then processed. And that DemoApplication and mainactivity are used to achieve the display of information received.
Then the interface is like this (the demo didn't do the screen fit, so that's it):
A bunch of configuration about the push, and a textview that shows the log. Mainly for us to show the use of Xiaomi push process.
Demomessagereceiver Broadcast Receivers
In order to realize the mobile reception of Xiaomi push, the core is the broadcast receiver which is the main demomessagereceiver mentioned above. This inherits from the Xiaomi SDK inside the Pushmessagereceiver class, this broadcast receiver can be overridden by the following 5 callback methods to implement the push processing.
@Override Public void Onreceivepassthroughmessage(context context, mipushmessage mipushmessage) {//used to receive a transmission message sent by the server to the client, which is triggered by a pass-through message. }@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 pop up notifications when the foreground is applied to the client will also trigger this callback function}@Override Public void Onreceiveregisterresult(context context, mipushcommandmessage mipushcommandmessage) {//To receive a response from a client after sending a registration command to the server}@Override Public void Oncommandresult(context context, mipushcommandmessage mipushcommandmessage) {//To receive a response from a client after sending a command to the server. Here you can get the results of the various commands, such as registering a service, setting aliases, and so on, where you can implement initialization errors and restart the functions. }
DemoApplication inheriting Application class
The main function of this application class is to set the app ID and key, as well as to register the push service within the OnCreate method.
In this demo, it also has a handler in it to let the broadcast receiver can use it to send toast and let mainactivity print log on TextView.
//user your AppID the key. Private Static FinalString app_id ="1000270";//user your AppID the key. Private Static FinalString App_key ="670100056270";//This tag retrieves the information it needs in ADB logcat, just enter adb logcat at the command line terminal | //Com.xiaomi.mipushdemo Public Static FinalString TAG ="Com.xiaomi.mipushdemo";Private StaticDemohandler Shandler =NULL;Private StaticMainactivity smainactivity =NULL;@Override Public void onCreate() {Super. OnCreate ();//Register the push service and send a broadcast to Demomessagereceiver after successful registration //You can get registration information from the Mipushcommandmessage object parameter in the Demomessagereceiver Oncommandresult method if(Shouldinit ()) {Mipushclient.registerpush ( This, app_id, App_key); } loggerinterface Newlogger =NewLoggerinterface () {@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 =NewDemohandler (Getapplicationcontext ()); } }//This is the method to check whether the process is in the foreground Private Boolean Shouldinit() {Activitymanager am = ((Activitymanager) Getsystemservice (Context.activity_service)); list<runningappprocessinfo> Processinfos = am.getrunningappprocesses (); String mainprocessname = Getpackagename ();intMypid = Process.mypid (); for(Runningappprocessinfo Info:processinfos) {if(Info.pid = = Mypid && mainprocessname.equals (info.processname)) {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 up a local push program, the specific settings are
- Set, revoke aliases: aliases (alias) are user identities other than Regid (auto-generated) and UserAccount, which can be
MiPushClient.setAlias() MiPushClient.unsetAlias() set and revoked through methods and.
- Set up, revoke user account (Useraccoun): Can be
MiPushClient.setUserAccount() MiPushClient.unsetUserAccount() set and revoked by methods and methods.
- SUBSCRIBE, REVOKE Tags: developers can combine their own business characteristics to give users a different label (TOPIC). When the 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 tag. Can be
MiPushClient.subscribe() MiPushClient.unsubscribe() subscribed to and canceled by and.
- Pause, resume push, set push time: Set push time demo used a homemade dialog class Timeintervaldialog to let the user set (forcibly increase the amount of code), can be
MiPushClient.setAcceptTime() set by, pause and resume is through pausePush() and resumePush() set.
However, pause and restore the implementation of the underlying is actually called set push time for 00:00-00:00 and 00:00-23:59 just, but also note that is not in the receiving window is temporarily not receive push messages, once to the receiving time that the message will be pushed to come.
Configuration of the Androidmanifest file
First Xiaomi push support the lowest Android version is 2.2, so to
<uses-sdk android:minSdkVersion="8"/>
Then you need the following permissions:
<uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE" / > <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 change to App package name--- <uses-permission android:name="Com.xiaomi.mipushdemo.permission.MIPUSH_RECEIVE" /> <!--here Com.xiaomi.mipushdemo change to App package name---
Then is to register a bunch of broadcastreceiver (all static registration, because to long-term in the background to run, there are inherited pushmessagereceiver Demomessagereceiver also need to register do not forget) and service, Altogether are 3 broadcast receivers and 4 services.
<service android:enabled = android:process =":p Ushservice " android:name =" Com.xiaomi.push.service.XMPushService "/> <serviceandroid:name="Com.xiaomi.push.service.XMJobService"android: Enabled="true"android:exported="false"android:permission=" Android.permission.BIND_JOB_SERVICE "android:process=":p ushservice " /> <!--Note: This service must be added after version 3.0.1 (including version 3.0.1) <serviceandroid:enabled="true"android:exported="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 version 2.2.5) <receiverandroid:exported="true"android:name=" Com.xiaomi.push.service.receivers.NetworkStatusReceiver " > <intent-filter> <action android:name="Android.net.conn.CONNECTIVITY_CHANGE" /> <category android:name="Android.intent.category.DEFAULT" /> </intent-filter> </receiver> <receiverandroid:exported="false"android:process=":p ushservice" android:name="Com.xiaomi.push.service.receivers.PingReceiver" > <intent-filter> <action android:name="Com.xiaomi.push.PING_TIMER" /> </intent-filter> </receiver> <receiverandroid:name="Com.xiaomi.mipushdemo.DemoMessageReceiver"Android : Exported="true"> <intent-filter> <action android:name="Com.xiaomi.mipush.RECEIVE_MESSAGE" /> </intent-filter> <intent-filter> <action android:name="Com.xiaomi.mipush.MESSAGE_ARRIVED" /> </intent-filter> <intent-filter> <action android:name="Com.xiaomi.mipush.ERROR" /> </intent-filter> </receiver>
As long as the demo AppID and Appkey changed to their own (remember that the package name and application name), and then can receive the corresponding push.
Mobile-ported implementations
Probably understand this demo, you can put millet push to transplant to their own app to achieve their own needs, the transplant should pay attention to:
1. First of all, to the application of the package name, from the Xiaomi push platform to register, get AppID and Appkey.
2. Add the SDK to the Xiaomi feed.
3. androidmanifest files are configured with permissions (permissions in Android6.0 may need to be dynamically acquired), register those service and Broadcastreceiver
4. Remember to initialize the Xiaomi push service when the app starts.
5. Finally, you can inherit a pushmessagereceiver and do whatever you like in it.
6. Finally provide a simplified after I write 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 the line. (Simplified demo address)
Android side Xiaomi push Demo analysis and implementation method