Message push Baidu Cloud push Android integration and use method

Source: Internet
Author: User

These two days due to the project needs, research a bit of Baidu Cloud push, originally this matter how much work, but registered Baidu developer account creation application make me egg pain chrysanthemum tight a good burst, these things did not what the technology upgrade, do not do and not, the only way. Fortunately, I spent n more than a millisecond seconds after the splinters came over.

Let's spit it out here. This cloud push although not many things, but for the first contact people still a bit not neat, so certainly to see the official documents, just don't know is Baidu which buddy do document, PDF do also too not thoughtful: English characters inside unexpectedly have Chinese space, copy to own project found Jiangshan a red, Niang, It is not necessarily possible to see what went wrong.

For instance:

What ' s wrong? ....

It may be possible to see through a bug at a glance, but when the blood is misty it is quite a pleasure to have the egg broken.

Start the integration below.

Let's start with how to use it to get a cool one.

One: Use Method 1. Open the admin console and find the project you created

Http://developer.baidu.com/console#app/project

2: Select Cloud Push---Notifications:

Note: User scope:

Everyone sends a notification to all users who install this app

Broadcast groups can send notifications to specific users based on tags

3: Mobile Interface:

Two: Integration method: 1. manifest file configuration ①: Add permissions

<!--the permissions required for Push service operation--

<uses-permission android:name= "Android.permission.INTERNET"/>

<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>

<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>

<uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/>

<uses-permission android:name= "Android.permission.WRITE_SETTINGS"/>

<uses-permission android:name= "Android.permission.VIBRATE"/>

<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name= "Android.permission.DISABLE_KEYGUARD"/>

<uses-permission android:name= "Android.permission.ACCESS_COARSE_LOCATION"/>

<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/>

<!--the permissions required for Push service operation--

②: Receiver received by registered message

<receiver android:name= "Com.zhuim.sgghealth.MyPushMessageReceiver" >

<intent-filter>

<!--receive push messages--

<action android:name= "Com.baidu.android.pushservice.action.MESSAGE"/>

<!--receive the return results of BIND, settags, etc.

<action android:name= "Com.baidu.android.pushservice.action.RECEIVE"/>

<!--Optional. Accept notification Click events, and notification customizations--

<action android:name= "Com.baidu.android.pushservice.action.notification.CLICK"/>

</intent-filter>

</receiver>

<!--used to receive system messages to ensure pushservice uptime

<receiver

Android:name= "Com.baidu.android.pushservice.PushServiceReceiver"

android:process= ": Bdservice_v1" >

<intent-filter>

<action android:name= "Android.intent.action.BOOT_COMPLETED"/>

<action android:name= "Android.net.conn.CONNECTIVITY_CHANGE"/>

<action android:name= "Com.baidu.android.pushservice.action.notification.SHOW"/>

<action android:name= "Com.baidu.android.pushservice.action.media.CLICK"/>

</intent-filter>

</receiver>

<!--push service receives various requests sent by the client--

<!--Note: Registrationreceiver in the 2.1.1 and previous version of the spelling error, for Registratonreceiver, with the new version of the SDK, please change to the following code--

<receiver

Android:name= "Com.baidu.android.pushservice.RegistrationReceiver"

android:process= ": Bdservice_v1" >

<intent-filter>

<action android:name= "Com.baidu.android.pushservice.action.METHOD"/>

<action android:name= "Com.baidu.android.pushservice.action.BIND_SYNC"/>

</intent-filter>

<intent-filter>

<action android:name= "Android.intent.action.PACKAGE_REMOVED"/>

<data android:scheme= "Package"/>

</intent-filter>

</receiver>

<!--Push Service--

<service

Android:name= "Com.baidu.android.pushservice.PushService"

Android:exported= "true"

android:process= ": Bdservice_v1"/>

<!--androidmenifest.xml increased Pushservice configuration--

2: Code file: ①: Make push function work

Add the following code to your main activity to get the push function to work:

Log in as Apikey, typically in the oncreate of the main activity

Pushmanager.startwork (Getapplicationcontext (),

Pushconstants.login_type_api_key, "Byi*********************zli3qlk");

One of the apikey is given when the project was created:

②: Inheriting the Frontiaapplication class

public class MyApp extends Frontiaapplication {

Public list<activity> activitieslist;

@Override

public void OnCreate () {

TODO auto-generated Method Stub

Activitieslist = new arraylist<activity> ();

Super.oncreate ();

}

}

③: Custom Broadcast recipients:

public class Mypushmessagereceiver extends Broadcastreceiver {

@Override

public void OnReceive (final context context, Intent Intent) {

if (Intent.getaction (). Equals (Pushconstants.action_message)) {

Get message Content

String message = Intent.getextras (). GetString (

pushconstants.extra_push_message_string);

} else if (Intent.getaction (). Equals (pushconstants.action_receive)) {

Handling return data for methods such as bindings

The return value of Pushmanager.startwork () is obtained by pushconstants.method_bind

Final String method = Intent.getstringextra (Pushconstants.extra_method);

method returns an error code. If the binding returns an error (not 0), the app will not receive the message properly.

There are several reasons for a binding failure, such as network reasons, or access token expiration.

Do not make simple startwork calls in case of an error, which can lead to a dead loop.

This can be resolved by limiting the number of retries, or by re-invoking them at other times.

int errorCode = Intent.getintextra (pushconstants.extra_error_code,pushconstants.error_success);

String content = "";

if (Intent.getbytearrayextra (pushconstants.extra_content) = null)

Content = new String (Intent.getbytearrayextra (pushconstants.extra_content));

The user processes the message in this custom: TODO Something

Notify user of Click event handling

} else if (Intent.getaction (). Equals (

Pushconstants.action_receiver_notification_click)) {

String title = Intent.getstringextra (Pushconstants.extra_notification_title);

String content = Intent.getstringextra (pushconstants.extra_notification_content);

Users in this custom processing click event: TODO Something

}

}

}

④: Import the jar package and so files required by Baidu Cloud push:

Libs import Pushservice-3.2.0.jar and add to build path

Import libbdpush_v1_0.so under Libs/armeabi

Message push Baidu Cloud push Android integration and use method

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.