簡單的Push一般都不能滿足需求,那就要在服務端做Push邏輯上的處理,客戶端也要做相應的一些操作,這篇文章只針對客戶端接收 TYPE_NOTIFICATION 類型通知做解釋...如果服務端發來的是 TYPE_MESSAGE 類型的通知,那基本就可以滿足所有需求,完全自定義實現了
1、寫自定義廣播,繼承 XGPushBaseReceiver
實現父類的方法,其中 onNotifactionShowedResult() 方法就是接收服務端發來的 TYPE_NOTIFICATION 類型的通知,如果是 TYPE_MESSAGE 類型,就在 onTextMeeage() 方法中接收數據。別忘了在清單註冊這個receiver
<receiver
android:name="com.himissing.poppy.MyNotificationsReceiver" >
<intent-filter>
<!-- XGPush receive msg -->
<action android:name="com.tencent.android.tpush.action.PUSH_MESSAGE" />
<!-- XGPush register/unregister etc. -->
<action android:name="com.tencent.android.tpush.action.FEEDBACK" />
</intent-filter>
</receiver>
在 onNotificationShowedResult(Context context, XGPushShowedResult) 方法中
@Override
public void onNotifactionShowedResult(Context context, XGPushShowedResult myNtf) {
// TODO Auto-generated method stub
String customContent = myNtf.getCustomContent();
MLog.v("[NTF-customContent] = " + customContent);
if(customContent != null && customContent.length() != 0){
MyNotificationManager mnm = MyNotificationManager.getInstance(context);
mnm.saveNotification(customContent);
}
}
得到的customContent 就是從服務端根據通知協議傳過來的 key-value ,Json 字符串
之後自己寫Manager對接收到的數據根據自己的需求進行處理就ok
2、自定義通知顯示樣式
在服務端發送通知的時候,可以指定一個style的id,取值範圍 1-4096,Xinge就會根據id在客戶端本地獲取對應的樣式對通知顯示
先在本地寫一個獲得自定義樣式的方法
/**
* 自定義通知樣式
* @author Jfomt
* @since 2014-04-15, v4.2.0
* @return XGBasicPushNotificationBuilder
*/
public XGBasicPushNotificationBuilder getNtfBuilder(){
XGBasicPushNotificationBuilder ntfBuild = new XGBasicPushNotificationBuilder();
ntfBuild.setIcon(R.drawable.icon_notification);
ntfBuild.setFlags(Notification.FLAG_AUTO_CANCEL);
//ntfBuild.setDefaults(Notification.DEFAULT_ALL);
ntfBuild.setVibrate(new long[]{100, 500});
ntfBuild.setSound(RingtoneManager.
getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION));
return ntfBuild;
然後在service啓動註冊Xinge的時候 set 這個樣式id
XGPushManager.setPushNotificationBuilder(getApplicationContext(),1, ntfManager.getNtfBuilder());
這樣就ok了,別忘了在服務端指定style的id爲1
3、自定義通知點擊事件處理
廣播裏有 onNotifactionClickedResult 這麼個方法,看樣子貌似就是幹這事的,但是不曉得啥原因就是不起作用
所有隻能從後臺指定所有的通知都點擊跳轉到程序主入口界面,然後在onstart() 方法中做處理,
XGPushClickedResult result = XGPushManager.onActivityStarted(this);
if (result != null) { //如果result爲null就說明通知不是來自Xinge
String customContent = result.getCustomContent();
String more = "";
// 解析自定義key-value
if (customContent != null && customContent.length() != 0) {
//customContent 是服務端傳過來的key-value值,在這裏根據需求解析處理
}
}
在onstop()方法中加上這句
XGPushManager.onActivityStoped(this);
ok,Xinge Push中 TYPE_NOTIFICATION 類型的通知的客戶端處理基本就這麼多了