(一) 集成资料
3.华为官方QQ群:662059980
(二) V6项目集成步骤和代码说明
1.华为官网申请PUSH服务:
1.1申请
点击申请按钮进行申请
1.2提交应用信息
填写应用信息后提交
1.3生成应用信息
应用信息页面
2.V6代码整合步骤:
2.1 引入华为sdk依赖库
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 - -->
<!-- 第三方相关 :接收Push消息(注册、Push消息、Push连接状态、标签,LBS上报结果)广播 -->
<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" />
<!-- 可选,用于点击通知栏或通知栏上的按钮后触发onEvent回调 -->
<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>
<!-- 接收通道发来的通知栏消息,兼容老版本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);
}
/**
* 注册华为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("注册token接口地址",url+"/"+method);
MOABaseAction.writeLogThread("注册token","UserGuid:"+MOABaseInfo.getUserGuid()+"PushToken:"+token+"appguid:"+"moa_app_standard_v6");
String bs = ws.start();
MOABaseAction.writeLogThread("注册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 增加判断是否为华为设备的方法(官方提供,保证使用)
/**
* 华为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 登录页面增加 华为初始化连接并实现回调
/**
*初始化连接
**/
if (AndtoidRomUtil.isEMUI(getContext())) {
HuaWeiPushManager.getInstance(this).connect();
}
/**
*进行连接获取token
**/
if (AndtoidRomUtil.isEMUI(getContext())) {
//获取token
HuaWeiPushManager.getTokenAsyn();
//获取push连接状态
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,"华为连接成功");
//是否允许透传消息
HuaWeiPushManager.setReceiveNormalMsg(true);
new Thread(new Runnable() {
@Override
public void run() {
//是否接收通知栏消息
HuaWeiPushManager.setReceiveNotifyMsg(true);
}
}).start();
}
@Override
public void onConnectionSuspended(int i) {
//HuaweiApiClient异常断开连接, if 括号里的条件可以根据需要修改
}
//调用HuaweiApiAvailability.getInstance().resolveError传入的第三个参数
//作用同startactivityforresult方法中的requestcode
private static final int REQUEST_HMS_RESOLVE_ERROR = 1000;
private String TAG = "华为";
@Override
public void onConnectionFailed(ConnectionResult arg0) {
Log.i("华为连接", "HuaweiApiClient连接失败,错误码:" + arg0.getErrorCode());
MOABaseAction.writeLogThread("华为连接", "HuaweiApiClient连接失败,错误码:" + arg0.getErrorCode());
// ToastUtil.toastShort(MOALoginActivity.this,"HuaweiApiClient连接失败,错误码:"+arg0.getErrorCode()+"");
if (HuaweiApiAvailability.getInstance().isUserResolvableError(arg0.getErrorCode())) {
final int errorCode = arg0.getErrorCode();
new Handler(getMainLooper()).post(new Runnable() {
@Override
public void run() {
// 此方法必须在主线程调用, xxxxxx.this 为当前界面的activity
HuaweiApiAvailability.getInstance().resolveError(MOALoginActivity.this, errorCode, REQUEST_HMS_RESOLVE_ERROR);
}
});
} else {
//其他错误码请参见开发指南或者API文档
}
}
2.7 增加中间平台注册和注销token的接口
注册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;
}
}
网友评论