實現友盟推送訊息的完全自定義處理
1,下面的前提是必須申請了友盟且有app key
3,若開發者需要實現對訊息的完全自定義處理,則可以繼承 UmengBaseIntentService
, 實現自己的Service來完全控制達到訊息的處理。
/**
* 友盟推送服務
*/
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<String ,String> 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 中宣告。
<!-- 請填寫實際的類名,下面僅是示例程式碼-->
<service android:name="com.umeng.message.example.MyPushIntentService" android:process=":push"/>
3,在主Activity中呼叫。
/**推送開啟 **/
PushAgent mPushAgent = PushAgent.getInstance(this);
mPushAgent.enable();//開啟推送
mPushAgent.setDebugMode(true);
mPushAgent.setPushIntentServiceClass(PushIntentService.class)