更新时间:2020-07-14 11:53
环境准备:
通过gradle使用推送相关SDK需要配置仓库信息,在EMAS平台上进行构建和模块管理需要接入emas gradle插件。
SDK仓库和对接EMAS平台插件接入请参考以下文档的2.2-2.3章节:https://help.aliyun.com/document_detail/87884.html
gradle依赖:
compile('com.taobao.android:accs_sdk_taobao:3.3.7.4-emas') {
exclude group: 'com.taobao.android', module: 'networksdk'
transitive true
}
compile 'com.taobao.android:accs-xiaomi:1.0.9' //可选
compile 'com.taobao.android:accs-huawei:1.1.3' //可选
compile 'com.taobao.android:accs-oppo:1.0.3' //可选
compile('com.taobao.android:accs-meizu:1.0.6') { //可选
transitive false
}
compile 'com.taobao.android:meizu-push:3.8.1@aar' //可选
需要向EMAS平台获取appkey、appsecret、推送域名等信息。可从平台上下载的脚手架工程中配置文件:aliyun-emas-services.json获取相关配置信息。
{
"private_cloud_config":{
"AppKey": "10000021",
"AppSecret": "f7bd9a1adabacebfc1edf933ff1c020d",
...
"ACCS":{
"Domain": "accs.emas-poc.com" //推送域名,这里是poc环境的推送域名
},
"Network":{
"IPStrategy": {} //如无域名,只能通过ip建连,则将ACCS.Domain设置为poc环境域铭,并在此处指定其对应的ip和端口。示例:"IPStrategy": {"accs.emas-poc.com":"ip:port"}
},
...
}
}
接入厂商通道的需要自行到各个品牌的推送接入平台申请对应的key、secret
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- accs start -->
<service
android:name="com.taobao.accs.ChannelService"
android:exported="true"
android:process=":channel">
<intent-filter>
<action android:name="com.taobao.accs.intent.action.SERVICE" />
</intent-filter>
</service>
<service
android:name="com.taobao.accs.ChannelService$KernelService"
android:exported="false"
android:process=":channel" />
<service
android:name="com.taobao.accs.data.MsgDistributeService"
android:exported="true">
<intent-filter>
<action android:name="com.taobao.accs.intent.action.RECEIVE" />
</intent-filter>
</service>
<receiver
android:name="com.taobao.accs.EventReceiver"
android:process=":channel">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="com.taobao.accs.ServiceReceiver"
android:process=":channel">
<intent-filter>
<action android:name="com.taobao.accs.intent.action.COMMAND" />
</intent-filter>
<intent-filter>
<action android:name="com.taobao.accs.intent.action.START_FROM_AGOO" />
</intent-filter>
</receiver>
<service
android:name="com.taobao.accs.internal.AccsJobService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:process=":channel" />
<!-- accs end -->
<!-- agoo start -->
<service
android:name="org.android.agoo.accs.AgooService"
android:exported="true"
android:process=":channel">
<intent-filter>
<action android:name="com.taobao.accs.intent.action.RECEIVE" />
</intent-filter>
</service>
<!-- 接受agoo本地的控制事件 -->
<receiver
android:name="com.taobao.agoo.AgooCommondReceiver"
android:exported="true"
android:process=":channel">
<intent-filter>
<action android:name="{$packageName}.intent.action.COMMAND" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<!-- accs的2.2.2以及以后基于intentService,用于消息转发,提高消息送达率 -->
<service
android:name="com.taobao.agoo.TaobaoMessageIntentReceiverService"
android:exported="true"
android:process=":channel">
<intent-filter>
<action android:name="org.android.agoo.client.MessageReceiverService" />
</intent-filter>
</service>
<!-- agoo end -->
<!-- 可选厂商通道相关 -->
<!-- 魅族 start -->
<!-- push应用定义消息receiver声明 -->
<receiver android:name=".DemoMeizuPushMsgReceiver">
<intent-filter>
<!-- 接收push消息 -->
<action android:name="com.meizu.flyme.push.intent.MESSAGE" />
<!-- 接收register消息 -->
<action android:name="com.meizu.flyme.push.intent.REGISTER.FEEDBACK" />
<!-- 接收unregister消息 -->
<action android:name="com.meizu.flyme.push.intent.UNREGISTER.FEEDBACK" />
<!-- 兼容低版本Flyme3推送服务配置 -->
<action android:name="com.meizu.c2dm.intent.REGISTRATION" />
<action android:name="com.meizu.c2dm.intent.RECEIVE" />
<category android:name="${PackgeName}" />
</intent-filter>
</receiver>
<!-- 魅族 end -->
<!-- 华为 start -->
<meta-data
android:name="com.huawei.hms.client.appid"
android:value="appid=xxxxx" />
<!-- 华为 end -->
必须调用, 构造一个初始化配置项,可参考从平台下载的脚手架工程中的配置文件aliyun-emas-services.json,详细见前文[环境配置]章节
AccsClientConfig.Builder clientBuilder = new AccsClientConfig.Builder()
.setAppKey(String appKey) //必选, 所属appkey
.setTag(AccsClientConfig.DEFAULT_CONFIGTAG) //必选, 多实例下tag, 其它sdk默认会使用该实例, 此时该值必须是`AccsClientConfig.DEFAULT_CONFIGTAG`
.setAppSecret(String appsecret) //必选, 所属秘钥
.setKeepAlive(boolean keepalive) //必选, 是否保活应用内连接, 建议值true
.setDisableChannel(boolean disableChannel); //必选,建议值true
.setInappHost(String host) //推送域名
.setInappPubKey(int pubKey) //连接公钥,sdk需要设置为SpdyProtocol.PUBKEY_SEQ_OPEN
.build();
ACCSClient.init(Context context, clientBuilder);
设备信息注册,绑定结果会在IRegister中异步返回,切记要放application里面调用, 以确保在主进程和CHANNEL进程都能执行到
/**
* agoo设备注册接口
* @param context
* @param accsConfigTag 指定accs实例tag, 此时应该使用上述的AccsClientConfig.DEFAULT_CONFIGTAG
* @param appKey 上文申请的appkey
* @param appSecret 上文申请的秘钥,使用无线保镖时无需填写
* @param ttid 渠道id, 可不填
* @param callback 注册结果回调
*/
TaobaoRegister.register(Context context, String accsConfigTag, String appKey, String appSecret, String ttid, IRegister callback) throws AccsException
class IRegister {
//agoo注册成功回调
public abstract void onSuccess(String deviceToken);
//agoo注册失败回调
public abstract void onFailure(String errCode, String errDesc);
}
//厂商通道初始化, 在各家push官网申请拿到, 同时服务token务必填入EMAS推送平台
String phoneBrand = Build.BRAND;
//简易判断机型, 可能不标准, 仅供参考
if ("huawei".equalsIgnoreCase(phoneBrand) || "honor".equalsIgnoreCase(phoneBrand)) {
HuaWeiRegister.register(mApplication);//华为通道(可选)
} else if ("xiaomi".equalsIgnoreCase(phoneBrand)) {
MiPushRegistar.register(mApplication, XIAOMI_APPID, XIAOMI_APPKEY);//小米通道(可选)
} else if ("oppo".equalsIgnoreCase(phoneBrand)) {
OppoRegister.register(mApplication, OPPO_APPKEY, OPPO_APPSECRET);//OPPO通道(可选)
} else if ("meizu".equalsIgnoreCase(phoneBrand)) {
MeizuRegister.register(mApplication, MEIZU_APPID, MEIZU_APPKEY);//MEIZU通道(可选)
}
非必须调用, 默认情况下TaobaoRegister.register
接口执行之后agoo就是开启的, 但是如果想运行时就关闭agoo推送, 那么调用TaobaoRegister.unbindAgoo
接口. 如果想重新开启agoo推送, 调用TaobaoRegister.bindAgoo
接口
非必须调用, 仅需要按用户维度推送的APP需要调用
当程序自身的用户进行登陆后,可以使用以下方法绑定用户,绑定结果会在ICallback中异步返回
TaobaoRegister.addAlias(Context context, String alias, ICallback bindAlias)
//当用户注销时,使用以下方法解绑用户,解绑结果会在会在ICallback中异步返回
TaobaoRegister.removeAlias(Context context, String alias, ICallback bindAlias)
消息解析示例:
String messageId = intent.getStringExtra(AgooConstants.MESSAGE_ID);
String message = intent.getStringExtra(AgooConstants.MESSAGE_BODY);
如果接入厂商通道,则需要做系统弹窗跳转activity配置。
业务方指定的该Activity需要继承自com.taobao.agoo.BaseNotifyClickActivity
, 同时实现父类的onMessage
方法, 对该方法的intent
参数进一步解析即可
<activity
android:name=".ThirdNotifyClickedActivity"
android:exported="true" />
public class ThirdNotifyClickedActivity extends BaseNotifyClickActivity {
private static final String TAG = "ThirdNotifyClickedActivity";
private final static String PRE_NOTIFY = "测试通知点击界面 内容: ";
@BindView(R.id.text)
public TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notify_clicked);
ButterKnife.bind(this);
mTextView.setText(PRE_NOTIFY);
ALog.d(TAG, "onCreate");
}
@Override
public void onMessage(final Intent intent) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String body = intent.getStringExtra(AgooConstants.MESSAGE_BODY);//拿到厂商通道托管弹窗消息内容
mTextView.setText(PRE_NOTIFY + body);
}
});
}
}
public static IRegister agooRegister = new IRegister() {
@Override
public void onSuccess(String deviceToken) {
ALog.i(TAG, "register onSuccess", "deviceToken", deviceToken);
}
@Override
public void onFailure(String errorCode, String errorMsg) {
ALog.e(TAG, "register onFailure", "errorCode", errorCode, "errorMsg", errorMsg);
}
};
private IAppReceiver mAppReceiver = new IAppReceiver() {
private String TAG = "mAppReceiver";
@Override
public void onBindApp(int errorCode) {
ALog.i(TAG, "onBindApp", "errorCode", errorCode);
try {
ACCSClient.getAccsClient(AccsClientConfig.DEFAULT_CONFIGTAG).bindUser("123324234");
} catch (AccsException e) {
e.printStackTrace();
}
}
@Override
public void onUnbindApp(int errorCode) {
ALog.i(TAG, "onUnbindApp", "errorCode", errorCode);
}
@Override
public void onBindUser(String userId, int errorCode) {
ALog.i(TAG, "onBindUser", "errorCode", errorCode);
}
@Override
public void onUnbindUser(int errorCode) {
ALog.i(TAG, "onUnbindUser", "errorCode", errorCode);
}
@Override
public void onSendData(String dataId, int errorCode) {
ALog.i(TAG, "onSendData");
}
@Override
public void onData(String userId, String dataId, byte[] data) {
ALog.i(TAG, "onData");
}
@Override
public String getService(String serviceId) {
String service = SERVICES.get(serviceId);
return service;
}
@Override
public Map<String, String> getAllServices() {
return SERVICES;
}
};
private void initAccs() {
ACCSManager.setAppkey(mApplication.getApplicationContext(), mAppkey, env);//兼容老接口 如果有任意地方使用老接口,必须setAppkey
NetworkSdkSetting.init(mApplication.getApplicationContext());
//关闭AMDC请求
HttpDispatcher.getInstance().setEnable(false);
ACCSClient.setEnvironment(mApplication.getApplicationContext(), env);
AwcnConfig.setAccsSessionCreateForbiddenInBg(false);
final AccsClientConfig clientConfig = new AccsClientConfig.Builder()
.setAppKey(appkey)
.setAppSecret(appsecret)
.setInappHost(emasHost)
.setInappPubKey(pubkey)
.setChannelHost(emasHost)
.setChannelPubKey(pubkey)
.setTag(AccsClientConfig.DEFAULT_CONFIGTAG)
.setConfigEnv(env)
.build();
new Thread() { //建议异步进行初始化
@Override
public void run() {
try {
ACCSClient.init(mApplication, clientConfig);
ACCSClient.getAccsClient(AccsClientConfig.DEFAULT_CONFIGTAG).bindApp(mChannelID, mAppReceiver);
initAgooPush();
} catch (AccsException e) {
ALog.w(TAG, "initDefaultAccsConfig", e);
}
}
}.start();
}
private void initAgooPush() {
try {
TaobaoRegister.setAgooMsgReceiveService(mReceivePushService);
TaobaoRegister.register(mApplication, AccsClientConfig.DEFAULT_CONFIGTAG, mAppkey, mAppSecret, "",
new IRegister() {
@Override
public void onSuccess(String deviceToken) {
ALog.d(TAG, "agoo register success");
AgooActivity.accsState = "online";
AgooActivity.accsToken = deviceToken;
}
@Override
public void onFailure(String s, String s1) {
ALog.w(TAG, "agoo register fail" + s + s1);
AgooActivity.accsState = "error:" + s;
}
});
}
//DemoAgooService.java消息接收
public class DemoAgooService extends TaobaoBaseIntentService {
.....
@Override
protected void onMessage(Context context, Intent intent) {
String messageId = intent.getStringExtra(AgooConstants.MESSAGE_ID);
String message = intent.getStringExtra(AgooConstants.MESSAGE_BODY);
ALog.i(TAG, "onMessage", "messageId", messageId, "message", message);
//解析agoo消息, 通知栏显示, 仅供参考
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
.....
manager.notify(notifyID, builder.build());
}
}
错误码 | 描述 | 备注 |
---|---|---|
200 | 成功 | |
-1 | 未知网络异常 | |
-2 | 参数非法或为空 | |
-3 | 服务端异常 | |
-4 | 消息过大 | |
-5 | 上行数据域名为空 | |
-6 | 鉴权参数异常或为空 | |
-7 | 鉴权异常 | |
-8 | 未知错误 | |
-9 | 请求超时 | 查看tag awcn error日志看连接有没有成功建立 |
-10 | 连接中断 | |
-11 | 从网络库拿不到网络session | 1、查看tag awcn error级别日志看连接有没有成功建立 2、网络库/mtop开启了降级 3、找不到tnet.so或者tnet.so和tnetjni版本不匹配 |
-12 | ping超时 | |
-13 | 无网络 | |
-14 | APPKEY为空 | |
-15 | app secret为空 | |
-16 | bindapp传入的callback为空 | |
-17 | ACCS被禁用 |
错误码 | 描述 | 备注 |
---|---|---|
-1108 | so加载失败 | tnet so没有打进包里或者tnet jni与so的版本不匹配 |
-38** | http2协议错误 | |
-2002 | 客户端主动关闭连接 | 一般是请求超时导致 |
-2003 | 建连超时 | |
-2004 | 请求超时 | 请求超时 |
-2005 | spdy stream被reset | |
-2032 | 服务端关闭连接 | |
-2611 | Connection refused | 一般手机网络有问题或者流量被禁用 |
-2613 | No route to host | 一般手机网络有问题或者流量被禁用 |
-3* | ssl错误 |
错误码 | 描述 | 备注 |
---|---|---|
100 | dm异常 | 联系技术支持排查 |
101 | sal异常 | 联系技术支持排查 |
102 | appkey非法 or tail访问失败 | 没有联系技术支持配appkey |
103 | packagename 非法 | |
104 | serviceId 非法 | 没有联系技术支持配serviceId |
105 | 设备离线 | |
106 | 服务异常 | 检查业务后台是否正常,正常则联系服务端排查 |
300 | app没有绑定 | 没有执行bindApp或者bindApp失败 ,查看bindapp返回错误码 |
314 | appkey没注册 | 检查应用创建 |
在文档使用中是否遇到以下问题
更多建议
匿名提交