前面发过一篇百度推送的文章,属于刚刚摸索入门级的(传送门:http://blog.csdn.net/zml_2015/article/details/50733659),本篇在入门级的基础上探索推送的高级部分
本次介绍分三部分,首先看第一部分:通知
上次我们实现了推送的基本通知功能,百度推送功能当然不止这么简单,项目中,客户需要点击推送消息跳转到指定页面。无非就是做一个Activity的跳转,并不难,但是要向那个页面传一个值,这就要我们回过头来看通知推送的功能了。怎么实现传值,其实看懂了也很简单,不废话,上代码:
packagecom.baidu.push;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.List;
importorg.json.JSONException;
importorg.json.JSONObject;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.text.TextUtils;
importandroid.util.Log;
importcom.baidu.frontia.api.FrontiaPushMessageReceiver;
importcom.lidroid.xutils.util.LogUtils;
importcom.robinframe.xutils.utils.StringUtils;
importcom.zmit.teddy.AppManager;
importcom.zmit.teddy.ContactsActivity;
importcom.zmit.teddy.DetaiDataActivity;
importcom.zmit.teddy.DetaiLlistActivty;
importcom.zmit.teddy.MainActivity;
importcom.zmit.teddy.PromotionActivity;
importcom.zmit.teddy.SplashScreensActivity;
/**
* Push消息处理receiver。请编写您需要的回调函数, 一般来说: onBind是必须的,用来处理startWork返回值;
* onMessage用来接收透传消息; onSetTags、onDelTags、onListTags是tag相关操作的回调;
* onNotificationClicked在通知被点击时回调; onUnbind是stopWork接口的返回值回调
*
* 返回值中的errorCode,解释如下:
* 0 - Success
* 10001 - Network Problem
* 30600 - Internal Server Error
* 30601 - Method Not Allowed
* 30602 - Request Params Not Valid
* 30603 - Authentication Failed
* 30604 - Quota Use Up Payment Required
* 30605 - Data Required Not Found
* 30606 - Request Time Expires Timeout
* 30607 - Channel Token Timeout
* 30608 - Bind Relation Not Found
* 30609 - Bind Number Too Many
*
* 当您遇到以上返回错误时,如果解释不了您的问题,请用同一请求的返回值requestId和errorCode联系我们追查问题。
*
*/
publicclass MyPushMessageReceiver extendsFrontiaPushMessageReceiver {
/** TAG to Log */
publicstatic final String TAG = MyPushMessageReceiver.class
.getSimpleName();
/**
* 调用PushManager.startWork后,sdk将对push
* server发起绑定请求,这个过程是异步的。绑定请求的结果通过onBind返回。 如果您需要用单播推送,需要把这里获取的channel
* id和user id上传到应用server中,再调用server接口用channel id和user id给单个手机或者用户推送。
*
* @param context
* BroadcastReceiver的执行Context
* @param errorCode
* 绑定接口返回值,0 - 成功
* @param appid
* 应用id。errorCode非0时为null
* @param userId
* 应用user id。errorCode非0时为null
* @param channelId
* 应用channel id。errorCode非0时为null
* @param requestId
* 向服务端发起的请求id。在追查问题时有用;
* @return none
*/
@Override
publicvoid onBind(Context context, interrorCode, String appid,
String userId, String channelId, String requestId) {
String responseString = "onBind errorCode="+ errorCode + " appid="
+ appid + " userId="+ userId + " channelId="+ channelId
+ " requestId="+ requestId;
Log.d(TAG, responseString);
// 绑定成功,设置已绑定flag,可以有效的减少不必要的绑定请求
if(errorCode == 0) {
Utils.setBind(context, true);
}
// Demo更新界面展示代码,应用请在这里加入自己的处理逻辑
updateContent(context, responseString);
}
/**
* 接收透传消息的函数。
*
* @param context
* 上下文
* @param message
* 推送的消息
* @param customContentString
* 自定义内容,为空或者json字符串
*/
@Override
publicvoid onMessage(Context context, String message,
String customContentString) {
String messageString = "透传消息 message=\""+ message
+ "\" customContentString="+ customContentString;
Log.d(TAG, messageString);
// 自定义内容获取方式,mykey和myvalue对应透传消息推送时自定义内容中设置的键和值
if(!TextUtils.isEmpty(customContentString)) {
JSONObject customJson = null;
try{
customJson = newJSONObject(customContentString);
String myvalue = null;
if(customJson.isNull("mykey")) {
myvalue = customJson.getString("mykey");
}
} catch(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Demo更新界面展示代码,应用请在这里加入自己的处理逻辑
updateContent(context, messageString);
}
/**
* 接收通知点击的函数。注:推送通知被用户点击前,应用无法通过接口获取通知的内容。
*
* @param context
* 上下文
* @param title
* 推送的通知的标题
* @param description
* 推送的通知的描述
* @param customContentString
* 自定义内容,为空或者json字符串
*/
@Override
publicvoid onNotificationClicked(Context context, String title,
String description, String customContentString) {
String notifyString = "通知点击 title=\""+ title + "\" description=\""
+ description + "\" customContent="+ customContentString;
Log.d(TAG, notifyString);
// 自定义内容获取方式,mykey和myvalue对应通知推送时自定义内容中设置的键和值
String myvalue = null;
if(!TextUtils.isEmpty(customContentString)) {
JSONObject customJson = null;
try{
customJson = newJSONObject(customContentString);
if(!customJson.isNull("mykey")) {
myvalue = customJson.getString("mykey");
}
} catch(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Demo更新界面展示代码,应用请在这里加入自己的处理逻辑
updateContent(context, myvalue);
}
/**
* setTags() 的回调函数。
*
* @param context
* 上下文
* @param errorCode
* 错误码。0表示某些tag已经设置成功;非0表示所有tag的设置均失败。
* @param successTags
* 设置成功的tag
* @param failTags
* 设置失败的tag
* @param requestId
* 分配给对云推送的请求的id
*/
@Override
publicvoid onSetTags(Context context, interrorCode,
List<String> sucessTags, List<String> failTags, String requestId) {
String responseString = "onSetTags errorCode="+ errorCode
+ " sucessTags="+ sucessTags + " failTags="+ failTags
+ " requestId="+ requestId;
Log.d(TAG, responseString);
// Demo更新界面展示代码,应用请在这里加入自己的处理逻辑
updateContent(context, responseString);
}
/**
* delTags() 的回调函数。
*
* @param context
* 上下文
* @param errorCode
* 错误码。0表示某些tag已经删除成功;非0表示所有tag均删除失败。
* @param successTags
* 成功删除的tag
* @param failTags
* 删除失败的tag
* @param requestId
* 分配给对云推送的请求的id
*/
@Override
publicvoid onDelTags(Context context, interrorCode,
List<String> sucessTags, List<String> failTags, String requestId) {
String responseString = "onDelTags errorCode="+ errorCode
+ " sucessTags="+ sucessTags + " failTags="+ failTags
+ " requestId="+ requestId;
Log.d(TAG, responseString);
// Demo更新界面展示代码,应用请在这里加入自己的处理逻辑
updateContent(context, responseString);
}
/**
* listTags() 的回调函数。
*
* @param context
* 上下文
* @param errorCode
* 错误码。0表示列举tag成功;非0表示失败。
* @param tags
* 当前应用设置的所有tag。
* @param requestId
* 分配给对云推送的请求的id
*/
@Override
publicvoid onListTags(Context context, interrorCode, List<String> tags,
String requestId) {
String responseString = "onListTags errorCode="+ errorCode + " tags="
+ tags;
Log.d(TAG, responseString);
// Demo更新界面展示代码,应用请在这里加入自己的处理逻辑
updateContent(context, responseString);
}
/**
* PushManager.stopWork() 的回调函数。
*
* @param context
* 上下文
* @param errorCode
* 错误码。0表示从云推送解绑定成功;非0表示失败。
* @param requestId
* 分配给对云推送的请求的id
*/
@Override
publicvoid onUnbind(Context context, interrorCode, String requestId) {
String responseString = "onUnbind errorCode="+ errorCode
+ " requestId = "+ requestId;
Log.d(TAG, responseString);
// 解绑定成功,设置未绑定flag,
if(errorCode == 0) {
Utils.setBind(context, false);
}
// Demo更新界面展示代码,应用请在这里加入自己的处理逻辑
updateContent(context, responseString);
}
/**
* 自定义函数,处理业务逻辑
* @param context
* @param content
*/
privatevoid updateContent(Context context, String content) {
System.out.println("推送返回值"+"updateContent"+content);
String logText = ""+ Utils.logStringCache;
// if (!logText.equals("")) {
// logText += "\n";
// }
//
// SimpleDateFormat sDateFormat = new SimpleDateFormat("HH-mm-ss");
// logText += sDateFormat.format(new Date()) + ": ";
// logText += content;
//
// Utils.logStringCache = logText;
// Intent intent = new Intent();
// intent.setClass(context.getApplicationContext(), ContactsActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.getApplicationContext().startActivity(intent);
if(!StringUtils.isEmpty(content)&&!content.equals("promotion")) {
Intent intent=newIntent();
intent.putExtra("CompanyId", content);
// intent.putExtra("cityid", content);
intent.setClass(context.getApplicationContext(), DetaiDataActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
context.getApplicationContext().startActivity(intent);
}elseif (content.equals("promotion")) {
Intent intent=newIntent();
intent.setClass(context.getApplicationContext(), PromotionActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
context.getApplicationContext().startActivity(intent);
}
}
}
上面的类是不是很熟悉,没错,就是推送Demo里的MyPushMessageReceiver类,我们的逻辑处理也都是在这个类里边。其他方法我们先不管,后面会详细介绍,本篇,我们只要注意一个方法onNotificationClicked( ),这个方法其实就是我们接收到通知后回调的方法,看参数 onNotificationClicked(Context context, String title, String description, String customContentString)。
- context上下文,这个不多说了
- title就是通知里显示的大标题
- description就是通知里显示的内容
- customContentString就是我们要接收的值了,这个东西怎么来的呢,看下图
可以看到图中红色框出部分,点开高级设置,就是我们发送的键值对,它会把键值对以json的格式传递到手机上,当用户点击通知的时候就会取到这个json,我们要做的就是解析了,剩下的就简单了,单纯的json解析,然后跳转逻辑就可以实现我们的需求了
@Override
publicvoid onNotificationClicked(Context context, String title,
String description, String customContentString) {
String notifyString = "通知点击 title=\""+ title + "\" description=\""
+ description + "\" customContent="+ customContentString;
Log.d(TAG, notifyString);
// 自定义内容获取方式,mykey和myvalue对应通知推送时自定义内容中设置的键和值
String myvalue = null;
if(!TextUtils.isEmpty(customContentString)) {
JSONObject customJson = null;
try{
customJson = newJSONObject(customContentString);
if(!customJson.isNull("mykey")) {
myvalue = customJson.getString("mykey");
}
} catch(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Demo更新界面展示代码,应用请在这里加入自己的处理逻辑
updateContent(context, myvalue);
}
百度推送的通知高级部分我们探索了一半了,剩下的就是一些自定义的行为了,
待探索。下章讲
述三大类中的消息部分。
本文永久地址:http://blog.it985.com/3173.html
本文出自 IT985博客 ,转载时请注明出处及相应链接。