你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS中關於信鴿推送的使用demo詳解

iOS中關於信鴿推送的使用demo詳解

編輯: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);
}

以上所述是小編給大家介紹的iOS中關於信鴿推送的使用demo詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved