基础配置接口

更新时间:
复制 MD 格式

配置项

说明

配置Application

Application必须配置。

配置AppKey

配置AppKey,如果在AndroidManifest.xml中已经配置,这里可以不调用。

配置AppSecret

配置AppSecret,如果在AndroidManifest.xml中已经配置,这里可以不调用。

配置关闭channel进程

关闭channel进程。

关闭Channel进程心跳

关闭channel进程心跳。

定期循环启动Channel进程

开启Channel进程时,设置定期循环启动Channel进程。默认关闭循环启动。

配置定期循环启动Channel进程的时间间隔

配置定期循环启动Channel进程的时间间隔,只有在开启定期循环启动Channel进程时才会生效,默认间隔5分钟。

注册推送通道

注册推送通道,开始接收推送。可以根据需要延迟注册,比如需要用户签署完隐私政策

获取设备标识

获取设备唯一标识,指定设备推送时需要。

设置日志等级

在通道初始化之前设置日志等级,默认等级为CloudPushService.LOG_DEBUG。

打开推送通道

打开推送,推送默认就是打开状态,一般与关闭推送通道配套使用,只有关闭过才需要打开推送。

关闭推送通道

关闭推送,注意此时并不是真正断开推送通道,而是告诉服务这个设备不接收推送了。关闭之后,即使重新初始化SDK也不会打开推送,需要主动调用打开推送,才会重新接收推送。

查询推送通道状态

判断当前推送打开关闭状态。

设置消息接收IntentService

设置接收推送的服务,服务需要继承AliyunMessageIntentService。默认使用广播的方式接收推送,设置之后会改为使用服务接收推送。

接收SDK日志输出

注册日志接口,用于接收SDK日志信息,排查问题。

配置Application

配置application。

重要

application必须配置。

application

接口定义

PushInitConfig.Builder application(Application application)

所属类

PushInitConfig.Builder

参数说明

参数

类型

是否必填

说明

application

Application

是

Application

代码示例

val config = PushInitConfig.Builder()
    .application(application)
    .build()
PushInitConfig config = new PushInitConfig.Builder()
      	.application(application)
      	.build();

配置AppKey

配置AppKey,如果在AndroidManifest.xml中已经配置,这里可以不调用。

appKey

接口定义

PushInitConfig.Builder appKey(String appKey)

所属类

PushInitConfig.Builder

参数说明

参数

类型

是否必填

说明

appKey

String

是

EMAS的appKey

代码示例

val config = PushInitConfig.Builder()
    .appKey(appKey)
    .build()
PushInitConfig config = new PushInitConfig.Builder()
      	.appKey(appKey)
      	.build();

配置AppSecret

配置AppSecret,如果在AndroidManifest.xml中已经配置,这里可以不调用。

appSecret

接口定义

PushInitConfig.Builder appSecret(String appSecret)

所属类

PushInitConfig.Builder

参数说明

参数

类型

是否必填

说明

appSecret

String

是

EMAS的appSecret

代码示例

val config = PushInitConfig.Builder()
    .appSecret(appSecret)
    .build()
PushInitConfig config = new 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

是

  • true,关闭channel进程

  • false,开启channel进程

代码示例

val config = PushInitConfig.Builder()
    .disableChannelProcess(false)
    .build()
PushInitConfig config = new 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

是

  • true,关闭channel进程心跳

  • false,开启channel进程心跳

代码示例

val config = PushInitConfig.Builder()
    .disableChannelProcessHeartbeat(false)
    .build()
PushInitConfig config = new PushInitConfig.Builder()
      	.disableChannelProcessHeartbeat(false)
      	.build();

定期循环启动Channel进程

开启Channel进程时,设置定期循环启动Channel进程。默认关闭循环启动。

loopStartChannel

接口定义

PushInitConfig.Builder loopStartChannel(boolean enable)

所属类

PushInitConfig.Builder

参数说明

参数

类型

是否必填

说明

loopStartChannel

boolean

是

  • true,开启channel进程循环启动

  • false,关闭channel进程循环启动

代码示例

val config = PushInitConfig.Builder()
    .loopStartChannel(true)
    .build()
PushInitConfig config = new PushInitConfig.Builder()
      	.loopStartChannel(true)
      	.build();

配置定期循环启动Channel进程的时间间隔

配置定期循环启动Channel进程的时间间隔,只有在开启定期循环启动Channel进程时才会生效,默认间隔是5分钟。

