iOS中关于信鸽推送的使用demo详解
程序员文章站
2023-12-18 09:47:28
最近在看推送方面的知识,用的是信鸽推送主要是因为后台用的是信鸽
推送用第三方推送,也就是在客户端建一个广播接收器,当服务器发送消息时发送到信鸽,信鸽再发送一次,广播接受器...
最近在看推送方面的知识,用的是信鸽推送主要是因为后台用的是信鸽
推送用第三方推送,也就是在客户端建一个广播接收器,当服务器发送消息时发送到信鸽,信鸽再发送一次,广播接受器接受下;
我实现的功能比较简单,当app在运行状态时,会在主页展示一个弹窗展示推送的消息;如果app不在运行状态且service没被销毁就展示默认的通知
那么如何在主页展示弹窗:当广播接受器收到我要的消息时,用观察者模式,收到消息在发送个消息个主界面
官方的demo连接:
修改后demo的地址:
1.先按照官网的例子添加好权限及服务有些服务要改成自己的包名;
2.把官网的广播接受器先复制到自己的项目中,再在清单文件中添加;
3.随后添加从官网给出的3个accesskey了
<meta-data android:name="com.tencent.rdm.uuid" android:value="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或删除
//信鸽start private string token; private message message = null; private void xginit() { // todo auto-generated method stub xgpushconfig.enabledebug(this, true); // 如果需要知道注册是否成功,请使用registerpush(getapplicationcontext(), // xgioperatecallback)带callback版本 // 如果需要绑定账号,请使用registerpush(getapplicationcontext(),account)版本 // 具体可参考详细的开发指南 // 传递的参数为applicationcontext //context context = getapplicationcontext(); //1.获取设备的token handler handler = new handlerextension(mainactivity.this); message = handler.obtainmessage(); xgpushmanager.registerpush(getapplicationcontext(), new xgioperatecallback() { @override public void onsuccess(object data, int flag) { // todo auto-generated method stub log.d("jiejie", "+++ register push sucess. token:" + data + " " + flag); token = data + ""; message.obj = "+++ register push sucess. token:" + data; system.out.println(token); message.sendtotarget(); } @override public void onfail(object data, int errcode, string msg) { // todo auto-generated method stub log.d("jiejie", "+++ register push fail. token:" + data + ", errcode:" + errcode + ",msg:" + msg); message.obj = "+++ register push fail. token:" + data + ", errcode:" + errcode + ",msg:" + msg; message.sendtotarget(); } }); } private static class handlerextension extends handler{ weakreference<mainactivity> mactivity; handlerextension(mainactivity activity) { mactivity = new weakreference<mainactivity>(activity); } @override public void handlemessage(message msg) { // todo auto-generated method stub super.handlemessage(msg); mainactivity theactivity = mactivity.get(); if(theactivity == null){ theactivity = new mainactivity(); } if(msg != null){ log.d(constants.logtag, msg.obj.tostring()); system.out.println("ddd"+msg.obj.tostring()); } } }
5.修改广播接受器中的ontextmessage方法,当消息发送来时,将消息发送到主界面,不展示通知
不过你先要知道你app是否正在运行状态
/** * 判断是否运行在前台 * * @param context * @return */ public static boolean isrunningforeground(context context) { string packagename = getpackagename(context); string topactivityclassname = gettopactivityname(context); log.d("tag", "packagename=" + packagename + ",topactivityclassname=" + topactivityclassname); if (packagename != null && topactivityclassname != null && topactivityclassname.startswith(packagename)) { log.d("tag", "---> isrunningforeground"); return true; } else { log.d("tag", "---> isrunningbackground"); return false; } } // 方法2、通过runningappprocessinfo类判断(不需要额外权限): public static boolean isbackground(context context) { activitymanager activitymanager = (activitymanager) context.getsystemservice(context.activity_service); list<runningappprocessinfo> appprocesses = activitymanager.getrunningappprocesses(); for (runningappprocessinfo appprocess : appprocesses) { if (appprocess.processname.equals(context.getpackagename())) { if (appprocess.importance == runningappprocessinfo.importance_background) { log.i("后台", appprocess.processname); return true; } else { log.i("前台", appprocess.processname); return false; } } } return false; }
6.通知eventbus来实现观察者模式在广播接收器中发送消息
// 消息透传 @override public void ontextmessage(context context, xgpushtextmessage message) { string text = "收到消息:" + message.tostring(); eventbus.getdefault().post(text); system.out.println(text); // 获取自定义key-value pushtextmessage pushtextmessage = new pushtextmessage(); string title = message.gettitle(); string content = message.getcontent(); pushtextmessage.settitle(title); pushtextmessage.setcontent(content); string customcontent = message.getcustomcontent(); if (customcontent != null && customcontent.length() != 0) { try { // jsonobject obj = new jsonobject(customcontent); // // key1为前台配置的key // if (!obj.isnull("key")) { // string value = obj.getstring("key"); // logutils.log(logtag, "get custom value:" + value); // } customcontent custom = com.alibaba.fastjson.jsonobject.parseobject(customcontent, customcontent.class); if (custom != null) { pushtextmessage.setcustomcontent(custom); } // ... } catch (exception e) { system.out.println(e + "d"); e.printstacktrace(); } } show(context, text); log.d("jiejie", "pushtextmessage:" + pushtextmessage); // eventbus.getdefault().post(pushtextmessage); try { // app自主处理消息的过程... boolean isforeground = apputil.isrunningforeground(context); log.d("jiejie", isforeground + "d"); if (isforeground) { eventbus.getdefault().post(pushtextmessage); } else { notify(context, title, content); } } catch (exception e) { system.out.println(e + "ddd"); e.printstacktrace(); }
7.在主界面中接受eventbus发送的消息,展示一个弹窗
@subscribe public void onmessagereviced(final pushtextmessage pushtextmessage){ log.d("jiejie", "好" + " 标题" +pushtextmessage.gettitle() + " 内容:" +pushtextmessage.getcontent() + "customcontent" + pushtextmessage.getcustomcontent().getcmd()); if(pushtextmessage != null){ showalertdialog(this, pushtextmessage); } } private void showalertdialog(context context,pushtextmessage text){ alertdialog.builder dialog = new alertdialog.builder(context); dialog.settitle("推送的标题"); dialog.setmessage(text.getcontent()); dialog.setpositivebutton("确认", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface arg0, int arg1) { // todo auto-generated method stub } }); alertdialog mdialog = dialog.create(); mdialog.show(); } @override protected void ondestroy() { // todo auto-generated method stub super.ondestroy(); eventbus.getdefault().unregister(this); }
以上所述是小编给大家介绍的ios中关于信鸽推送的使用demo详解,希望对大家有所帮助