1:umeng push 在通知管理中心关掉后,消息处理
umeng push message 在消息通知管理中心关掉某个应用的消息接收后,仍然能收到消息,只是关掉了显示这一层。消息任然能后接收到后做自己的处理。
2:消息类型
notification 和message,他们的区别?
一直没有搞懂他们的区别是什么?
3:代码实现帮助类
public class UmPushHelper { public static UmPushHelper instance; private PushAgent mPushAgent; public IUmengRegisterCallback mRegisterCallback; public IUmengUnregisterCallback mUnregisterCallback; public final String CALLBACK_RECEIVER_ACTION = "callback_receiver_action"; private UmPushHelper(){}; public static UmPushHelper getInstance(){ if (instance == null){ instance = new UmPushHelper(); } return instance; } public void init(final Context context){ mPushAgent = PushAgent.getInstance(context); // TODO: 16/5/26 not called 本应在这里实现alias设置,奈何此回调根本不执行。若有知道的朋友请指教。可能是我的jar版本过低(历史原因,2.4.1 version) //mPushAgent.enable(new IUmengRegisterCallback() { // @Override // public void onRegistered(final String s) { // Log.i("push addAlias","push enbale()"+s); // new Thread(){ // @Override // public void run() { // super.run(); // try{ // Log.i("pushmsg","addAlias before "+MyPreferences.getUser(context)); // boolean flag = mPushAgent.addAlias("1234", "user_key"); // Log.i("pushmsg",flag+"addAlias after"); // // //设置用户id和device_token的一一映射关系,确保同一个alias只对应一台设备: // // TODO: 16/5/26 not found // //mPushAgent.setExclusiveAlias("zhangsan@sina.com", ALIAS_TYPE.SINA_WEIBO); // // //若是要移除用户id,可调用以下接口: // //mPushAgent.removeAlias(MyPreferences.getUser(context).getNike_name(),"user_key"); // }catch (Exception e){ // e.printStackTrace(); // Log.i("push addAlias",e.getMessage()); // } // } // }.start(); // } //}); addAlias(context); //mPushAgent.setDebugMode(true); /** * 该Handler是在IntentService中被调用,故 1. * 如果需启动Activity,需添加Intent.FLAG_ACTIVITY_NEW_TASK 2. * IntentService里的onHandleIntent方法是并不处于主线程中,因此,如果需调用到主线程,需如下所示; * 或者可以直接启动Service * */ UmengMessageHandler messageHandler = new UmengMessageHandler() { @Override public void dealWithCustomMessage(final Context context, final UMessage msg) {
//notification 的自处理custom 类型的notification,不会弹出 notification. new Handler().post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub UTrack.getInstance(context.getApplicationContext()).trackMsgClick(msg); com.alibaba.fastjson.JSONObject msgJsonObj = JSON.parseObject(msg.custom); int messageType = msgJsonObj.getIntValue("type"); String title = msgJsonObj.getString("title"); if (messageType == 5){ // todo you business }else { Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show(); } } }); } @Override public Notification getNotification(Context context, UMessage msg) { //notification,此种情况不可避免的会弹出notification. switch (msg.builder_id) { case 1: NotificationCompat.Builder builder = new NotificationCompat.Builder(context); RemoteViews myNotificationView = new RemoteViews(context.getPackageName(), R.layout.notification_view); myNotificationView.setTextViewText(R.id.notification_title, msg.title); myNotificationView.setTextViewText(R.id.notification_text, msg.text); myNotificationView.setImageViewBitmap(R.id.notification_large_icon, getLargeIcon(context, msg)); // myNotificationView.setImageViewResource(R.id.notification_small_icon, // getSmallIconId(context, msg)); builder.setContent(myNotificationView); builder.setAutoCancel(true); Notification mNotification = builder.build(); // 由于Android // v4包的bug,在2.3及以下系统,Builder创建出来的Notification,并没有设置RemoteView,故需要添加此代码 mNotification.contentView = myNotificationView; return mNotification; default: // 默认为0,若填写的builder_id并不存在,也使用默认。 return super.getNotification(context, msg); } } }; mPushAgent.setMessageHandler(messageHandler); /** * umeng * 该Handler是在BroadcastReceiver中被调用,故 * 如果需启动Activity,需添加Intent.FLAG_ACTIVITY_NEW_TASK * */ UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() { @Override public void dealWithCustomAction(Context context, UMessage msg) { // broadcast msg ,此种应该为自定比较深并且比较灵活的消息类型处理方法。 JSONObject obj = new JSONObject(msg.extra); String message = obj.optString("message"); String messageWhere = obj.optString("message_where"); int messageType = obj.optInt("type"); // next todo what you want } }; mPushAgent.setNotificationClickHandler(notificationClickHandler); mRegisterCallback = new IUmengRegisterCallback() { @Override public void onRegistered(String registrationId) { // TODO Auto-generated method stub Intent intent = new Intent(CALLBACK_RECEIVER_ACTION); context.sendBroadcast(intent); } }; mPushAgent.setRegisterCallback(mRegisterCallback); mUnregisterCallback = new IUmengUnregisterCallback() { @Override public void onUnregistered(String registrationId) { // TODO Auto-generated method stub Intent intent = new Intent(CALLBACK_RECEIVER_ACTION); context.sendBroadcast(intent); } }; mPushAgent.setUnregisterCallback(mUnregisterCallback); } public void addAlias(final Context context){ new Thread(){ @Override public void run() { super.run(); super.run(); try { if (MyPreferences.getUser(context) != null && !TextUtils.isEmpty(MyPreferences.getUser(context) .getUserid())){if (MyPreferences.getUser(context) != null && !TextUtils.isEmpty(MyPreferences.getUser(context) .getUserid())){ boolean flag = mPushAgent.addAlias(MyPreferences.getUser(context) .getUserid(), "user_key"); Log.i("pushmsg", "---addAlias state "+flag); } //设置用户id和device_token的一一映射关系,确保同一个alias只对应一台设备: // TODO: 16/5/26 not found in my jar version //mPushAgent.setExclusiveAlias("zhangsan@sina.com", ALIAS_TYPE.SINA_WEIBO); //若是要移除用户id,可调用以下接口: //mPushAgent.removeAlias(MyPreferences.getUser(context).getNike_name(),"user_key");} //设置用户id和device_token的一一映射关系,确保同一个alias只对应一台设备: //mPushAgent.setExclusiveAlias("zhangsan@sina.com", ALIAS_TYPE.SINA_WEIBO); //若是要移除用户id,可调用以下接口: //mPushAgent.removeAlias(MyPreferences.getUser(context).getNike_name(),"user_key"); } catch (Exception e) { e.printStackTrace(); Log.i("push addAlias", e.getMessage()); } } }.start(); } /** * must called when user logout * * @param context */ public void removeAlias(final Context context){ new Thread(){ @Override public void run() { super.run(); super.run(); try { Log.i("pushmsg", "---addAlias before " + MyPreferences.getUser(context)); if (MyPreferences.getUser(context) != null && !TextUtils.isEmpty(MyPreferences.getUser(context) .getUserid())){ boolean flag = mPushAgent.removeAlias(MyPreferences.getUser(context) .getUserid(), "user_key"); Log.i("pushmsg", "---addAlias state "+flag); } //设置用户id和device_token的一一映射关系,确保同一个alias只对应一台设备: // TODO: 16/5/26 not found //mPushAgent.setExclusiveAlias("zhangsan@sina.com", ALIAS_TYPE.SINA_WEIBO); //若是要移除用户id,可调用以下接口: //mPushAgent.removeAlias(MyPreferences.getUser(context).getNike_name(),"user_key"); } catch (Exception e) { e.printStackTrace(); Log.i("push addAlias", e.getMessage()); } } }.start(); }}