java实现百度云推送送怎么实现客户端之间的通讯

Sina Visitor System百度云推送如何将安卓客户端的channelId和userId传到java端服务器
[问题点数:60分]
本版专家分:0
结帖率 50%
CSDN今日推荐
本版专家分:5079
2015年9月 移动开发大版内专家分月排行榜第一
本版专家分:30
结帖率 75%
匿名用户不能发表回复!|
CSDN今日推荐16:07 提问
自己的服务器和百度云涂推送的连接问题
现在想通过第三方平台来实现消息的推送.那么问题来了:自己的服务器要怎么和百度云推送连接,才能实现服务器把消息推给百度云然后由百度云推送到客户端呢?
在此先谢过各位热心伙伴.
按赞数排序
百度云本身就有推送接口,你往对应接口发送数据不就可以了
现在百度下载的Baidu-Push-SDK-Java-1.1.2这个SDK这么使用,求指教,里面很多包,我也找不到相对应的文档说明
想要实现web消息推送,用Goeasy吧,这都是我最近才发现的一个专业做web消息推送的工具,还不错,服务器很稳定,中英文都有,网址就是
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐专注Android移动开发,热爱分享,支持开源
【第三方SDK】使用百度云推送实现推送功能详解
之前介绍过如何使用shareSDK实现新浪微博分享功能,今天介绍如何使用百度云推送SDK实现Android手机后台推送功能。
运行效果如下
第一步,如果使用百度的SDK,当然要先成为百度的开发者啦,这个就不详述了。成为开发者之后,我们要建立一个应用,如下图所示
第二步,创建好应用之后,我们点击开方者服务管理,进入工程管理页面,然后点击左侧云推送,进入云推送功能页面,具体如下图
进入云推送详细页面之后,我们点击推送设置,设置好我们的应用的包名,然后点击快速实例,将系统给我们产生的示例代码下载下来
下载实例代码
第三步,将实例代码导入Eclipse,开始准备整合。
实例代码结构如下
经过整合之后,我们的Demo代码如下
主要有这么几个注意点:
1.MainActivity中代码如下
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initWithApiKey();
// 无账号初始化,用api key绑定
private void initWithApiKey() {
PushManager.startWork(getApplicationContext(),
PushConstants.LOGIN_TYPE_API_KEY,
"FKkvaMToo1wkeIFEcH7udatc");
在主界面要进行api key的绑定,“FKkvaMToo1wkeIFEcH7udatc”这一串数值是应用的api key,在下面这个地方可以获取
2.在MyApplication中,我们需要自定义Application,并按照下面实现(不光有这种形式,更多信息还是看百度SDK的说明文档,说得很详细)
public class MyApplication extends FrontiaApplication {
public void onCreate() {
super.onCreate();
自定义Application之后,我们需要在清单文件中进行设置
&application
android:name="com.example.baidupush.MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" &3.MyPushMessageReceiver这个类是用来接收服务器发送来的请求的,我这里直接把实例代码中的拷贝了过来,具体功能实现还需要自己修改
4.Utils这个类不是关键代码,可以无视
5.必须把libs文件夹下面的资源包拷过来
6.如果要实现富媒体推送,还需要把资源图片和布局也拷贝过来
7.清单文件配置很重要,下面是配置代码和说明
&?xml version="1.0" encoding="utf-8"?&
&manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.baidupush"
android:versionCode="1"
android:versionName="1.0" &
android:minSdkVersion="14"
android:targetSdkVersion="19" /&
&!-- 推送权限,必须加 --&
&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" /&
&application
android:name="com.example.baidupush.MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" &
android:name="com.example.baidupush.MainActivity"
android:launchMode="singleTask"
android:label="@string/app_name" &
&intent-filter&
&action android:name="android.intent.action.MAIN" /&
&category android:name="android.intent.category.LAUNCHER" /&
&/intent-filter&
&/activity&
&!-- push应用定义消息receiver声明 ,这是对前面MyPushMessageReceiver的注册--&
&receiver android:name="com.example.baidupush.MyPushMessageReceiver" &
&intent-filter&
&!-- 接收push消息 --&
&action android:name="com.baidu.android.pushservice.action.MESSAGE" /&
&!-- 接收bind,unbind,fetch,delete等反馈消息 --&
&action android:name="com.baidu.android.pushservice.action.RECEIVE" /&
&!-- 接收notification的点击事件 --&
&action android:name="com.baidu.android.pushservice.action.notification.CLICK" /&
&/intent-filter&
&/receiver&
&!-- push必须的receviver和service声明 ,不用修改,直接拷贝--&
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&
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&
android:name="com.baidu.android.pushservice.PushService"
android:exported="true"
android:process=":bdservice_v1" &
&intent-filter&
&action android:name="com.baidu.android.pushservice.action.PUSH_SERVICE" /&
&/intent-filter&
&/service&
&!-- push结束 --&
&/application&
&/manifest&这样,当我们在百度的云推送后台发送推送信息的时候,客户端就可以收到来自我们后台的推送了。
除此之外,百度云推送还有一些其他功能,比如设置点击notification之后的显示界面、响声,富文本推送,自定义notification显示样式等等,如果想了解这些,还是下载SDK文档,文档说的很清楚。
CSDN的上传资源网页挂掉了,先不传代码了。
没有更多推荐了,基于百度云推送的实时通信客户端实现(二) - 菜菜翠翠.java - 推酷
基于百度云推送的实时通信客户端实现(二) - 菜菜翠翠.java
上一篇说了欢迎和登陆界面的实现,现在来说一下关于聊天界面的搭建,整体界面采用了一个ListView,主要的布局很简单,在这里使用了ListView自定义是Adapter的写法
同时,我构造了一个TaliListInfo的类来和Adapter对应
闲话不多说,直接上代码
1 package com.demo.A
3 public class TalkListInfo {
private int isN
public String getName() {
public void setName(String name) {
this.name =
public String getDate() {
public void setDate(String date) {
this.date =
public String getItem() {
public void setItem(String item) {
this.item =
public int getIsNew() {
return isN
public void setIsNew(int isNew) {
this.isNew = isN
public TalkListInfo() {
public TalkListInfo(String name, String date, String item, int isNew) {
this.name =
this.date =
this.isNew = isN
this.item =
Adapter的构造过程
package com.demo.A
import java.util.L
import com.tuisong.R;
import android.content.C
import android.database.DataSetO
import android.view.LayoutI
import android.view.V
import android.view.ViewG
import android.widget.BaseA
import android.widget.LinearL
import android.widget.TextV
public class NewAdapterView extends BaseAdapter {
@SuppressWarnings(&unused&)
private static final String TAG = NewAdapterView.class.getSimpleName();
private List&MessageEnity&
// 构造函数
public NewAdapterView(Context context, List&MessageEnity& list) {
this.context =
this.coll =
public boolean areAllItemsEnabled() {
return false;
public boolean isEnabled(int arg0) {
return false;
public int getCount() {
// TODO Auto-generated method stub
return coll.size();
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return coll.get(arg0);
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
public int getItemViewType(int position) {
public View getView(int positon, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
MessageEnity entity = coll.get(positon);
int itemLayout = entity.getLayoutID();
LinearLayout layout = new LinearLayout(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(itemLayout, layout, true);
TextView tvName = (TextView) layout.findViewById(R.id.person_name);
tvName.setText(entity.getfromUser());
TextView tvData = (TextView) layout.findViewById(R.id.time_say);
tvData.setText(entity.getTime());
TextView tvItem = (TextView) layout.findViewById(R.id.item_say);
tvItem.setText(entity.getItem());
public int getViewTypeCount() {
return coll.size();
public boolean hasStableIds() {
return false;
public boolean isEmpty() {
return false;
public void registerDataSetObserver(DataSetObserver observer) {
public void unregisterDataSetObserver(DataSetObserver observer) {
&同时,还需要写一个对应的布局来作为ListView的Adapter的布局
1 &?xml version=&1.0& encoding=&utf-8&?&
2 &LinearLayout xmlns:android=&http://schemas.android.com/apk/res/android&
xmlns:tools=&http://schemas.android.com/tools&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:background=&#eeeeee&
android:orientation=&horizontal&
tools:context=&com.baidu.push.example.CustomActivity& &
&ImageView
android:layout_width=&64dp&
android:layout_height=&64dp&
android:background=&@drawable/retouxiang&
android:contentDescription=&@string/app_name&
android:scaleType=&fitXY& /&
&LinearLayout
android:layout_width=&fill_parent&
android:layout_height=&64dp&
android:orientation=&vertical& &
&LinearLayout
android:layout_width=&fill_parent&
android:layout_height=&32dp&
android:orientation=&horizontal& &
android:id=&@+id/tv_name&
android:layout_width=&0dp&
android:layout_height=&32dp&
android:layout_weight=&1&
android:text=&@string/app_name&
android:textColor=&#000000& /&
android:id=&@+id/tv_time&
android:layout_width=&0dp&
android:layout_height=&32dp&
android:layout_weight=&1&
android:text=&@string/app_name&
android:textColor=&#000000& /&
&/LinearLayout&
android:id=&@+id/tv_item&
android:layout_width=&fill_parent&
android:layout_height=&32dp&
android:text=&@string/app_name&
android:textColor=&#000000& /&
&/LinearLayout&
52 &/LinearLayout&
同时,因为只是个Demo,我的头像没有下载的功能,采用了一个空白的头像图片,取消了下载功能,效果如图
&同时,因为聊天记录保存在SQLite中,所以,需要自己实现一个DBhelper的类继承SQLiteOpenHelper,代码如下:
1 package com.tuisong.
3 import java.util.ArrayL
4 import java.util.HashS
5 import java.util.L
6 import com.demo.Adapter.MessageE
7 import com.demo.Adapter.TalkListI
9 import android.annotation.SuppressL
10 import android.content.ContentV
11 import android.content.C
12 import android.database.C
13 import android.database.sqlite.SQLiteD
14 import android.database.sqlite.SQLiteOpenH
16 public class DBHelpter extends SQLiteOpenHelper {
private final static String DATABASE_NAME = &chat_record_db&;
private final static String TABLE_NAME = &chat_table&;
private final static String FROM_USER = &fromuser&;
private final static String TO_USER = &touser&;
private final static String TIME = &time&;
private final static String CHAT_CONTENT = &content&;
private final static String LAYOUT_ID = &layoutid&;
@SuppressWarnings(&unused&)
private final static String CHAT_ID = &id&;
private String openid = null;
public DBH
@SuppressLint(&NewApi&)
public DBHelpter(Context context) {
super(context, DATABASE_NAME, null, 1);
System.out.println(&the data has been created&);
// TODO Auto-generated constructor stub
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sql = &CREATE TABLE if not exists
chat_table ( id
INTEGER PRIMARY KEY
AUTOINCREMENT, fromuser TEXT,
TEXT, content TEXT, layoutid TEXT);&;
db.execSQL(sql);
System.out.println(sql);
//更新数据库,因为我没有数据的修改,所以不需要实现此方法
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
// db.execSQL(&drop table if exists& + TABLE_NAME);
// onCreate(db);
//插入数据
public void Insert(MessageEnity message) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues value = new ContentValues();
value.put(FROM_USER, message.getfromUser());
value.put(TO_USER, message.gettomUser());
value.put(TIME, message.getTime());
value.put(CHAT_CONTENT, message.getItem());
value.put(LAYOUT_ID, message.getLayoutID());
long ss = db.insert(TABLE_NAME, null, value);
System.out.println(&has been insert in db & + ss);
// 消息内容
public List&MessageEnity& findPersonMessage(String openid, String userid) {
List&MessageEnity& messages = new ArrayList&MessageEnity&();
SQLiteDatabase db = this.getReadableDatabase();
System.out.println(&dsdfsw& + db.toString());
String sql = &select * from chat_table where (fromuser='& + openid
+ &' and touser='& + userid + &') or (touser='& + openid
+ &' and fromuser='& + userid + &') order by time &;
Cursor cursor = db.rawQuery(sql, null);
cursor.moveToFirst();
System.out.println(&the find message is & + cursor.getCount());
if (cursor.getCount() & 0) {
if (cursor.getCount() & 10) {
cursor.moveToPosition(cursor.getCount() - 10);
for (int i = 0; i & 10; i++) {
MessageEnity message = new MessageEnity();
message.setfroUser(cursor.getString(cursor
.getColumnIndex(FROM_USER)));
message.setItem(cursor.getString(cursor
.getColumnIndex(CHAT_CONTENT)));
message.setLayoutID(cursor.getInt(cursor
.getColumnIndex(LAYOUT_ID)));
message.setTime(cursor.getString(cursor
.getColumnIndex(TIME)));
message.settoUser(cursor.getString(cursor
.getColumnIndex(TO_USER)));
messages.add(message);
cursor.moveToNext();
cursor.moveToFirst();
for (int i = 0; i & cursor.getCount(); i++) {
MessageEnity message = new MessageEnity();
message.setfroUser(cursor.getString(cursor
.getColumnIndex(FROM_USER)));
message.setItem(cursor.getString(cursor
.getColumnIndex(CHAT_CONTENT)));
message.setLayoutID(cursor.getInt(cursor
.getColumnIndex(LAYOUT_ID)));
message.setTime(cursor.getString(cursor
.getColumnIndex(TIME)));
message.settoUser(cursor.getString(cursor
.getColumnIndex(TO_USER)));
messages.add(message);
cursor.moveToNext();
return null;
// 列出消息列表
public List&TalkListInfo& findList(String touser) {
List&TalkListInfo& infos = new ArrayList&TalkListInfo&();
SQLiteDatabase db = this.getReadableDatabase();
System.out.println(&the ver sion is& + db.getVersion());
String sql = &select fromuser, time , content,count(distinct fromuser) from chat_table where touser='&
+ touser + &' group by fromuser order by time desc&;
// Cursor cursor = db.query(TABLE_NAME, new String[] { FROM_USER, TIME,
// CHAT_CONTENT }, null, null, null, null, TIME);
Cursor cursor = db.rawQuery(sql, null);
System.out.println(&the ver num is& + cursor.getCount());
cursor.moveToFirst();
if (cursor.getCount() & 0) {
System.out.println(&the tag num &);
for (int i = 0; i & cursor.getCount(); i++) {
if (cursor.getString(cursor.getColumnIndex(FROM_USER)).equals(
openid) != true) {
TalkListInfo info = new TalkListInfo();
info.setName(cursor.getString(cursor
.getColumnIndex(FROM_USER)));
info.setDate(cursor.getString(cursor.getColumnIndex(TIME)));
info.setItem(cursor.getString(cursor
.getColumnIndex(CHAT_CONTENT)));
infos.add(info);
cursor.moveToNext();
System.out.println(&tag2&);
removeDuplicate(infos);
System.out.println(&tag3&);
public static void removeDuplicate(List&TalkListInfo& list) {
HashSet&TalkListInfo& h = new HashSet&TalkListInfo&(list);
list.clear();
list.addAll(h);
System.out.println(list);
DBHelper.java
&好了到了这里,准备工作已经基本完成了,现在就要编写界面的后台代码了
1 package com.baidu.push.
3 import java.util.ArrayL
4 import java.util.HashM
5 import java.util.L
6 import java.util.M
8 import com.baidu.android.pushservice.PushC
9 import com.baidu.android.pushservice.PushM
10 import com.demo.Adapter.ListA
11 import com.demo.Adapter.TalkListI
12 import com.tuisong.R;
13 import com.tuisong.Save_R
14 import com.tuisong.U
15 import com.tuisong.db.DBH
17 import android.app.A
18 import android.app.NotificationM
19 import android.content.BroadcastR
20 import android.content.C
21 import android.content.I
22 import android.content.SharedP
23 import android.os.B
24 import android.view.M
25 import android.view.MenuI
26 import android.view.V
27 import android.widget.AdapterView.OnItemClickL
28 import android.widget.AdapterV
29 import android.widget.ListV
30 import android.widget.RelativeL
32 public class PushDemoActivity extends Activity implements OnItemClickListener {
private static ListV
private static List&TalkListInfo&
private static ListA
RelativeLayout mainLayout = null;
public static int initialCnt = 0;
public Save_Read save = new Save_Read();
public static Map&String, String&
public static DBH
public static S
public SharedP
public static NotificationM
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
@SuppressWarnings(&unused&)
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
@SuppressWarnings(&unused&)
MyReceiver receiver = new MyReceiver();
sp = this.getSharedPreferences(&key&, 0);
SharedPreferences.Editor edtior = sp.edit();
edtior.putString(&SSP&, &1&);
edtior.commit();
list = (ListView) findViewById(R.id.list);
help = new DBHelpter(this);
map = new HashMap&String, String&();
userid = sp.getString(&Login&, null);
System.out.println(&the
id is & + userid);
talk = new ArrayList&TalkListInfo&();
talk = help.findList(userid);
if (talk != null) {
for (TalkListInfo t : talk) {
map.put(&name&, t.getName());
map.put(&time&, t.getDate());
map.put(&content&, t.getItem());
adapter = new ListAdapter(this, talk);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView&?& arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent = new Intent(PushDemoActivity.this,
TalkActivity.class);
intent.putExtra(&name&, talk.get(arg2).getName());
intent.putExtra(&userid&, userid);
Until.setString(talk.get(arg2).getName());
startActivity(intent);
// 以apikey的方式登录,一般放在主Activity的onCreate中
PushManager.startWork(getApplicationContext(),
PushConstants.LOGIN_TYPE_API_KEY,
Utils.getMetaValue(PushDemoActivity.this, &api_key&));
public void onStart() {
super.onStart();
public void onResume() {
super.onResume();
SharedPreferences.Editor edtior = sp.edit();
edtior.putString(&SSP&, &0&);
edtior.commit();
protected void onNewIntent(Intent intent) {
// 如果要统计Push引起的用户使用应用情况,请实现本方法,且加上这一个语句
setIntent(intent);
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
SharedPreferences.Editor edtior = sp.edit();
edtior.putString(&SSP&, &0&);
edtior.commit();
public void onStop() {
super.onStop();
PushManager.activityStoped(this);
// SharedPreferences.Editor edtior = sp.edit();
// edtior.putString(&Login&, &-1&);
// edtior.commit();
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// SharedPreferences.Editor edtior = sp.edit();
// edtior.putString(&Login&, &-1&);
// edtior.commit();
public void onItemClick(AdapterView&?& arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
public boolean onCreateOptionsMenu(Menu menu) {
// 调用父类方法来加入系统菜单
// 虽然目前android还没有系统菜单,但是为了兼容到以后的版本,最好加上
super.onCreateOptionsMenu(menu);
// 添加菜单项(多种方式)
// 1.直接指定标题
menu.add(1, 0, 1, &退出&);
// 3.显示指定菜单项的组号、ID、排序号、标题
// 如果希望显示菜单,请返回true
return true;
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// 响应每个菜单项(通过菜单项的ID)
// do something here
// 对没有处理的事件,交给父类来处理
return super.onOptionsItemSelected(item);
// 返回true表示处理完菜单项的事件,不需要将该事件继续传播下去了
return true;
//修改当前活动activity的状态
public void change() {
SharedPreferences.Editor edtior = sp.edit();
edtior.putString(&Login&, &-1&);
edtior.commit();
PushDEmoActivity.java
到这里,对话列表的界面搭建已经完成了,下一篇文章里面介绍聊天界面写法
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致}

我要回帖

更多关于 java消息推送到客户端 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信