loopInterval

接口定义

PushInitConfig.Builder loopInterval(long interval)

所属类

PushInitConfig.Builder

参数说明

参数

类型

是否必填

说明

loopInterval

long

是

循环时间间隔,单位时毫秒。

代码示例

val config = PushInitConfig.Builder()
    .loopInterval(5 * 60 * 1000)
    .build()
PushInitConfig config = new PushInitConfig.Builder()
      	.loopInterval(5 * 60 * 1000)
      	.build();

注册推送通道

注册推送通道,开始接收推送。可以根据需要延迟注册,比如需要用户签署完隐私政策

register

接口定义

void register(Context context, CommonCallback callback)

所属类

CloudPushService

参数说明

参数

类型

是否必填

说明

context

Context

是

应用上下文(需要ApplicationContext)

callback

CommonCallback

是

回调,错误码参见错误处理

代码示例

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")
    }
})
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);
    }
});

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

是

回调,错误码参见错误处理

获取设备标识

获取设备唯一标识,指定设备推送时需要。

getDeviceId

接口定义

String getDeviceId()

所属类

CloudPushService

返回说明

类型

说明

String

设备唯一标识

代码示例

val deviceId = PushServiceFactory.getCloudPushService().deviceId
String deviceId = PushServiceFactory.getCloudPushService().getDeviceId();

设置日志等级

在通道初始化之前设置日志等级,默认等级为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(object : CommonCallback() {
    override fun onSuccess(s: String?) {}
    override fun onFailed(errorCode: String?, errorMsg: String?) {}
})
PushServiceFactory.getCloudPushService().turnOnPushChannel(new CommonCallback() {
    @Override
    public void onSuccess(String s) {
        
    }

    @Override
    public void onFailed(String errorCode, String errorMsg) {
        
    }
});

关闭推送通道

关闭推送,注意此时并不是真正断开推送通道,而是告诉服务这个设备不接收推送了。关闭之后,即使重新初始化SDK也不会打开推送,需要主动调用打开推送,才会重新接收推送。

重要
  • SDK版本V3.0.3及以上版本支持。

  • 用于在程序运行时动态关闭推送通道,全量推送场景下,关闭推送通道需要24小时生效,其他场景实时生效。

turnOffPushChannel

接口定义

void turnOffPushChannel(CommonCallback callback)

所属类

CloudPushService

参数说明

参数

类型

是否必填

说明

callback

CommonCallback

是

操作成功与否的回调

代码示例

PushServiceFactory.getCloudPushService().turnOffPushChannel(object : CommonCallback() {
    override fun onSuccess(s: String?) {}
    override fun onFailed(errorCode: String?, errorMsg: String?) {}
})
PushServiceFactory.getCloudPushService().turnOffPushChannel(new CommonCallback() {
    @Override
    public void onSuccess(String s) {
        
    }

    @Override
    public void onFailed(String errorCode, String errorMsg) {
        
    }
});

查询推送通道状态

判断当前推送打开关闭状态。

checkPushChannelStatus

接口定义

void checkPushChannelStatus(CommonCallback callback)

所属类

CloudPushService

参数说明

参数

类型

是否必填

说明

callback

CommonCallback

是

查询结果回调,response为on表示推送通道打开,off表示推送通道关闭

示例代码

PushServiceFactory.getCloudPushService().checkPushChannelStatus(object : CommonCallback {
    override fun onSuccess(response: String) {
        if (response == "on") {
            // 当前是打开状态
        } else {
            // 当前是关闭状态
        }
    }

    override fun onFailed(errorCode: String, errorMsg: String) {}
})
PushServiceFactory.getCloudPushService().checkPushChannelStatus(new CommonCallback() {
    @Override
    public void onSuccess(String response) {
        if (response.equals("on")) {
            // 当前是打开状态
        } else {
            // 当前是关闭状态
        }
    }

    @Override
    public void onFailed(String errorCode, String errorMsg) {
        
    }
});

设置消息接收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日志输出

重要

如果要输出日志到文件或者上传,注意不要包含debug和info级别日志,避免日志量过大,影响应用性能

注册日志接口,用于接收SDK日志信息,排查问题。

addListener

接口定义

static void addListener(final LoggerListener lisn)

所属类

AmsLogger

代码示例

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) {}
})
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) {

	}
});