当前位置:主页 > 移动开发 > iOS代码 >

iOS中关于信鸽推送的使用demo详解

时间:2020-12-05 12:01:23 | 栏目:iOS代码 | 点击:

最近在看推送方面的知识,用的是信鸽推送主要是因为后台用的是信鸽

推送用第三方推送,也就是在客户端建一个广播接收器,当服务器发送消息时发送到信鸽,信鸽再发送一次,广播接受器接受下;

我实现的功能比较简单,当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="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);
}

您可能感兴趣的文章:

相关文章