(一) 集成資料
1.華為管理后臺(tái)登錄地址 :https://hwid1.vmall.com/CAS/portal/loginAuth.html
2.sdk接入說(shuō)明網(wǎng)址:https://developer.huawei.com/consumer/cn/service/hms/catalog/huaweipush_agent.html?page=hmssdk_huaweipush_sdkdownload_agent
3.華為官方QQ群:662059980
(二) V6項(xiàng)目集成步驟和代碼說(shuō)明
1.華為官網(wǎng)申請(qǐng)PUSH服務(wù):
1.1申請(qǐng)
點(diǎn)擊申請(qǐng)按鈕進(jìn)行申請(qǐng)
1.2提交應(yīng)用信息
填寫應(yīng)用信息后提交
1.3生成應(yīng)用信息
應(yīng)用信息頁(yè)面
2.V6代碼整合步驟:
2.1 引入華為sdk依賴庫(kù)
compile 'com.huawei.android.hms:push:2.5.2.300'
2.2 build.gradle中配置maven地址
maven {
url 'http://developer.huawei.com/repo/'
}
2.3 AndroidManifest.xml中增加華為receiver诀艰,更換appid
<!-- 華為推送配置 start - -->
<!-- 第三方相關(guān) :接收Push消息(注冊(cè)、Push消息汁政、Push連接狀態(tài)、標(biāo)簽纹笼,LBS上報(bào)結(jié)果)廣播 -->
<meta-data
android:name="com.huawei.hms.client.appid"
android:value="10896842" />
<activity
android:name="com.huawei.hms.activity.BridgeActivity"
android:configChanges="orientation|locale|screenSize|layoutDirection|fontScale"
android:excludeFromRecents="true"
android:exported="false"
android:hardwareAccelerated="true"
android:theme="@android:style/Theme.Translucent">
<meta-data
android:name="hwc-theme"
android:value="androidhwext:style/Theme.Emui.Translucent" />
</activity>
<provider
android:name="com.huawei.hms.update.provider.UpdateProvider"
android:authorities="${applicationId}.hms.update.provider"
android:exported="false"
android:grantUriPermissions="true" />
<!-- xxx.xx.xx為CP自定義的廣播名稱踢故,比如: com.huawei.hmssample. HuaweiPushRevicer -->
<receiver android:name="com.epoint.GS.receiver.HW_Receiver">
<intent-filter>
<!-- 必須,用于接收TOKEN -->
<action android:name="com.huawei.android.push.intent.REGISTRATION" />
<!-- 必須溯饵,用于接收消息 -->
<action android:name="com.huawei.android.push.intent.RECEIVE" />
<!-- 可選,用于點(diǎn)擊通知欄或通知欄上的按鈕后觸發(fā)onEvent回調(diào) -->
<action android:name="com.huawei.android.push.intent.CLICK" />
<!-- 可選背镇,查看PUSH通道是否連接咬展,不查看則不需要 -->
<action android:name="com.huawei.intent.action.PUSH_STATE" />
</intent-filter>
</receiver>
<receiver android:name="com.huawei.hms.support.api.push.PushEventReceiver">
<intent-filter>
<!-- 接收通道發(fā)來(lái)的通知欄消息泽裳,兼容老版本PUSH -->
<action android:name="com.huawei.intent.action.PUSH" />
</intent-filter>
</receiver>
<!-- end -->
2.4 重寫華為PushReceiver
import android.app.NotificationManager;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import com.epoint.GS.frm.CGTTipsManager;
import com.epoint.GS.task.Task_registHuaWeiPushToken;
import com.epoint.GS.utils.MyLog;
import com.epoint.frame.core.db.service.FrmDBService;
import com.epoint.frame.core.net.WebServiceUtilDAL;
import com.epoint.mobileim.utils.ImDBFrameUtil;
import com.epoint.mobileoa.action.MOABaseAction;
import com.epoint.mobileoa.action.MOAConfigKeys;
import com.epoint.mobileoa.utils.MOABaseInfo;
import com.huawei.hms.support.api.push.PushReceiver;
/*
* 接收Push所有消息的廣播接收器
*/
public class HW_Receiver extends PushReceiver {
String TAG = "HW_Receiver";
@Override
public void onToken(Context context, String token, Bundle extras) {
String belongId = extras.getString("belongId");
String pushToken = token;
String content = "獲取token和belongId成功,token = " + token + ",belongId = " + belongId;
MOABaseAction.writeLogThread(TAG, content);
registHuaWeiPushToken(token);
FrmDBService.setConfigValue(MOAConfigKeys.HW_Token, token);
Log.i("token",pushToken);
}
/**
* 注冊(cè)華為token
* @param token
*/
private void registHuaWeiPushToken(final String token) {
/**
*
*/
new Thread(new Runnable() {
@Override
public void run() {
String method = "registHuaWeiPushToken";
String url = MOABaseInfo.getPlatformWebservice();
String namespace = "http://server.service.axis2";
WebServiceUtilDAL ws = new WebServiceUtilDAL(url, method, namespace);
ws.addProperty("UserGuid", MOABaseInfo.getUserGuid());
ws.addProperty("PushToken", token);
ws.addProperty("appguid", "moa_app_standard_v6");
MOABaseAction.writeLogThread("注冊(cè)token接口地址",url+"/"+method);
MOABaseAction.writeLogThread("注冊(cè)token","UserGuid:"+MOABaseInfo.getUserGuid()+"PushToken:"+token+"appguid:"+"moa_app_standard_v6");
String bs = ws.start();
MOABaseAction.writeLogThread("注冊(cè)token",bs);
}
}).start();
}
@Override
public boolean onPushMsg(Context context, byte[] msg, Bundle bundle) {
String content = /*"收到一條Push消息: " +*/ new String(msg, "UTF-8");
return false;
}
public void onEvent(Context context, Event event, Bundle extras) {
Log.i(TAG, extras.toString());
if (Event.NOTIFICATION_OPENED.equals(event) || Event.NOTIFICATION_CLICK_BTN.equals(event)) {
int notifyId = extras.getInt(BOUND_KEY.pushNotifyId, 0);
if (0 != notifyId) {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(notifyId);
}
String content = "收到通知附加消息: " + extras.getString(BOUND_KEY.pushMsgKey);
Log.i(TAG, content);
}
super.onEvent(context, event, extras);
}
}
2.5 增加判斷是否為華為設(shè)備的方法(官方提供破婆,保證使用)
/**
* 華為rom
* @return
*/
public static boolean isEMUI(Context context) {
PackageInfo pi = null;
PackageManager pm = context.getPackageManager();
int hwid = 0;
try {
pi = pm.getPackageInfo("com.huawei.hwid", 0);
if (pi != null) {
hwid = pi.versionCode;
Log.i("hwid",""+hwid);
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if(hwid==0){
return false;
}else{
return true;
}
}
2.6 登錄頁(yè)面增加 華為初始化連接并實(shí)現(xiàn)回調(diào)
/**
*初始化連接
**/
if (AndtoidRomUtil.isEMUI(getContext())) {
HuaWeiPushManager.getInstance(this).connect();
}
/**
*進(jìn)行連接獲取token
**/
if (AndtoidRomUtil.isEMUI(getContext())) {
//獲取token
HuaWeiPushManager.getTokenAsyn();
//獲取push連接狀態(tài)
HuaWeiPushManager.getPushStatus();
}
//-----------------------------------------------------------------------
@Override
protected void onDestroy() {
super.onDestroy();
if (AndtoidRomUtil.isEMUI(getContext())) {
HuaWeiPushManager.getInstance(this).disconnect();
}
}
@Override
public void onConnected() {
MOABaseAction.writeLogThread("華為連接", "華為連接成功");
// ToastUtil.toastShort(MOALoginActivity.this,"華為連接成功");
//是否允許透?jìng)飨? HuaWeiPushManager.setReceiveNormalMsg(true);
new Thread(new Runnable() {
@Override
public void run() {
//是否接收通知欄消息
HuaWeiPushManager.setReceiveNotifyMsg(true);
}
}).start();
}
@Override
public void onConnectionSuspended(int i) {
//HuaweiApiClient異常斷開連接, if 括號(hào)里的條件可以根據(jù)需要修改
}
//調(diào)用HuaweiApiAvailability.getInstance().resolveError傳入的第三個(gè)參數(shù)
//作用同startactivityforresult方法中的requestcode
private static final int REQUEST_HMS_RESOLVE_ERROR = 1000;
private String TAG = "華為";
@Override
public void onConnectionFailed(ConnectionResult arg0) {
Log.i("華為連接", "HuaweiApiClient連接失敗涮总,錯(cuò)誤碼:" + arg0.getErrorCode());
MOABaseAction.writeLogThread("華為連接", "HuaweiApiClient連接失敗,錯(cuò)誤碼:" + arg0.getErrorCode());
// ToastUtil.toastShort(MOALoginActivity.this,"HuaweiApiClient連接失敗祷舀,錯(cuò)誤碼:"+arg0.getErrorCode()+"");
if (HuaweiApiAvailability.getInstance().isUserResolvableError(arg0.getErrorCode())) {
final int errorCode = arg0.getErrorCode();
new Handler(getMainLooper()).post(new Runnable() {
@Override
public void run() {
// 此方法必須在主線程調(diào)用, xxxxxx.this 為當(dāng)前界面的activity
HuaweiApiAvailability.getInstance().resolveError(MOALoginActivity.this, errorCode, REQUEST_HMS_RESOLVE_ERROR);
}
});
} else {
//其他錯(cuò)誤碼請(qǐng)參見開發(fā)指南或者API文檔
}
}
2.7 增加中間平臺(tái)注冊(cè)和注銷token的接口
注冊(cè)token
import com.epoint.frame.core.net.WebServiceUtilDAL;
import com.epoint.frame.core.task.BaseRequestor;
import com.epoint.mobileoa.utils.MOABaseInfo;
public class Task_registHuaWeiPushToken extends BaseRequestor {
public String UserGuid;
public String PushToken;
public String appguid;
@Override
public Object execute() {
String method = "registHuaWeiPushToken";
String url = MOABaseInfo.getPlatformWebservice();
String namespace = "http://server.service.axis2";
WebServiceUtilDAL ws = new WebServiceUtilDAL(url, method, namespace);
ws.addProperty("UserGuid", UserGuid);
ws.addProperty("PushToken", PushToken);
ws.addProperty("appguid", appguid);
String bs = ws.start();
return bs;
}
}
注銷token
import com.epoint.frame.core.net.WebServiceUtilDAL;
import com.epoint.frame.core.task.BaseRequestor;
import com.epoint.mobileoa.utils.MOABaseInfo;
public class Task_deleteHuaWeiPushToken extends BaseRequestor{
public String token ;
@Override
public Object execute() {
String method = "deleteHuaWeiPushToken";
String url = MOABaseInfo.getPlatformWebservice();
String namespace = "http://server.service.axis2";
WebServiceUtilDAL ws = new WebServiceUtilDAL(url, method, namespace);
ws.addProperty("token", token);
String bs = ws.start();
return bs;
}
}