Android 百度推送使用总结
在上班工作中的一个项目中使用到了百度推送,以前对推送一无了解,现将百度推送学习总结分享如下。
1、首先配置权限。
2、客户端实现自己的Receiver(继承自BroadcastReceiver),用于处理当接收到推送消息时的处理响应事件。
<! -- push service client --> <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" /> <! -- 可选。如果不声明,用户点击通知后,默认打开应用,如果在应用中进行了申明,点击通知消息时不会打开应用,应该在自己写的Receiver类里进行处理,如打开某个Activity,同时Receiver会接收到用户点击行为的intent,并获取通知的标题,内容或自定义内容等--> <action android:name=" com.baidu.android.pushservice.action.notification.CLICK” /> </intent-filter> </receiver> 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); }3、配置PushService服务
<! -- push service start --> <! -- 用于接收系统消息以保证 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> <! -- Push 服务接收客户端发送的各种请求--> <receiver android:name="com.baidu.android.pushservice.RegistratonReceiver" 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 android:name="com.baidu.android.pushservice.PushService" android:exported="true" android:process=":bdservice_v1"/> <! -- push service end -->4、调用API
>>1. 在主 Activiy 的 OnCreate 方法中,调用接口 startWork,其中 loginValue 是百度账户的accessToken 或者是 ApiKey,由 loginType 决定。
PushManager.startWork(context, loginType, loginValue)
功能:完成 Push 服务的初始化, 并且完成自动 bind 工作
参数
context:当前执行 Context
loginType:String 绑定认证方式——无账号认证方式用 PushConstants.LOGIN_TYPE_API_KEY ;百度 Auth2.0 认证方式用
PushConstants.LOGIN_TYPE_ACCESS_TOKEN
loginValue: 和 loginType 对应,分别是应用的 API KEY,或者百度 Auth2.0 Access Token
>>2. 自定义通知样式(可选)
通知样式定制, 改变 Notification 里的铃声、震动、显示与消失行为,并定制通知栏的 layout、图标、标题、内容、状态栏图标等。
>>3.客户端程序需要自己实现一个 BroadcastReceiver 来接收 Push 消息和接口回调
public class PushMessageReceiver extends BroadcastReceiver {public static final String TAG = PushMessageReceiver.class.getSimpleName(); @Overridepublic void onReceive(final Context context, Intent intent) {if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {// 处理push消息String message = intent.getExtras().getString(PushConstants.EXTRA_PUSH_MESSAGE_STRING); Intent responseIntent = null;responseIntent = new Intent(PushDemoActivity.ACTION_MESSAGE);responseIntent.putExtra(PushDemoActivity.EXTRA_MESSAGE, message);responseIntent.setClass(context, PushDemoActivity.class);responseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(responseIntent); } else if (intent.getAction().equals(PushConstants.ACTION_RECEIVE)) {// 处理bind、setTags 等方法口的返回数据 final String method = intent.getStringExtra(PushConstants.EXTRA_METHOD); final int errorCode = intent.getIntExtra(PushConstants.EXTRA_ERROR_CODE,PushConstants.ERROR_SUCCESS); final String content = new String(intent.getByteArrayExtra(PushConstants.EXTRA_CONTENT));Intent responseIntent = null;responseIntent = new Intent(PushDemoActivity.ACTION_RESPONSE);responseIntent.putExtra(PushDemoActivity.RESPONSE_METHOD, method);responseIntent.putExtra(PushDemoActivity.RESPONSE_ERRCODE, errorCode);responseIntent.putExtra(PushDemoActivity.RESPONSE_CONTENT, content);responseIntent.setClass(context, PushDemoActivity.class);responseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(responseIntent);} else if (intent.getAction().equals(PushConstants.ACTION_RECEIVER_NOTIFICATION_CLICK)) {// 处理用户点击通知消息时响应事件Intent aIntent = new Intent();aIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);aIntent.setClass(context, CustomActivity.class);String title = intent.getStringExtra(PushConstants.EXTRA_NOTIFICATION_TITLE);aIntent.putExtra(PushConstants.EXTRA_NOTIFICATION_TITLE, title);String content = intent.getStringExtra(PushConstants.EXTRA_NOTIFICATION_CONTENT);aIntent.putExtra(PushConstants.EXTRA_NOTIFICATION_CONTENT, content);context.startActivity(aIntent);}} }5、常用的类和API
>>1.常用的类:
| Class | 描述 |
| PushManager | PushManager 提供了所有使用 Push 服务的静态方法 |
| CustomPushNotificationBuilder | 提供了改变 Notification 里的铃声、 |
| PushSettings | PushSettings 提供了端上 Push 服务的配置静态方法 |
>>2.API
| method | detail | class.api |
| Push 服务接口 | 提供 Push 服务 | startWork |
| Tag 管理接口 | Tag 的创建与删除 | setTags(Context,List<String>),delTags(...) |
| 通知管理接口 | 自定义通知样式 | CustomPushNotificationBuilder, setNotificationFlags, |
| 推送效果反馈 | 反馈推送通知的效果 | activityStarted, activityStoped |
| 设置接口 | Push 服务设置 | enableDebugMode |
转载于:https://www.cnblogs.com/a284628487/archive/2013/06/12/3132829.html
总结
以上是生活随笔为你收集整理的Android 百度推送使用总结的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: chrome调试、移动端调试
- 下一篇: Android基础之用Eclipse搭建