Baidu message push SDK (with the simplest push Demo attached)

Source: Internet
Author: User

In the previous article "Baidu message push rest api inquiry", we learned how to use rest api to push messages. In this article, let's take a look at the SDK provided by Baidu message push for us.

Help: http://developer.baidu.com/wiki/index.php? Title = docs/cplat/push/api


Let's take a look at the server SDK.


Directory structure after downloading and unzipping


Let's look at the sample first.

Package test; import com. baidu. yun. channel. auth. channelKeyPair; import com. baidu. yun. channel. client. baiduChannelClient; import com. baidu. yun. channel. exception. channelClientException; import com. baidu. yun. channel. exception. channelServerException; import com. baidu. yun. channel. model. pushBroadcastMessageRequest; import com. baidu. yun. channel. model. pushBroadcastMessageResponse; import com. baidu. yun. channel. model. pushUnicastMessageRequest; import com. baidu. yun. channel. model. pushUnicastMessageResponse; import com. baidu. yun. core. log. yunLogEvent; import com. baidu. yun. core. log. yunLogHandler; public class AndroidPushNotificationSample {public static void main (String [] args) {/*** @ brief Push unicast notification (Android Push SDK intercepts and resolves) message_type = 1 (0 by default) * // 1. set ApiKey/SecretKey String apiKey = "Your apiKey" for the developer platform; String secretKey = "your own secretKey"; ChannelKeyPair pair = new ChannelKeyPair (apiKey, secretKey); // 2. create a BaiduChannelClient instance BaiduChannelClient channelClient = new BaiduChannelClient (pair); // 3. to learn the interaction details, register the YunLogHandler class channelClient. setChannelLogHandler (new YunLogHandler () {@ Override public void onHandle (YunLogEvent event) {System. out. println (event. getMessage () ;}}); try {// 4. create a request object // The ChannelId of the mobile phone end, And the UserId of the mobile phone end. Replace it with 1111111111111. Replace it with your own PushBroadcastMessageRequest request = new PushBroadcastMessageRequest (); // PushUnicastMessageRequest request = new PushUnicastMessageRequest (); request. setDeviceType (3); // device_type => 1: web 2: pc 3: android // 4: ios 5: wp // request. setChannelId (3721876992860457831L); // request. setUserId ("1105477905904433716"); request. setMessageType (1); request. setMessage ("{\" title \ ": \" bowl of dried meat \ ", \" description \ ": \" Welcome to CSDN blog \"}"); // 5. call the pushMessage interface/* PushUnicastMessageResponse response = channelClient. pushUnicastMessage (request); */PushBroadcastMessageResponse response = channelClient. pushBroadcastMessage (request); // 6. system successfully pushed for authentication. out. println ("push amount:" + response. getSuccessAmount ();} catch (ChannelClientException e) {// handle client error exception e. printStackTrace ();} catch (ChannelServerException e) {// handle server error exception System. out. println (String. format ("request_id: % d, error_code: % d, error_message: % s", e. getRequestId (), e. getErrorCode (), e. getErrorMsg ()));}}}
The example is very simple. It can be seen from the name, so it won't be too long here.

Next let's take a look at the client SDK

Download the Android SDK and decompress it as follows:

From the user manual, we can see that the Android Push service runs in the following service mode. If a mobile phone integrates multiple Baidu Push services, in order to reduce memory and power consumption, there is only one backend service to share the Push channel.

Next, let's take a look at the user manual:

According to the description in the user manual, I made a simplest Demo, which can fully meet general requirements.


MainActivity. java

Public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // Log On As apikey, which is usually put in PushManager in onCreate of the main Activity. startWork (getApplicationContext (), PushConstants. LOGIN_TYPE_API_KEY, "Your Own apikey ");}}
PushReciver. java

Package com. example. baidulotterypush; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. util. log; import android. widget. toast; import com. baidu. android. pushservice. pushConstants; public class MyPushMessageReceiver extends BroadcastReceiver {private static final String TAG = "large bowl"; @ Overridepublic void onReceive (Context context, Intent intent) {if (intent. getAction (). equals (PushConstants. ACTION_MESSAGE) {} else if (intent. getAction (). equals (PushConstants. ACTION_RECEIVE) {} else if (intent. getAction (). equals (PushConstants. ACTION_RECEIVER_NOTIFICATION_CLICK) {Log. I (TAG, "title =" + intent. getStringExtra (PushConstants. extra_icationication_title); Log. I (TAG, "content =" + intent. getStringExtra (PushConstants. extra_icationication_content ));}}}
Running result:


Demo: http://download.csdn.net/detail/lxq_xsyu/6954373



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.