公司要求做这个推送(自定义通知),因为官方文档写的太简洁了,自己参照各种文档,才从坑了爬出来…
public class XGMessageReceiver extends XGPushBaseReceiver {
private Intent intent = new Intent("tdotapp.com.xingedemo.activity.UPDATE_LISTVIEW");
public static final String LogTag = "TPushReceiver";
private void show(Context context, String text) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
@Override
public void onRegisterResult(Context context, int i, XGPushRegisterResult xgPushRegisterResult) {
}
@Override
public void onUnregisterResult(Context context, int i) {
if (context == null) {
return;
}
String text = "";
if (i == XGPushBaseReceiver.SUCCESS) {
text = "反注册成功";
} else {
text = "反注册失败" + i;
}
Log.d(LogTag, text);
show(context, text);
}
@Override
public void onSetTagResult(Context context, int i, String s) {
if (context == null) {
return;
}
String text = "";
if (i == XGPushBaseReceiver.SUCCESS) {
text = "\"" + s + "\"设置成功";
} else {
text = "\"" + s + "\"设置失败,错误码:" + i;
}
Log.d(LogTag, text);
show(context, text);
}
@Override
public void onDeleteTagResult(Context context, int i, String s) {
}
//消息透传
@Override
public void onTextMessage(Context context, XGPushTextMessage message) {
// TODO Auto-generated method stub
show(context, "haha");
String text = "收到消息:" + message.toString();
// 获取自定义key-value
String customContent = message.getCustomContent();
if (customContent != null && customContent.length() != 0) {
try {
JSONObject obj = new JSONObject(customContent);
// key1为前台配置的key
if (!obj.isNull("key")) {
String value = obj.getString("key");
Log.d(LogTag, "get custom value:" + value);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// APP自主处理消息的过程...
XGLocalMessage localMessage = new XGLocalMessage();
localMessage.setTitle("haha");
localMessage.setContent(message.getContent());
XGCustomPushNotificationBuilder build = new XGCustomPushNotificationBuilder();
build.setSound(
RingtoneManager.getActualDefaultRingtoneUri(
context, RingtoneManager.TYPE_ALARM)) // 设置声音
// setSound(
// Uri.parse("android.resource://" + getPackageName()
// + "/" + R.raw.wind)) 设定Raw下指定声音文件
.setDefaults(Notification.DEFAULT_VIBRATE) // 振动
.setFlags(Notification.FLAG_NO_CLEAR); // 是否可清除
// 设置自定义通知layout,通知背景等可以在layout里设置
build.setLayoutId(R.layout.layout_notification);
// 设置自定义通知标题id
build.setLayoutTitleId(R.id.title);
// 设置自定义通知图片id
build.setLayoutIconId(R.id.icon);
// 设置自定义通知图片资源
build.setLayoutIconDrawableId(R.drawable.ic_launcher);
// 设置状态栏的通知小图标
build.setIcon(R.drawable.ic_launcher);
// 设置时间id
build.setLayoutTimeId(R.id.time);
// 若不设定以上自定义layout,又想简单指定通知栏图片资源
build.setNotificationLargeIcon(R.drawable.tenda_icon);
// 客户端保存build_id
XGPushManager.setDefaultNotificationBuilder(context, build);
XGPushManager.addLocalNotification(context, localMessage);
Log.d(LogTag, text);
show(context, text);
}
private void sendIconCountMessage(Context context) {
Intent it = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
it.putExtra("android.intent.extra.update_application_component_name", "com.example.wujie.xungetest/.MainActivity");
String iconCount = "50";
it.putExtra("android.intent.extra.update_application_message_text", iconCount);
context.sendBroadcast(it);
}
//通知点击回调
@Override
public void onNotifactionClickedResult(Context context, XGPushClickedResult message) {
if (context == null || message == null) {
return;
}
String text = "";
sendIconCountMessage(context);
// samsungShortCut(context, "25");
if (message.getActionType() == XGPushClickedResult.NOTIFACTION_CLICKED_TYPE) {
// 通知在通知栏被点击啦。。。。。
// APP自己处理点击的相关动作
// 这个动作可以在activity的onResume也能监听,请看第3点相关内容
text = "通知被打开 :" + message;
} else if (message.getActionType() == XGPushClickedResult.NOTIFACTION_DELETED_TYPE) {
// 通知被清除啦
// APP自己处理通知被清除后的相关动作
text = "通知被清除 :" + message;
}
Toast.makeText(context, "广播接收到通知被点击:" + message.toString(),
Toast.LENGTH_SHORT).show();
// 获取自定义key-value
String customContent = message.getCustomContent();
if (customContent != null && customContent.length() != 0) {
try {
JSONObject obj = new JSONObject(customContent);
// key1为前台配置的key
if (!obj.isNull("ID")) {
String value = obj.getString("ID");
Log.d(LogTag, "get custom value:" + value);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// APP自主处理的过程
show(context, text);
}
//通知展示栏
@Override
public void onNotifactionShowedResult(Context context, XGPushShowedResult xgPushShowedResult) {
if (context == null || xgPushShowedResult == null) {
return;
}
show(context, "您有1条新消息, " + "通知被展示 , " + xgPushShowedResult.toString());
}
}
**private Intent intent = new Intent(“tdotapp.com.xingedemo.activity.UPDATE_LISTVIEW”);
tdotapp.com.xingedemo要替换自己的包名,不然点击推送直接黑屏
自定义推送还有其他的方法,个人认为这样比较通俗易懂