Android Push Jpush

Source: Internet
Author: User

Package Com.seven7.mypushdome;import Org.json.jsonexception;import Org.json.jsonobject;import Cn.jpush.android.api.jpushinterface;import Android.app.notification;import Android.app.NotificationManager; Import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.os.bundle;import Android.support.v4.app.notificationcompat;import Android.util.log;public Class  Mypushreceiver extends Broadcastreceiver {private static final String TAG = "Jpush";p ublic mypushreceiver () {}/** * @param KDKAJS * */@Overridepublic void OnReceive (context context, Intent Intent) {Bundle bundle = Intent.getextras (); LOG.D (TAG, "[Myreceiver] onreceive-" + intent.getaction () + ", Extras:" + printbundle (bundle));//Registration ID Broadcast This is more important, because all the push service Services must be registered to receive the message//registration is automatically completed in the background, if the registration is not successful, then all the push method can not be normal//This registration of the message may be sent to their own business Server. That is, when the user logs on, send to their server if (JPushInterface.ACTION_REGISTRATION_ID.equals (Intent.getaction ())) {String Regid = Bundle.getstring (Jpushinterface.extra_regisTRATION_ID); LOG.D (TAG, "[Myreceiver] receive Registration ID:" + regid);//Send the registration ID to your server ...} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals (Intent.getaction ())) {//All custom messages will enter this method LOG.D (TAG, "[ Myreceiver] received a push down custom message: "+ bundle.getstring (jpushinterface.extra_message));p rocesscustommessage (context, bundle );} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals (Intent.getaction ())) {//All of the normal pushes will go into this section, And Jpush himself will do notification display//We just save the Notificationid, or saved to the local, for the sorting of the list. LOG.D (TAG, "[Myreceiver] receives push-down notifications"), int notifactionid = Bundle.getint (jpushinterface.extra_notification_id); LOG.D (TAG, "[Myreceiver] receives the ID of the push down notification:" + Notifactionid);} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals (Intent.getaction ())) {//NOTIFICATION click Open, The main target is the normal push message log.d (TAG, "[Myreceiver] user clicked Open the Notification");//Open the custom activityintent i = new Intent (context, mainactivity.class); I.putextras (bundle);//I.setflags (Intent.flag_activity_new_task); I.setflags (intent.flag_activity_new_task| Intent.flag_activity_clear_top); context.startactivity (i);} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals (Intent.getaction ())) {LOG.D (TAG, "[Myreceiver] user receives a rich PUSH CALLBACK: "+ bundle.getstring (Jpushinterface.extra_extra));//Here according to the Jpushinterface.extra_extra content processing code, such as opening a new activity,//to open a webpage, etc...} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals (Intent.getaction ())) {Boolean connected = Intent.getbooleanextra (Jpushinterface.extra_connection_change, false); LOG.E (TAG, "[Myreceiver]" + intent.getaction () + "connected state change to" + connected);} else {log.d (TAG, "[Myreceiver] Unhandled Intent-" + intent.getaction ());}} Print all intent extra data private static String Printbundle (bundle bundle) {StringBuilder sb = new StringBuilder (); for (stri ng Key:bundle.keySet ()) {if (Key.equals (jpushinterface.extra_notification_id)) {sb.append ("\nkey:" + key + ", Value:" + Bundle.getint (key));} else if (key.equals (Jpushinterface.extra_connection_change)) {sb.append ("\nkey: "+ key +", Value: "+ Bundle.getboolean (key));} else {sb.append ("\nkey:" + key + ", Value:" + bundle.getstring (key));}} return sb.tostring ();} Send msg to mainactivity/** * May often be used to get additional custom fields * * @param context * @param bundle */private void Processcustommes Sage (context context, bundle bundle) {//if (Mainactivity.isforeground) {//check whether the current software is in the foreground//use Jpushinterface.extra_ The message mechanical can push the message to get string message = Bundle.getstring (jpushinterface.extra_message);//A point that may be used frequently, get additional custom fields,// This string is the JSON format for your own server to pass certain properties and configurations to specific clients, for example, to display some numbers, specific events, or to access a particular URL, using extras//such as displaying order information, a specific list of items, Specific Consulting URL string extras = bundle.getstring (Jpushinterface.extra_extra);//display of content using broadcasts or notifications Notificationcompat.builder Builder = new Notificationcompat.builder (context); Builder.setcontenttext (message). Setsmallicon (R.drawable.ic_ launcher); Builder.setcontenttitle ("Message"); Builder.setdefaults (Notification.default_sound); LOG.I ("Jpush", Extras + "~ ~"); int drawresid=r.drawable.ic_launcher;int num = 0; String title= "heLlo "; int icontype=0;/** * Custom info: Get * */if (extras! = null) {try {Jsonobject object = new Jsonobject (extras); num = object. Optint ("num"); Title=object.optstring ("title", "Hello"); Icontype=object.optint ("Icontype");} catch (Jsonexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Builder.setcontenttext (title);//different festivals different push icon switch (icontype) {case 0://push icon 1drawresid=r.drawable.ic_launcher; Break;case 1://push icon 2drawresid=r.drawable.ic_launcher;break;default:break;} Builder.setsmallicon (DRAWRESID); if (num > 0) {builder.setnumber (num);} Notification Notification = Builder.build (); Notificationmanager manager = (Notificationmanager) context.getsystemservice (Context.notification_service); Manager.notify (1, notification);//display of content using broadcasts or notifications//Intent msgintent = new Intent (mainactivity.message_received_ ACTION);//Msgintent.putextra (Mainactivity.key_message, MESSAGE);//if (! Exampleutil.isempty (Extras)) {//try {//Jsonobject Extrajson = new Jsonobject (extras);//if (null! = EXTRAJSOn && extrajson.length () > 0) {//Msgintent.putextra (Mainactivity.key_extras, EXTRAS);//}//} catch (Jsonexcep tion e) {////}////}//context.sendbroadcast (msgintent);//}}}

Android Push Jpush

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.