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调试、移动端调试
- 下一篇: 10篇写给Git初学者的最佳教程