1,下面的前提是必须申请了友盟且有app key

3,若开发者需要实现对消息的完全自定义处理,则可以继承 UmengBaseIntentService, 实现自己的Service来完全控制达到消息的处理。

1,实现一个类,继承 UmengBaseIntentService, 重写onMessage(Context context, Intent intent) 方法,并请调用super.onMessage(context, intent)。参考 demo 应用中MyPushIntentService。请参考下面代码:

/**

* 友盟推送服务

*/

public class PushIntentService extends UmengBaseIntentService {

private static final String TAG = PushIntentService.class.getName();

// 如果需要打开Activity,请调用Intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);否则无法打开Activity。

@Override

protected void onMessage(Context context, Intent intent) {

// 需要调用父类的函数,否则无法统计到消息送达

super.onMessage(context, intent);

try {

//可以通过MESSAGE_BODY取得消息体

String message = intent.getStringExtra(BaseConstants.MESSAGE_BODY);

//            String str=message.replaceAll("\\\\", "");//将URL中的反斜杠替换为空  加上之后收不到消息

UMessage msg = new UMessage(new JSONObject(message));

Log.d(TAG,"message=" + message);    //消息体

Log.d(TAG, "custom=" + msg.custom);    //自定义消息的内容

Log.d(TAG, "title=" + msg.title);    //通知标题

Log.d(TAG, "text=" + msg.text);    //通知内容

//消息处理

Map extra=msg.extra;

String displayType=extra.get("displayType");//展示情况WAP 和直接activity展示

Intent intentAct = new Intent();

intentAct.setClass(context, MessageDetailActivity.class);

intentAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Bundle bundle = new Bundle();

MessageItem item=new MessageItem();//自定义的消息bean

item.setMsmType("PUSH");

item.setMsmcontent(msg.text);//获取推送的消息内容

item.setTitle(msg.title);//获取推送的消息标题

if (displayType.equals("DISPLAYONAPP")){//手机端展示

item.setDisplayType("DISPLAYONAPP");

} else if (displayType.equals("DISPLAYONWAP")){//打开指定网页

item.setDisplayType("DISPLAYONWAP");

item.setOtherParams(extra.get("otherParams"));//将整个自定义参数传出去,在需要的地方处理

}else{

System.out.println("推送消息类型错误!");

}

bundle.putSerializable("message", item);//传递一个序列化参数

intentAct.putExtras(bundle);

showNotification(context, msg, intentAct);//必须要有,不然收不到推送的消息

// 完全自定义消息的处理方式,点击或者忽略

boolean isClickOrDismissed = true;

if(isClickOrDismissed) {

//完全自定义消息的点击统计

UTrack.getInstance(getApplicationContext()).trackMsgClick(msg);

} else {

//完全自定义消息的忽略统计

UTrack.getInstance(getApplicationContext()).trackMsgDismissed(msg);

}

} catch (Exception e) {

Log.e(TAG, e.getMessage());

}

}

// 通知栏显示当前播放信息,利用通知和 PendingIntent来启动对应的activity

public void showNotification(Context context,UMessage msg,Intent intent) {

NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

builder.setAutoCancel(true);

Notification mNotification = builder.build();

mNotification.icon = R.drawable.ic_launcher;//notification通知栏图标

mNotification.defaults |= Notification.DEFAULT_SOUND;

mNotification.defaults |= Notification.DEFAULT_VIBRATE ;

mNotification.tickerText=msg.ticker;

//自定义布局

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.activity_umeng_push);

contentView.setImageViewResource(R.id.Umeng_view, R.drawable.ic_launcher);

contentView.setTextViewText(R.id.push_title, msg.title);

contentView.setTextViewText(R.id.push_content, msg.text);

mNotification.contentView = contentView;

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,

intent, PendingIntent.FLAG_UPDATE_CURRENT);//不是Intent

//notifcation.flags = Notification.FLAG_NO_CLEAR;// 永久在通知栏里

mNotification.flags = Notification.FLAG_AUTO_CANCEL;

