首页 > 代码库 > 信鸽推送的使用
信鸽推送的使用
最近在看推送方面的知识,用的是信鸽推送主要是因为后台用的是信鸽
推送用第三方推送,也就是在客户端建一个广播接收器,当服务器发送消息时发送到信鸽,信鸽再发送一次,广播接受器接受下;
我实现的功能比较简单,当app在运行状态时,会在主页展示一个弹窗展示推送的消息;如果app不在运行状态且service没被销毁就展示默认的通知
那么如何在主页展示弹窗:当广播接受器收到我要的消息时,用观察者模式,收到消息在发送个消息个主界面
官方的Demo连接:http://xg.qq.com/xg/help/ctr_help/download
修改后Demo的地址:http://download.csdn.net/detail/qq_29774291/9635735
1.先按照官网的例子添加好权限及服务有些服务要改成自己的包名;
2.把官网的广播接受器先复制到自己的项目中,再在清单文件中添加;
3.随后添加从官网给出的3个AccessKey了
<meta-data android:name="com.tencent.rdm.uuid" android:value="http://www.mamicode.com/eb5fa555d70c3246a4944f55be8c266b" /> <!-- 【必须】 请将YOUR_ACCESS_ID修改为APP的AccessId,“21”开头的10位数字,中间没空格 --> <!-- 【必须】 请修改为APP的AccessId,“21”开头的10位数字,中间没空格 --> <meta-data android:name="XG_V2_ACCESS_ID" android:value="2100219302" /> <!-- 【必须】 请修改为APP的AccessKey,“A”开头的12位字符串,中间没空格 --> <meta-data android:name="XG_V2_ACCESS_KEY" android:value="A15KJ71W9ELC" />
4.现在在主界面中注册信鸽推送主要是获取一个token,然后可以上传给服务端,现在服务器就可以给你发送消息了;当然从信鸽官方后台可以发送消息,但是从信鸽后台发送的消息有时会接受不到,这点做的完全不及极光推送好
XGPushConfig.enableDebug(this, true);这句发布的时候记得改为false或删除
1 //信鸽start 2 private String token; 3 private Message message = null; 4 private void XGInit() { 5 // TODO Auto-generated method stub 6 XGPushConfig.enableDebug(this, true); 7 // 如果需要知道注册是否成功,请使用registerPush(getApplicationContext(), 8 // XGIOperateCallback)带callback版本 9 // 如果需要绑定账号,请使用registerPush(getApplicationContext(),account)版本10 // 具体可参考详细的开发指南11 // 传递的参数为ApplicationContext12 //Context context = getApplicationContext();13 //1.获取设备的Token14 Handler handler = new HandlerExtension(MainActivity.this);15 message = handler.obtainMessage();16 XGPushManager.registerPush(getApplicationContext(), new XGIOperateCallback() {17 18 @Override19 public void onSuccess(Object data, int flag) {20 // TODO Auto-generated method stub21 Log.d("jiejie", "+++ register push sucess. token:" + data + " " + flag);22 token = data + "";23 message.obj = "+++ register push sucess. token:" + data;24 System.out.println(token);25 message.sendToTarget();26 }27 28 @Override29 public void onFail(Object data, int errCode, String msg) {30 // TODO Auto-generated method stub31 Log.d("jiejie", "+++ register push fail. token:" + data32 + ", errCode:" + errCode + ",msg:"33 + msg);34 message.obj = "+++ register push fail. token:" + data35 + ", errCode:" + errCode + ",msg:" + msg;36 message.sendToTarget();37 }38 });39 }40 private static class HandlerExtension extends Handler{41 WeakReference<MainActivity> mActivity;42 HandlerExtension(MainActivity activity) {43 mActivity = new WeakReference<MainActivity>(activity);44 }45 @Override46 public void handleMessage(Message msg) {47 // TODO Auto-generated method stub48 super.handleMessage(msg);49 MainActivity theActivity = mActivity.get();50 if(theActivity == null){51 theActivity = new MainActivity();52 }53 if(msg != null){54 Log.d(Constants.LogTag, msg.obj.toString());55 System.out.println("ddd"+msg.obj.toString());56 }57 }58 }
5.修改广播接受器中的onTextMessage方法,当消息发送来时,将消息发送到主界面,不展示通知
不过你先要知道你app是否正在运行状态
1 /** 2 * 判断是否运行在前台 3 * 4 * @param context 5 * @return 6 */ 7 public static boolean isRunningForeground(Context context) { 8 String packageName = getPackageName(context); 9 String topActivityClassName = getTopActivityName(context);10 Log.d("TAG", "packageName=" + packageName + ",topActivityClassName=" + topActivityClassName);11 if (packageName != null && topActivityClassName != null && topActivityClassName.startsWith(packageName)) {12 Log.d("TAG", "---> isRunningForeGround");13 return true;14 } else {15 Log.d("TAG", "---> isRunningBackGround");16 return false;17 }18 }19 20 // 方法2、通过RunningAppProcessInfo类判断(不需要额外权限):21 22 public static boolean isBackground(Context context) {23 ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);24 List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();25 for (RunningAppProcessInfo appProcess : appProcesses) {26 if (appProcess.processName.equals(context.getPackageName())) {27 if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {28 Log.i("后台", appProcess.processName);29 return true;30 } else {31 Log.i("前台", appProcess.processName);32 return false;33 }34 }35 }36 return false;37 }
6.通知EventBus来实现观察者模式在广播接收器中发送消息
1 // 消息透传 2 @Override 3 public void onTextMessage(Context context, XGPushTextMessage message) { 4 String text = "收到消息:" + message.toString(); 5 EventBus.getDefault().post(text); 6 System.out.println(text); 7 // 获取自定义key-value 8 PushTextMessage pushTextMessage = new PushTextMessage(); 9 10 String title = message.getTitle();11 String content = message.getContent();12 pushTextMessage.setTitle(title);13 pushTextMessage.setContent(content); 14 String customContent = message.getCustomContent();15 if (customContent != null && customContent.length() != 0) {16 try {17 // JSONObject obj = new JSONObject(customContent);18 // // key1为前台配置的key19 // if (!obj.isNull("key")) {20 // String value = http://www.mamicode.com/obj.getString("key");21 // LogUtils.log(LogTag, "get custom value:" + value);22 // }23 CustomContent custom = com.alibaba.fastjson.JSONObject.parseObject(customContent, CustomContent.class);24 if (custom != null) {25 pushTextMessage.setCustomContent(custom);26 }27 28 // ...29 } catch (Exception e) {30 System.out.println(e + "d");31 e.printStackTrace();32 }33 }34 show(context, text);35 Log.d("jiejie", "pushTextMessage:" + pushTextMessage);36 // EventBus.getDefault().post(pushTextMessage);37 try {38 // APP自主处理消息的过程...39 boolean isForeground = AppUtil.isRunningForeground(context);40 Log.d("jiejie", isForeground + "d");41 if (isForeground) {42 EventBus.getDefault().post(pushTextMessage);43 } else {44 notify(context, title, content);45 }46 } catch (Exception e) {47 System.out.println(e + "ddd");48 e.printStackTrace();49 }
7.在主界面中接受EventBus发送的消息,展示一个弹窗
1 @Subscribe 2 public void onMessageReviced(final PushTextMessage pushTextMessage){ 3 Log.d("jiejie", "好" + " 标题" +pushTextMessage.getTitle() + " 内容:" +pushTextMessage.getContent() + "CustomContent" + pushTextMessage.getCustomContent().getCmd()); 4 if(pushTextMessage != null){ 5 showAlertDialog(this, pushTextMessage); 6 } 7 } 8 private void showAlertDialog(Context context,PushTextMessage text){ 9 AlertDialog.Builder dialog = new AlertDialog.Builder(context);10 dialog.setTitle("推送的标题");11 dialog.setMessage(text.getContent());12 dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() {13 14 @Override15 public void onClick(DialogInterface arg0, int arg1) {16 // TODO Auto-generated method stub17 18 }19 });20 AlertDialog mDialog = dialog.create();21 mDialog.show();22 }23 24 @Override25 protected void onDestroy() {26 // TODO Auto-generated method stub27 super.onDestroy();28 EventBus.getDefault().unregister(this);29 }
信鸽推送的使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。