Android 消息推送 -- Xinge Push[客户端数据接收处理]

来源:互联网 发布:js点击显示div 编辑:程序博客网 时间:2024/06/13 00:35


简单的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) 方法中

@Overridepublic void onNotifactionShowedResult(Context context, XGPushShowedResult myNtf) {// TODO Auto-generated method stubString 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就说明通知不是来自XingeString customContent = result.getCustomContent();String more = "";// 解析自定义key-valueif (customContent != null && customContent.length() != 0) {//customContent 是服务端传过来的key-value值,在这里根据需求解析处理}}

在onstop()方法中加上这句

XGPushManager.onActivityStoped(this);

ok,Xinge Push中 TYPE_NOTIFICATION 类型的通知的客户端处理基本就这么多了



0 0
原创粉丝点击