Reprint Please specify source : http://blog.csdn.net/yianemail/article/details/47044019
Baidu Cloud Message push, the processing of the message on the client and the way the message is displayed is based on the notification bar message prompt.
But this is obviously not the effect we want, we want to use it to achieve chat, that is, in the communication chat interface how to update the push message in real-time, with the continuous acceptance of the message implementation of the chat.
One: We know the use of Baidu News push, to achieve their own pushmessagereceiver. It is important to have three methods, namely:
Public class Baidupushreceiverextends pushmessagereceiver
1,
/**
* Onbind: Get the result of binding request
*/
@Override
Public void Onbind (context context,int errorCode, String AppID,
String userId, String channelid, String RequestID) {}
2,
/**
* OnMessage: Receive a pass-through message
*/
@Override
Public void OnMessage (context context, String message,
String customcontentstring) {}
3,
/**
* onnotificationarrived: Message on the client in the form of a notification
*/
@Override
Public void onnotificationarrived (context context, String title,
String description, String customcontentstring) {}
onbind: In this function can get binding results, that is, you can get the UserID and Channelid (Java background is also based on these two information to implement their own push)
OnMessage: In this function is to process the transmission of the message, what is a pass-through, that is, you can directly when the application is running to accept messages, rather than as a title bar notification in the form of the display. And that's what we're focusing on.
Onnotificationarrived: Push messages in the form of notifications.
Two: Chat interface is a ListView, we have to do is in the message through the OnMessage function inside, to determine whether the current application is starting, if it is starting, directly to the received message updated in the chat interface. (If you don't know how to start it, notice it in the form of a title bar notification.) )
to determine if the app is starting:
public static Boolean Isrunningapp (context context, String PackageName) { Boolean isapprunning = false; Activitymanager am = (activitymanager) context.getsystemservice (context.activity_service); for (Runningtaskinfo info:am.getRunningTasks) { if (Info.topActivity.getPackageName (). Equals (PackageName) && info.baseActivity.getPackageName (). Equals (PackageName)) {isapprunning = True;break;}} return isapprunning;}
Every time after receiving the message in the chat interface to implement the update, this important point, if we want to jump interface is mainactivity, then mainfest.xml the mainactivity boot mode is set to:
Android:launchmode="singletask"
Jump Interface : Then in the chat interface Onresume function for chat interface update processing
public void jumpactivity (context context, String Msg) {comment.msg = msg;intent Intent = new Intent (); Intent.setclass (cont Ext.getapplicationcontext (), mainactivity.class); Intent.addflags (Intent.flag_activity_new_task); Context.getapplicationcontext (). StartActivity (intent);}
Third: Use Java background push message content to fixed client (according to the document, add the corresponding jar):
four: Effect
Run the Java background and send the message content "Hello" to the client:
run Java background, send message content "Iam ok,are you?" To the client:
This completes the real-time communication chat interface implementation effect.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Baidu Cloud message push mechanism in the Instant Messaging chat interface information processing, tips.