本文适用于 Android SDK 3.x,具体版本下限以各接口说明为准。文中的“及以上版本”均限定在 3.x 范围内。使用 4.x 的应用请参见基础配置接口(4.x)。
|
配置项 |
说明 |
|
Application必须配置。 |
|
|
配置AppKey,如果在 |
|
|
配置AppSecret,如果在 |
|
|
关闭channel进程。 |
|
|
关闭channel进程心跳。 |
|
|
开启Channel进程时,设置定期循环启动Channel进程。默认关闭循环启动。 |
|
|
配置定期循环启动Channel进程的时间间隔,只有在开启定期循环启动Channel进程时才会生效,默认间隔5分钟。 |
|
|
注册推送通道,开始接收推送。可以根据需要延迟注册,比如需要用户签署完隐私政策 |
|
|
获取设备唯一标识,指定设备推送时需要。 |
|
|
在通道初始化之前设置日志等级,默认等级为CloudPushService.LOG_DEBUG。 |
|
|
打开推送,推送默认就是打开状态,一般与关闭推送通道配套使用,只有关闭过才需要打开推送。 |
|
|
关闭推送,注意此时并不是真正断开推送通道,而是告诉服务这个设备不接收推送了。关闭之后,即使重新初始化SDK也不会打开推送,需要主动调用打开推送,才会重新接收推送。 |
|
|
判断当前推送打开关闭状态。 |
|
|
设置接收推送的服务,服务需要继承AliyunMessageIntentService。默认使用广播的方式接收推送,设置之后会改为使用服务接收推送。 |
|
|
注册日志接口,用于接收SDK日志信息,排查问题。 |
配置Application
配置application。
application必须配置。
application
接口定义
PushInitConfig.Builder application(Application application)
所属类
PushInitConfig.Builder
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
application |
Application |
是 |
Application |
代码示例
PushInitConfig config = new PushInitConfig.Builder()
.application(application)
.build();val config = PushInitConfig.Builder()
.application(application)
.build()配置AppKey
配置AppKey,如果在AndroidManifest.xml中已经配置,这里可以不调用。
appKey
接口定义
PushInitConfig.Builder appKey(String appKey)
所属类
PushInitConfig.Builder
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
appKey |
String |
是 |
EMAS的appKey |
代码示例
PushInitConfig config = new PushInitConfig.Builder()
.appKey(appKey)
.build();val config = PushInitConfig.Builder()
.appKey(appKey)
.build()配置AppSecret
配置AppSecret,如果在AndroidManifest.xml中已经配置,这里可以不调用。
appSecret
接口定义
PushInitConfig.Builder appSecret(String appSecret)
所属类
PushInitConfig.Builder
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
appSecret |
String |
是 |
EMAS的appSecret |
代码示例
PushInitConfig config = new PushInitConfig.Builder()
.appSecret(appSecret)
.build();val config = PushInitConfig.Builder()
.appSecret(appSecret)
.build()配置关闭channel进程
关闭channel进程。
disableChannelProcess
接口定义
PushInitConfig.Builder disableChannelProcess(boolean disableChannelProcess)
该接口已经废弃。默认channel进程是关闭的,如果要开启channel进程,请在AndroidManifest.xml中将ChannelService、ChannelService$KernelService的enable属性设为true。
所属类
PushInitConfig.Builder
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
disableChannelProcess |
boolean |
是 |
|
代码示例
PushInitConfig config = new PushInitConfig.Builder()
.disableChannelProcess(false)
.build();val config = PushInitConfig.Builder()
.disableChannelProcess(false)
.build()关闭Channel进程心跳
关闭channel进程心跳。
disableChannelProcessHeartbeat
接口定义
PushInitConfig.Builder disableChannelProcessHeartbeat(boolean disableChannelProcessHeartbeat)
该接口已经废弃。默认channel进程心跳是关闭的,如果要开启channel进程心跳,请在AndroidManifest.xml中将AccsJobService、EventReceiver、ServiceReceiver的enable属性设为true。
所属类
PushInitConfig.Builder
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
disableChannelProcessHeartbeat |
boolean |
是 |
|
代码示例
PushInitConfig config = new PushInitConfig.Builder()
.disableChannelProcessHeartbeat(false)
.build();val config = PushInitConfig.Builder()
.disableChannelProcessHeartbeat(false)
.build()定期循环启动Channel进程
开启Channel进程时,设置定期循环启动Channel进程。默认关闭循环启动。
loopStartChannel
接口定义
PushInitConfig.Builder loopStartChannel(boolean enable)
所属类
PushInitConfig.Builder
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
loopStartChannel |
boolean |
是 |
|
代码示例
PushInitConfig config = new PushInitConfig.Builder()
.loopStartChannel(true)
.build();val config = PushInitConfig.Builder()
.loopStartChannel(true)
.build()配置定期循环启动Channel进程的时间间隔
配置定期循环启动Channel进程的时间间隔,只有在开启定期循环启动Channel进程时才会生效,默认间隔是5分钟。
loopInterval
接口定义
PushInitConfig.Builder loopInterval(long interval)
所属类
PushInitConfig.Builder
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
loopInterval |
long |
是 |
循环时间间隔,单位时毫秒。 |
代码示例
PushInitConfig config = new PushInitConfig.Builder()
.loopInterval(5 * 60 * 1000)
.build();val config = PushInitConfig.Builder()
.loopInterval(5 * 60 * 1000)
.build()注册推送通道
注册推送通道,开始接收推送。可以根据需要延迟注册,比如需要用户签署完隐私政策
register
接口定义
void register(Context context, CommonCallback callback)
所属类
CloudPushService
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
context |
Context |
是 |
应用上下文(需要ApplicationContext) |
|
callback |
CommonCallback |
是 |
回调,错误码参见错误处理(3.x) |
代码示例
CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.register(applicationContext, new CommonCallback() {
@Override
public void onSuccess(String response) {
Log.i(TAG, "init cloudchannel success " + pushService.getDeviceId());
}
@Override
public void onFailed(String errorCode, String errorMessage) {
Log.e(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
}
});val pushService = PushServiceFactory.getCloudPushService()
pushService.register(applicationContext, object : CommonCallback() {
override fun onSuccess(response: String?) {
Log.i(TAG, "init cloudchannel success " + pushService.deviceId)
}
override fun onFailed(errorCode: String, errorMessage: String) {
Log.e(TAG, "init cloudchannel failed -- errorcode:$errorCode -- errorMessage:$errorMessage")
}
})register
支持动态设置AppKey、AppSecret的注册接口,与注册功能相同。
接口定义
void register(Context context, String appKey, String appSecret, CommonCallback callback)
该接口已经废弃。
所属类
CloudPushService
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
context |
Context |
是 |
应用上下文(需要ApplicationContext) |
|
appKey |
String |
是 |
EMAS的appKey |
|
appSecret |
String |
是 |
EMAS的appSecret |
|
callback |
CommonCallback |
是 |
回调,错误码参见错误处理(3.x) |
获取设备标识
获取设备唯一标识,指定设备推送时需要。
getDeviceId
接口定义
String getDeviceId()
所属类
CloudPushService
返回说明
|
类型 |
说明 |
|
String |
设备唯一标识 |
代码示例
String deviceId = PushServiceFactory.getCloudPushService().getDeviceId();val deviceId = PushServiceFactory.getCloudPushService().deviceId设置日志等级
本节描述 3.x 的日志配置与监听方式。4.x 的统一日志 TAG、初始化后的日志等级设置顺序和监听范围请参见基础配置接口(4.x)。
在通道初始化之前设置日志等级,默认等级为CloudPushService.LOG_DEBUG。
setLogLevel
接口定义
void setLogLevel(int logLevel)
所属类
CloudPushService
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
logLevel |
int |
是 |
设置日志等级,支持以下几种类型: CloudPushService.LOG_ERROR CloudPushService.LOG_INFO CloudPushService.LOG_DEBUG CloudPushService.LOG_OFF:关闭Log |
代码示例
PushServiceFactory.getCloudPushService().setLogLevel(CloudPushService.LOG_INFO);PushServiceFactory.getCloudPushService().setLogLevel(CloudPushService.LOG_INFO)打开推送通道
打开推送,推送默认就是打开状态,一般与关闭推送通道配套使用,只有关闭过才需要打开推送。
-
SDK版本V3.0.3及以上版本支持。
-
用于在程序运行时动态打开推送通道,全量推送场景下,打开推送通道需要24小时生效,其他场景实时生效。
turnOnPushChannel
接口定义
void turnOnPushChannel(CommonCallback callback)
所属类
CloudPushService
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
callback |
CommonCallback |
是 |
操作成功与否的回调 |
代码示例
PushServiceFactory.getCloudPushService().turnOnPushChannel(new CommonCallback() {
@Override
public void onSuccess(String s) {
}
@Override
public void onFailed(String errorCode, String errorMsg) {
}
});PushServiceFactory.getCloudPushService().turnOnPushChannel(object : CommonCallback() {
override fun onSuccess(s: String?) {}
override fun onFailed(errorCode: String?, errorMsg: String?) {}
})关闭推送通道
关闭推送,注意此时并不是真正断开推送通道,而是告诉服务这个设备不接收推送了。关闭之后,即使重新初始化SDK也不会打开推送,需要主动调用打开推送,才会重新接收推送。
-
SDK版本V3.0.3及以上版本支持。
-
用于在程序运行时动态关闭推送通道,全量推送场景下,关闭推送通道需要24小时生效,其他场景实时生效。
turnOffPushChannel
接口定义
void turnOffPushChannel(CommonCallback callback)
所属类
CloudPushService
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
callback |
CommonCallback |
是 |
操作成功与否的回调 |
代码示例
PushServiceFactory.getCloudPushService().turnOffPushChannel(new CommonCallback() {
@Override
public void onSuccess(String s) {
}
@Override
public void onFailed(String errorCode, String errorMsg) {
}
});PushServiceFactory.getCloudPushService().turnOffPushChannel(object : CommonCallback() {
override fun onSuccess(s: String?) {}
override fun onFailed(errorCode: String?, errorMsg: String?) {}
})查询推送通道状态
判断当前推送打开关闭状态。
checkPushChannelStatus
接口定义
void checkPushChannelStatus(CommonCallback callback)
所属类
CloudPushService
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
callback |
CommonCallback |
是 |
查询结果回调,response为on表示推送通道打开,off表示推送通道关闭 |
示例代码
PushServiceFactory.getCloudPushService().checkPushChannelStatus(new CommonCallback() {
@Override
public void onSuccess(String response) {
if (response.equals("on")) {
// 当前是打开状态
} else {
// 当前是关闭状态
}
}
@Override
public void onFailed(String errorCode, String errorMsg) {
}
});PushServiceFactory.getCloudPushService().checkPushChannelStatus(object : CommonCallback {
override fun onSuccess(response: String) {
if (response == "on") {
// 当前是打开状态
} else {
// 当前是关闭状态
}
}
override fun onFailed(errorCode: String, errorMsg: String) {}
})设置消息接收IntentService
设置接收推送的服务,服务需要继承AliyunMessageIntentService。默认使用广播的方式接收推送,设置之后会改为使用服务接收推送。
-
SDK版本V3.0.10及以上版本支持。
-
通过IntentService组件接收消息回调。
-
设置后消息将通过该组件处理,不再通过MessageReceiver处理。
setPushIntentService
接口定义
void setPushIntentService(Class messageIntentService)
所属类
CloudPushService
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
messageIntentService |
Class |
否 |
自定义接收消息IntentService的class,继承AliyunMessageIntentService。 null表示使用广播接收推送 |
代码示例
PushServiceFactory.getCloudPushService()
.setPushIntentService(MyMessageIntentService.class);PushServiceFactory.getCloudPushService()
.setPushIntentService(MyMessageIntentService.class)接收SDK日志输出
本节描述 3.x 的日志配置与监听方式。4.x 的统一日志 TAG、初始化后的日志等级设置顺序和监听范围请参见基础配置接口(4.x)。
将日志写入文件或上传时,建议排除 debug 和 info 级别日志,并控制日志量,以减少对应用性能的影响。
注册日志接口,用于接收SDK日志信息,排查问题。
addListener
接口定义
static void addListener(final LoggerListener lisn)
所属类
AmsLogger
代码示例
AmsLogger.addListener(new LoggerListener() {
@Override
public void d(String TAG, String msg, Throwable tr, int flag) {
}
@Override
public void i(String TAG, String msg, Throwable tr, int flag) {
}
@Override
public void w(String TAG, String msg, Throwable tr, int flag) {
}
@Override
public void e(String TAG, String msg, Throwable tr, int flag) {
}
});AmsLogger.addListener(object : LoggerListener {
override fun d(TAG: String, msg: String, tr: Throwable, flag: Int) {}
override fun i(TAG: String, msg: String, tr: Throwable, flag: Int) {}
override fun w(TAG: String, msg: String, tr: Throwable, flag: Int) {}
override fun e(TAG: String, msg: String, tr: Throwable, flag: Int) {}
})