百度云推送介绍
云推送(Push)是百度开放云向开发者提供的消息推送服务;通过利用云端与客户端之间建立稳定、可靠的长连接来为开发者提供向客户端应用推送实时消息服务。
百度云推送服务支持推送三种类型的消息:通知、透传消息及富媒体;支持向所有用户或根据标签分类向特定用户群体推送消息;支持更多自定义功能(如自定义内容、后续行为、样式模板等);提供用户信息及通知消息统计信息,方便开发者进行后续开发及运营。
开发指南
2.创建应用,获取 API Key 及 Secret Key,请参考查看应用密钥。
3.下载sdk,导入sdk包到项目,并添加到bulid path。
4. 在AndroidManifest.xml 中注册响应的receiver和Service。
在Android端,总共实现了三个Receiver和一个Service,
其中,一个Receiver是用来处理注册绑定后接收服务端返回的channelID等信息:
<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>
第二个Receiver是用于接收系统消息以保证PushService正常运行:
<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>
第三个Receiver就是开发者自己实现的用来接收并处理推送消息:
<receiver android:name="your.package.PushMessageReceiver"> <intent-filter> <!-- 接收 push 消息 --> <action android:name="com.baidu.android.pushservice.action.MESSAGE" /> <!-- 接收 bind、setTags 等 method 的返回结果 --> <action android:name="com.baidu.android.pushservice.action.RECEIVE" /> </intent-filter> </receiver>
一个Service就是在后台运行的用于保障与Push Server维持长连接并做相关处理的后台服务:
<service android:name="com.baidu.android.pushservice.PushService" android:exported="true" android:process=" bdservice_v1"/> <!-- push service end -->
5.在入口Activity的onCreate方法中进行推送服务的注册绑定
PushManager.startWork(getApplicationContext(),PushConstants.LOGIN_TYPE_API_KEY, "you_api_key");
6.添加PushMessageReceiver.java处理消息
在开发者自己需要处理的广播接收器中,可以对接收到的推送消息进行处理,Push消息通过 action为com.baidu.android.pushservice.action.MESSAGE的Intent把数据发送给客户端your.package.PushMessageReceiver,消息格式由应用自己决定,PushService只负责把服务器下发的消息以字符串格式透传给客户端。接口调用回调通过action为com.baidu.android.pushservice.action.RECEIVE的Intent 返回给your.package.PushMessageReceiver。
public class PushMessageReceiver extends BroadcastReceiver { public static final String TAG = PushMessageReceiver.class.getSimpleName(); @Override public void onReceive(final Context context, Intent intent) { if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) { //获取消息内容 String message = intent.getExtras().getString( PushConstants.EXTRA_PUSH_MESSAGE_STRING); //消息的用户自定义内容读取方式 Log.i(TAG, "onMessage: " + message); } else if (intent.getAction().equals(PushConstants.ACTION_RECEIVE)) { //处理绑定等方法的返回数据 //PushManager.startWork()的返回值通过PushConstants.METHOD_BIND得到 //获取方法 final String method = intent .getStringExtra(PushConstants.EXTRA_METHOD); //方法返回错误码。若绑定返回错误(非0),则应用将不能正常接收消息。 //绑定失败的原因有多种,如网络原因,或access token过期。 //请不要在出错时进行简单的startWork调用,这有可能导致死循环。 //可以通过限制重试次数,或者在其他时机重新调用来解决。 final int errorCode = intent .getIntExtra(PushConstants.EXTRA_ERROR_CODE, PushConstants.ERROR_SUCCESS); //返回内容 final String content = new String( intent.getByteArrayExtra(PushConstants.EXTRA_CONTENT)); //用户在此自定义处理消息,以下代码为demo界面展示用 Log.d(TAG, "onMessage: method : " + method); Log.d(TAG, "onMessage: result : " + errorCode); Log.d(TAG, "onMessage: content : " + content); } } }
7.云推送提供php、java等Server端的SDK供开发者在自己的服务器上实现推送服务进行定制化管理和操作