我們先看下手機端如何做。
1.註冊百度賬戶
2.加入 百度開發者
3.創建應用
4.下載sdk
5.導入sdk包,開發應用
5.1 在AndroidManifest.xml 中註冊響應的receiver
5.2 在主窗體的oncreate中寫
PushManager.startWork(getApplicationContext(),
PushConstants.LOGIN_TYPE_API_KEY,
PushServiceUtils.getMetaValue(this, "api_key"));
復制代碼
5.3 編寫自己的receiver。處理 綁定的相關消息,推送的消息,通知欄點擊後的消息
public class MyPushMessageReceiver extends BroadcastReceiver {
private static final String TAG = "BroadcastReceiver";
@Override
public void onReceive(final Context context, Intent intent) {
Log.d(TAG, ">>> Receive intent: \r\n" + intent);
if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
// 獲取消息內容
String message = intent.getExtras().getString(
PushConstants.EXTRA_PUSH_MESSAGE_STRING);
// 消息的用戶自定義內容讀取方式
Log.i(TAG, "onMessage: " + message);
// 自定義內容的json串
Log.d(TAG,
"EXTRA_EXTRA = "
+ intent.getStringExtra(PushConstants.EXTRA_EXTRA));
// 用戶在此自定義處理消息,以下代碼為demo界面展示用
Intent responseIntent = null;
responseIntent = new Intent(PushServiceUtils.ACTION_MESSAGE);
responseIntent.putExtra(PushServiceUtils.EXTRA_MESSAGE, message);
responseIntent.setClass(context, MainActivity.class);
responseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(responseIntent);
} 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調用,這有可能導致死循環。
// 可以通過限制重試次數,或者在其他時機重新調用來解決。
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));
}
// 用戶在此自定義處理消息,以下代碼為demo界面展示用
Log.d(TAG, "onMessage: method : " + method);
Log.d(TAG, "onMessage: result : " + errorCode);
Log.d(TAG, "onMessage: content : " + content);
Toast.makeText(
context,
"method : " + method + "\n result: " + errorCode
+ "\n content = " + content, Toast.LENGTH_SHORT)
.show();
Intent responseIntent = null;
responseIntent = new Intent(PushServiceUtils.ACTION_RESPONSE);
responseIntent.putExtra(PushServiceUtils.RESPONSE_METHOD, method);
responseIntent.putExtra(PushServiceUtils.RESPONSE_ERRCODE,
errorCode);
responseIntent.putExtra(PushServiceUtils.RESPONSE_CONTENT, content);
responseIntent.setClass(context, MainActivity.class);
responseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(responseIntent);
// 可選。通知用戶點擊事件處理
} else if (intent.getAction().equals(
PushConstants.ACTION_RECEIVER_NOTIFICATION_CLICK)) {
Log.d(TAG, "intent=" + intent.toUri(0));
// 自定義內容的json串
String customData = intent
.getStringExtra(PushConstants.EXTRA_EXTRA);
Log.d(TAG,
"EXTRA_EXTRA = "
+ intent.getStringExtra(PushConstants.EXTRA_EXTRA));
if (customData == null || "".equals(customData)) {
return;
}
Intent aIntent = new Intent();
aIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
aIntent.setClass(
context,
com.pdwy.wulianwang.mobile.main.notification.NotificationDetails_Activity.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);
String detailContent = "";
try {
org.json.JSONObject json = new JSONObject(customData);
detailContent = json.getString("detailContent");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 保存在數據庫
NotifyDao dao = new NotifyDao();
int notifyId = dao.saveNotify(title, content, detailContent);
// 向消息詳細頁發送內容
aIntent.putExtra("notify_id", notifyId);
context.startActivity(aIntent);
}
}
}
要是還不能解決
妳來我們群裏說吧 這裏是開發者互相學習交流的
有大神 讓他們給妳解釋妳的疑問 q un號 188168040