//使用自定义下拉视图时,不需要再调用setLatestEventInfo()方法,但是必须定义contentIntent

mNotification.contentIntent = contentIntent;

mNotificationManager.notify(103, mNotification);

}

}

说明:当自定义的参数中有URL时,会被转义,不要在这里面处理,把整个参数传递出去,在需要的地方进行取缔,不然会收不到推送的消息,我的message如下:

message={

"msg_id":"uu56667143874445555800",

"display_type":"notification",

"alias":"",

"random_min":0,

"body":{

"text":"content",

"title":"title",

"ticker":"ticker",

"play_vibrate":"true",

"play_lights":"true",

"play_sound":"true"

},

"extra":{

"otherParams":"

{\"url\":\"http://www.baidu.com\"}",

"displayType":"DISPLAYONWAP"

}

}

自定义的messageItem如下:

package com.pitaya.daokoudai.model.bean.account;

import org.json.JSONException;

import org.json.JSONObject;

import java.io.Serializable;

/**

* 我的消息  bean  包含類型,時間,類容,狀態,消息未读数

*/

public class MessageItem implements Serializable {

private String msmType;

private Long msmDate;

private String msmcontent;

private boolean msmstatus;

private int unreadmsg;

private Long id;

private String displayType;

private String otherParams;

public String getDisplayType() {

return displayType;

}

public void setDisplayType(String displayType) {

this.displayType = displayType;

}

public String getOtherParams() {

return otherParams;

}

public void setOtherParams(String otherParams) {//将参数中的\全部换成“”

String string=otherParams.replaceAll("\\\\","");//是4杠,不是2杠

try {

JSONObject jsonObject=new JSONObject(string);

this.otherParams = jsonObject.optString("url");

} catch (JSONException e) {

e.printStackTrace();

}

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

private String title;

public int getUnreadmsg() {

return unreadmsg;

}

public void setUnreadmsg(int unreadmsg) {

this.unreadmsg = unreadmsg;

}

public Long getId() {

return id;

}

public void setId(Long id) {

this.id = id;

}

public String getMsmType() {

return msmType;

}

public void setMsmType(String msmType) {

this.msmType = msmType;

}

public Long getMsmDate() {

return msmDate;

}

public void setMsmDate(Long msmDate) {

this.msmDate = msmDate;

}

public String getMsmcontent() {

return msmcontent;

}

public void setMsmcontent(String msmcontent) {

this.msmcontent = msmcontent;

}

public boolean getMsmstatus() {

return msmstatus;

}

public void setMsmstatus(boolean msmstatus) {

this.msmstatus = msmstatus;

}

}

2,在AndroidManifest.xml 中声明。

3,在主Activity中调用。     /**推送开启 **/     PushAgent mPushAgent = PushAgent.getInstance(this);     mPushAgent.enable();//开启推送     mPushAgent.setDebugMode(true);     mPushAgent.setPushIntentServiceClass(PushIntentService.class)

java友盟自定义行为_实现友盟推送消息的完全自定义处理相关推荐

  1. 友盟小米收不到推送消息_友盟推送SDK集成测试、常见问题以及注意事项总结

    最近为了解决公司APP在一些手机出现的推送问题重新集成了最新版的友盟推送SDK,花费了几天时间终于把集成和测试工作完成,最终在华为,Nexus,三星,小米,HTC,魅族等10多部手机上测试并达到了预想 ...

  2. 友盟小米收不到推送消息_一个轻量级、可插拔的Android消息推送框架。一键集成推送(极光推送、友盟推送、华为、小米推送等)...

    XPush 一个轻量级.可插拔的Android消息推送框架.一键集成推送(极光推送.友盟推送.华为.小米推送等),提供有效的保活机制,支持推送的拓展,充分解耦推送和业务逻辑,解放你的双手! 在提iss ...

  3. iOS10后友盟推送报错,在iOS9设备上获取到device_token但接不到推送消息,iOS10上报错code=3000

    iOS10来了,今天特别适配了iOS10,讲一下其中的坑,在iOS10中,报如下错误: Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的 ...

  4. 阿里达摩院联合友盟+ ,推出国内首个智能推送

    你还记得最近因为优秀的推送文案而让你印象深刻的App是哪个么?某游戏的热血邀约?某标题党的新闻资讯?或者是某次约会提醒? 恐怕很多人的答案是否定的.因为有无数的案例证明现在主流的推送方案问题多多,已经 ...

  5. 友盟小米收不到推送消息_Android 手机收不到消息推送的设置指南 - 融云 RongCloud...

    如何解决 Android 手机因推送权限问题收不到消息提醒? 问题描述 部分 Android 手机系统在黑屏待机后自动清理后台运行的软件,这样影响了应用正常接收新的消息,需要开启手机的某些权限.此文档 ...

  6. java消息推送怎么实现_调用钉钉接口实现机器人推送消息

    一.摘要 现实交易中为了能及时了解发明者量化机器人交易状态,有时候我们需要将机器人所执行的交易结果发送到微信.邮箱.短信等等.但每天上百条各种各样的信息,使得对这些信息已经不敏感,导致重要的信息不能及 ...

  7. 微信小程序推送消息java开发_干货 | 微信小程序推送消息简单Demo

    在开始前,你需要准备:注册微信小程序 一个简单的springBoot 项目 微信开发者工具 正式 微信小程序发送消息主要通过WxMaTemplateMessage 类来推送 public class ...

  8. java 通过企业微信推送消息

    首先我们要知道企业微信推送消息的步骤,企业微信官方提供了多个API供我们调用,这里我们只讲我们需要的API: 企业微信的官方开放的API地址:https://work.weixin.qq.com/ap ...

  9. Java 服务端推送消息有那么难吗?

    点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 转自公众号:码农小胖哥 今天项目经理交给我一个开发任务.如果有人在前台下了订单就给后台仓库管 ...

最新文章

  1. MyBatis的运行的核心原理解析(三)
  2. Facebook欧盟垄断案陷入灰色地带 立法或调整
  3. 微信公众平台如何启用开发模式
  4. sublime4 安装pretty json 并绑定快捷键
  5. Redis数据库(一)——介绍、配置与优化
  6. 叮咚酒店营销版小程序v8.5.8+前端
  7. led灯光衰怎么解决_花小钱办大事 主流直插式LED大灯横评
  8. 结构体可以整体交换吗_结构胶能够耐高温吗?可以在高温环境中工作吗?好用吗?...
  9. 对文式编程的一些误解
  10. json类型大小 mysql_MySQL数据类型 - JSON数据类型 (1)
  11. C++小游戏 双人贪吃蛇
  12. HTC Vive Unity 教程
  13. html链接外部样式表、链接网站图标
  14. 页眉怎么添加【节】,设置不同章节不同页眉
  15. 键盘输入一个整数1~7代表今天周几,再输入间隔天数n,判断n天后是周几
  16. 如何利用MSDN在线查询MFC里面的API
  17. bravado哺乳内衣 这款哺乳胸罩,越早买越好,别等到下垂涨奶才知道后悔!
  18. count的几种写法
  19. java 设置打印机颜色_java 操作颜色选择器和打印机实现打印功能【代码片段】...
  20. 新松机器人产业小镇_总投资18.7亿元的新松浑南智慧产业园,是我国最大的机器人产业化基地。该基地内,...

热门文章

  1. 无效数据是什么,我们该怎么处理?
  2. 如何规划和管理自己的职业生涯?
  3. spring cloud 快速上手系列 -> 04-网关 Gateway -> 041-空的工程
  4. VC++ MFC进度条
  5. 哈佛大学搞出声波传数据芯片,抗干扰能力更强,适用于量子计算等新兴领域...
  6. activiti之UserTask用户任务的到期日期(dueDate)属性作用
  7. python3内置集成开发工具_python应用(3):启用集成开发工具pycharm
  8. 中国导电油墨市场需求预测分析及投资战略研究报告2022-2028年
  9. 如何操作 WebOffice.ocx
  10. #Java学习之路——基础阶段二(第十一篇)