The use of the carrier pigeon push in IOS detailed demo _ios

Source: Internet
Author: User
Tags static class stub eventbus

Recently in the knowledge of the push, using the carrier pigeon is mainly because the backstage with a homing pigeon

Push with a third party push, that is, to build a broadcast receiver on the client, when the server sends messages to the carrier pigeon, the carrier pigeon again sent, broadcast receiver accepted;

I realize that the function is relatively simple, when the app in the running state, will be displayed on the homepage of a window display push message; If the app is not running and the service is not destroyed, show the default notification

So how to display the window in the home page: when the broadcast receiver receives the message I want, in the Observer mode, receive the message in the main interface to send a message

Official Demo Connection: Http://xg.qq.com/xg/help/ctr_help/download

Modified Demo Address: http://download.csdn.net/detail/qq_29774291/9635735

1. According to the official website example to add good permissions and services to some services to change their own package name;

2. Copy the website's broadcast receiver to your own project and add it to the list file;

3. Then add the 3 accesskey from the official website.

<meta-data android:name= "Com.tencent.rdm.uuid" android:value= "eb5fa555d70c3246a4944f55be8c266b"/>
< !--"must" please change your_access_id to app Accessid, 10 digits at the beginning of "21", no spaces in the middle-->
<!--"must" modify the Accessid of the app, 10 digits to the beginning of "21", No space in the middle-->
<meta-data
android:name= "xg_v2_access_id" android:value=
"2100219302"
/> <! --"must" please modify the accesskey for app, the 12-bit string at the beginning of "A", there is no space between-->
<meta-data
android:name= "Xg_v2_access_key"
Android:value= "A15KJ71W9ELC"/>

4. Now registered in the main interface carrier pigeon push is mainly to obtain a token, it can then be passed on to the server, which can now send you a message; Of course, from the official carrier pigeon can send a message, but the message sent from the back of the homing pigeon sometimes can not accept, this is done completely less than the Aurora push good

Xgpushconfig.enabledebug (this, true); Remember to change to false or delete

