Android uses SIGNALR for push services

Source: Internet
Author: User

The app I do needs to implement SIGNALR push service

There are two ways

A family has integrated it with video tutorials

The program that is https://www.youtube.com/watch?v=f9ih_M7PC10 downloaded is in this video, but the update I did not successfully completed HTTPS://WWW.YOUTUBE.COM/WATCH?V=F9IH_M7PC10

The second type of self-integration

Two steps.

First step, I'm in the activity. Implement data that is pushed by the receiving server

First download the java-client address above Google Https://github.com/SignalR/java-client

Download the Basic-http-client-android-0.88.jar address again Basic-http-client-android-0.88.jar

These are the preparations, and here's what you need to build the package jar

1.Open Android Studio, click "Import non-android Studio Project" and select the cloned directory

Build the project.

The Signalr-client-sdk.jar'll be generated inside The/signalr-client-sdk/build/libs folder

The Signalr-client-sdk-android.aar'll be generated inside The/signalr-client-sdk-android/build/outputs/aar folder

2. The only jar here with Eclipse is useful, and AAR has to do it by itself, and here's what I'm going to do.

Unzip the AAR to remove the layout part repack. jar Rename *.aar to *.zip and extract the jar. Meaning to rename AAR first to extract the jar inside the zip


3. Packaged longpolling in the video. Jar My side is not the same so I can't pack up, choose to put all the classes used in a package in fact it is equivalent to unpack the package. Of course you can re-pack it later in Eclipse.


4. The programming phase is very simple to invoke in activity.

public class Signalractivity extends Activity {


Private ImageButton GoBack;
Private TextView what;
Private Ihubproxy hub = null;
PRIVATE Hubconnection conn = null;
Private final static String
Hub_url= "Http://192.168.1.200:82/signalr/hubs";
@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.signalr_layout);
GoBack = (ImageButton) Findviewbyid (r.id.goback);
Goback.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
SignalrActivity.this.finish ();
}
});
what = (TextView) Findviewbyid (r.id.what);
conn = new Hubconnection (Icons.hub_url, this,
New Longpollingtransport ()) {
@Override
public void OnError (Exception Exception) {
}
@Override
public void OnMessage (String message) {
What.settext (message);
}
@Override
public void onstatechanged (Statebase oldstate, Statebase newstate) {
}
};
Conn.getheaders (). Put ("Cookie",
"Bearertoken=" + Sharedpref.gettoken (Getapplicationcontext ()));
BeginConnect ();
}


private void BeginConnect () {
TODO auto-generated Method Stub
try {
HUB = conn. Createhubproxy ("Clienthub");
} catch (Operationapplicationexception e) {
E.printstacktrace ();
}
Hub. On ("Debug1", new Hubondatacallback () {//addnewmessagetopage
@Override
public void onreceived (Jsonarray args) {
for (int i = 0; i < args.length (); i++) {
Toast.maketext (Getapplicationcontext (), "Chen" +args, Toast.length_short
). Show ();
}
}
});
Conn. Start ();
}
}

Here you can see the message of the push.

The second step is to implement notification bar notification push message when you do not run the program

That is, you have to write a service first, it is very simple, is equivalent to change the activity into service

public class Signalrpushservice extends Service {
Private String message;
Private Boolean quit;
Private Mybinder binder = new Mybinder ();
Private Ihubproxy hub = null;
PRIVATE Hubconnection conn = null;
Number of notification bar messages
private int messagenotificationid = 0;
@SuppressLint ("Handlerleak")
Private Handler Handler = new Handler () {
@SuppressWarnings ("deprecation")
public void Handlemessage (Message msg) {
if (messagenotificationid<5) {
Notificationmanager manager = (Notificationmanager) getsystemservice (Context.notification_service);
Build a Notification object (there are three parameters that need to be passed, namely, icon, title, and time)
Notification Notification = new Notification (
R.drawable.ic_launcher, "notice", System.currenttimemillis ());
Intent intent1 = new Intent (Signalrpushservice.this,
Allalarmactivity.class);//Jump to view activity
Pendingintent pendingintent = pendingintent.getactivity (
Signalrpushservice.this, 0, intent1, 0);
Notification.setlatesteventinfo (Getapplicationcontext (), "Alarm Prompt",
message, pendingintent);
Notification.flags = notification.flag_auto_cancel;//automatically disappears when clicked
Notification.defaults = notification.default_sound;//Sound Default
Manager.notify (messagenotificationid, notification);//launch notification, ID specified by yourself, each notification corresponding unique flag
messagenotificationid++;
}
}
};


public class Mybinder extends Binder//①
{
Public String GetMessage () {
Get the running status of the service: Count
return message;
}
}


@Override
Public IBinder Onbind (Intent arg0) {
return binder;
}


@Override
public void OnCreate () {
Super.oncreate ();
}


@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {
New Thread () {
public void Run () {
Looper.prepare ();
BeginConnect ();//Get messages from the server
Looper.loop ();
};
}.start ();
Return Super.onstartcommand (Intent, flags, Startid);
}


@Override
public void OnDestroy () {
Super.ondestroy ();
This.quit = true;
}


private void BeginConnect () {


TODO auto-generated Method Stub
conn = new Hubconnection (Icons.hub_url, Getbasecontext (),
New Longpollingtransport ()) {
@Override
public void OnMessage (String message) {
SignalrPushService.this.message = message;
Handler.sendemptymessage (0);
}
};
Conn.getheaders (). Put ("Cookie",
"Bearertoken=" + Sharedpref.gettoken (Getapplicationcontext ()));
Quit = false;
while (!quit) {
try {
HUB = conn. Createhubproxy ("Clienthub");
} catch (Operationapplicationexception e) {
E.printstacktrace ();
}
/*
* Hub. On ("Debug1", new Hubondatacallback () {//addnewmessagetopage
*
* @Override public void onreceived (Jsonarray args) {Message msg =
* Handler.obtainmessage (); Msg.what = 1; Handler.sendmessage (msg);
* Toast.maketext (Signalrpushservice.this, args+ "",
* Toast.length_short). Show (); } });
*/
Conn. Start ();
}
}
}

My service is encrypted because of the server, so I have to add a step

conn.getheaders (). Put (" Cookie ",
" bearertoken= "+ Sharedpref.gettoken (Getapplicationcontext ()));

Add a head cookie.

Here is a new thread to start the SIGNALR

This enables the push service to be delivered.

Android uses SIGNALR for push services

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.