你好,歡迎來到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詳解,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對本站網站的支撐!

【iOS中關於信鴿推送的應用demo詳解】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

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