Pigeon start private String token;
Private message = NULL; private void Xginit () {//TODO auto-generated Method Stub Xgpushconfig.enabledebug (this, true);/////If you need to know if registration is successful, use Regist Erpush (Getapplicationcontext (),//Xgioperatecallback) with callback version//If you need to bind your account, use Registerpush ( Getapplicationcontext (), account) version//can refer to the detailed Development Guide///pass parameters for ApplicationContext//context context =
Getapplicationcontext ();
1. Acquisition of equipment token Handler Handler = new Handlerextension (mainactivity.this);
message = Handler.obtainmessage (); Xgpushmanager.registerpush (Getapplicationcontext (), new Xgioperatecallback () {@Override public void onsuccess (Object data, int flag) {//TODO auto-generated Method Stub log.d ("Jiejie", "+++ Register push sucess.")
Token: "+ data +" "+ flag);
token = data + ""; Message.obj = "+++ Register push sucess.
Token: "+ data;
SYSTEM.OUT.PRINTLN (token);
Message.sendtotarget (); @Override public void Onfail (Object data, int errcode, String msg) {//TODO auto-generated Method Stub log.d ("JiEjie "," +++ Register push fail.
Token: "+ Data +", Errcode: "+ Errcode +", msg: "+ msg"; Message.obj = "+++ Register push fail.
Token: "+ Data +", Errcode: "+ Errcode +", msg: "+ msg;
Message.sendtotarget ();
}
});
private static class Handlerextension extends handler{weakreference<mainactivity> mactivity;
Handlerextension (mainactivity activity) {mactivity = new weakreference<mainactivity> (activity);} @Override
public void Handlemessage (message msg) {//TODO auto-generated Method Stub super.handlemessage (msg);
Mainactivity theactivity = Mactivity.get (); 
if (theactivity = = null) {theactivity = new mainactivity ();} if (msg!= null) {LOG.D (Constants.logtag, msg.obj.toString ());
System.out.println ("ddd" +msg.obj.tostring ()); }
}
}

5. Modify the Ontextmessage method in the broadcast receiver, send the message to the main interface when the message is sent, do not show the notification

But first you need to know if your app is running.

/** * To determine whether to run in the foreground * * @param context * @return/public static Boolean Isrunningforeground (context context) {String pack
Agename = getpackagename (context);
String Topactivityclassname = gettopactivityname (context);
LOG.D ("TAG", "packagename=" + PackageName + ", topactivityclassname=" + topactivityclassname); if (PackageName!= null && topactivityclassname!= null && topactivityclassname.startswith (PackageName)  {LOG.D ("tag", "---> isrunningforeground"); return true;} else {log.d ("tag", "---> isrunningbackground");
False }//Method 2, judged by Runningappprocessinfo class (no additional permissions required): public static boolean isbackground {Activitymanager act
Ivitymanager = (Activitymanager) context.getsystemservice (Context.activity_service);
list<runningappprocessinfo> appprocesses = activitymanager.getrunningappprocesses ();
for (Runningappprocessinfo appprocess:appprocesses) {if (AppProcess.processName.equals (Context.getpackagename ())) { if (appprocess.importance = = Runningappprocessinfo.importance_background) {log.i ("background", appprocess.processname); return true;} else {log.i ("foreground",
Appprocess.processname);
return false;
}} return false; }

6. Notifies the eventbus to implement the Observer mode to send messages in the broadcast receiver

Message pass through @Override public void ontextmessage (context context, Xgpushtextmessage message) {String Text = "Received messages:" + messages
. toString ();
Eventbus.getdefault (). Post (text);
System.out.println (text);
Gets the custom key-value pushtextmessage pushtextmessage = new Pushtextmessage ();
String title = Message.gettitle ();
String content = Message.getcontent ();
Pushtextmessage.settitle (title); 
Pushtextmessage.setcontent (content);
String customcontent = Message.getcustomcontent (); if (customcontent!= null && customcontent.length ()!= 0) {try {//Jsonobject obj = new Jsonobject (customcontent
); Key1 for the foreground configured key//if (!obj.isnull ("key")) {//String value = obj.getstring ("key");//LogUtils.log (LogTag, "Get Cust
Om Value: "+ value";
} customcontent Custom = Com.alibaba.fastjson.JSONObject.parseObject (Customcontent, Customcontent.class); if (custom!= null) {pushtextmessage.setcustomcontent (custom);}//...}
catch (Exception e) {System.out.println (E + "D"); E.printstacktrace ();} Show (cOntext, text);
LOG.D ("Jiejie", "Pushtextmessage:" + pushtextmessage);
Eventbus.getdefault (). Post (Pushtextmessage);
The try {//app processes messages independently ... boolean isforeground = Apputil.isrunningforeground (context);
LOG.D ("Jiejie", Isforeground + "D"); if (isforeground) {Eventbus.getdefault (). Post (pushtextmessage);} else {Notify (context, title, content);}} catch (Exception e) {System.out.println (E + "ddd"); E.printstacktrace ();}

7. Accept the message sent by Eventbus in the main interface, display a window

@Subscribe public
void onmessagereviced (final pushtextmessage pushtextmessage) {
log.d ("Jiejie", "good" + "title" + Pushtextmessage.gettitle () + "content:" +pushtextmessage.getcontent () + "customcontent" + pushtextmessage.getcustomcontent (). Getcmd ());
if (pushtextmessage!= null) {
Showalertdialog (this, pushtextmessage);
}
}
private void Showalertdialog (context context,pushtextmessage text) {
alertdialog.builder dialog = new Alertdialog.builder (context);
Dialog.settitle ("push title");
Dialog.setmessage (Text.getcontent ());
Dialog.setpositivebutton ("Confirm", new Dialoginterface.onclicklistener () {
@Override public
void OnClick ( Dialoginterface arg0, int arg1) {
//TODO auto-generated method stub
}
});
Alertdialog Mdialog = Dialog.create ();
Mdialog.show ();
}
@Override
protected void OnDestroy () {
//TODO auto-generated method Stub
Super.ondestroy ();
Eventbus.getdefault (). Unregister (this);

The above is a small set of iOS to introduce the introduction of the carrier pigeon in the use of the demo, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.