iOS中关于信鸽推送的使用demo详解
最近在看推送方面的知识,用的是信鸽推送主要是因为后台用的是信鸽
推送用第三方推送,也就是在客户端建一个广播接收器,当服务器发送消息时发送到信鸽,信鸽再发送一次,广播接受器接受下;
我实现的功能比较简单,当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-dataandroid: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
privateStringtoken;
privateMessagemessage=null;
privatevoidXGInit(){
//TODOAuto-generatedmethodstub
XGPushConfig.enableDebug(this,true);
//如果需要知道注册是否成功,请使用registerPush(getApplicationContext(),
//XGIOperateCallback)带callback版本
//如果需要绑定账号,请使用registerPush(getApplicationContext(),account)版本
//具体可参考详细的开发指南
//传递的参数为ApplicationContext
//Contextcontext=getApplicationContext();
//1.获取设备的Token
Handlerhandler=newHandlerExtension(MainActivity.this);
message=handler.obtainMessage();
XGPushManager.registerPush(getApplicationContext(),newXGIOperateCallback(){
@Override
publicvoidonSuccess(Objectdata,intflag){
//TODOAuto-generatedmethodstub
Log.d("jiejie","+++registerpushsucess.token:"+data+""+flag);
token=data+"";
message.obj="+++registerpushsucess.token:"+data;
System.out.println(token);
message.sendToTarget();
}
@Override
publicvoidonFail(Objectdata,interrCode,Stringmsg){
//TODOAuto-generatedmethodstub
Log.d("jiejie","+++registerpushfail.token:"+data
+",errCode:"+errCode+",msg:"
+msg);
message.obj="+++registerpushfail.token:"+data
+",errCode:"+errCode+",msg:"+msg;
message.sendToTarget();
}
});
}
privatestaticclassHandlerExtensionextendsHandler{
WeakReference<MainActivity>mActivity;
HandlerExtension(MainActivityactivity){
mActivity=newWeakReference<MainActivity>(activity);
}
@Override
publicvoidhandleMessage(Messagemsg){
//TODOAuto-generatedmethodstub
super.handleMessage(msg);
MainActivitytheActivity=mActivity.get();
if(theActivity==null){
theActivity=newMainActivity();
}
if(msg!=null){
Log.d(Constants.LogTag,msg.obj.toString());
System.out.println("ddd"+msg.obj.toString());
}
}
}
5.修改广播接受器中的onTextMessage方法,当消息发送来时,将消息发送到主界面,不展示通知
不过你先要知道你app是否正在运行状态
/**
*判断是否运行在前台
*
*@paramcontext
*@return
*/
publicstaticbooleanisRunningForeground(Contextcontext){
StringpackageName=getPackageName(context);
StringtopActivityClassName=getTopActivityName(context);
Log.d("TAG","packageName="+packageName+",topActivityClassName="+topActivityClassName);
if(packageName!=null&&topActivityClassName!=null&&topActivityClassName.startsWith(packageName)){
Log.d("TAG","--->isRunningForeGround");
returntrue;
}else{
Log.d("TAG","--->isRunningBackGround");
returnfalse;
}
}
//方法2、通过RunningAppProcessInfo类判断(不需要额外权限):
publicstaticbooleanisBackground(Contextcontext){
ActivityManageractivityManager=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo>appProcesses=activityManager.getRunningAppProcesses();
for(RunningAppProcessInfoappProcess:appProcesses){
if(appProcess.processName.equals(context.getPackageName())){
if(appProcess.importance==RunningAppProcessInfo.IMPORTANCE_BACKGROUND){
Log.i("后台",appProcess.processName);
returntrue;
}else{
Log.i("前台",appProcess.processName);
returnfalse;
}
}
}
returnfalse;
}
6.通知EventBus来实现观察者模式在广播接收器中发送消息
//消息透传
@Override
publicvoidonTextMessage(Contextcontext,XGPushTextMessagemessage){
Stringtext="收到消息:"+message.toString();
EventBus.getDefault().post(text);
System.out.println(text);
//获取自定义key-value
PushTextMessagepushTextMessage=newPushTextMessage();
Stringtitle=message.getTitle();
Stringcontent=message.getContent();
pushTextMessage.setTitle(title);
pushTextMessage.setContent(content);
StringcustomContent=message.getCustomContent();
if(customContent!=null&&customContent.length()!=0){
try{
//JSONObjectobj=newJSONObject(customContent);
////key1为前台配置的key
//if(!obj.isNull("key")){
//Stringvalue=obj.getString("key");
//LogUtils.log(LogTag,"getcustomvalue:"+value);
//}
CustomContentcustom=com.alibaba.fastjson.JSONObject.parseObject(customContent,CustomContent.class);
if(custom!=null){
pushTextMessage.setCustomContent(custom);
}
//...
}catch(Exceptione){
System.out.println(e+"d");
e.printStackTrace();
}
}
show(context,text);
Log.d("jiejie","pushTextMessage:"+pushTextMessage);
//EventBus.getDefault().post(pushTextMessage);
try{
//APP自主处理消息的过程...
booleanisForeground=AppUtil.isRunningForeground(context);
Log.d("jiejie",isForeground+"d");
if(isForeground){
EventBus.getDefault().post(pushTextMessage);
}else{
notify(context,title,content);
}
}catch(Exceptione){
System.out.println(e+"ddd");
e.printStackTrace();
}
7.在主界面中接受EventBus发送的消息,展示一个弹窗
@Subscribe
publicvoidonMessageReviced(finalPushTextMessagepushTextMessage){
Log.d("jiejie","好"+"标题"+pushTextMessage.getTitle()+"内容:"+pushTextMessage.getContent()+"CustomContent"+pushTextMessage.getCustomContent().getCmd());
if(pushTextMessage!=null){
showAlertDialog(this,pushTextMessage);
}
}
privatevoidshowAlertDialog(Contextcontext,PushTextMessagetext){
AlertDialog.Builderdialog=newAlertDialog.Builder(context);
dialog.setTitle("推送的标题");
dialog.setMessage(text.getContent());
dialog.setPositiveButton("确认",newDialogInterface.OnClickListener(){
@Override
publicvoidonClick(DialogInterfacearg0,intarg1){
//TODOAuto-generatedmethodstub
}
});
AlertDialogmDialog=dialog.create();
mDialog.show();
}
@Override
protectedvoidonDestroy(){
//TODOAuto-generatedmethodstub
super.onDestroy();
EventBus.getDefault().unregister(this);
}
以上所述是小编给大家介绍的iOS中关于信鸽推送的使用demo详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!