1.1.0
注意
该门户网站正在开发中。文档的完整版本请看这里.
- Kotlin
- Java
为了推送通知的运行,必须遵守以下条件:
- 用户的设备上必须安装 RuStore 应用程序。
- RuStore 应用程序必须支持推送通知的功能。
- RuStore 应用程序应允许在后台模式下运行。
- 用户必须在 RuStore 应用程序中获得授权。
- 应用程序的签名指纹必须与开发者控制台中添加的指纹匹配。
添加存储库
连接本地存储库:
repositories { \`\`maven { \`\`url = uri( \"https://artifactory-external.vkpartner.ru/artifactory/maven\" ) \`\`}}
集成依赖项
在您的配置文件中添加以下代码以连接依赖项:
dependencies { \`\`implementation( \"ru.rustore.sdk:pushclient:1.2.0\" )}\<br\>
编辑您的应用程序的清单
声明扩展 RuStoreMessagingService 的服务:
\< service \`\`android:name = \".MyRuStoreMessagingService\" \`\`android:exported = \"true\" \`\`tools:ignore = \"ExportedService\" \> \`\`\< intent-filter \> \`\`\< action android:name = \"ru.rustore.sdk.pushclient.MESSAGING_EVENT\" /\> \`\`\</ intent-filter \>\</ service \>
果您想更改标准 通知的图标或颜色,可以添加以下元数据:
\`\<\` \`meta-data\`\<br\>\` android:name="ru.rustore.sdk.pushclient.default_notification_icon"\<br\> android:resource\` \`=\` \`\"@drawable/ic_baseline_android_24\"\` \`/\>\`\<br\>\`\<\` \`meta-data\`\<br\>\` android:name="ru.rustore.sdk.pushclient.default_notification_color"\<br\> \`android:resource = \"@color/your_favorite_color\" /\> \<br\>
可以添加以下元数据以重写通知渠道:
\< meta-data \`\`android:name = \"ru.rustore.sdk.pushclient.default_notification_channel_id\" \`\`android:value = \"@string/pushes_notification_channel_id\" /\>
加推送通知渠道时,您必须自行创建该渠道。
初始化
要进行初始化,需要在RuStore 控制台系统中获得项目 ID。 为此,请转到应用程序页面的"推送通知"部分,然后选择"项目"。
要进行初始化,请在项目的 Application 中添加以下代码:
classApp: Application() { \`\`override fun onCreate() { \`\`super .onCreate() \`\`RuStorePushClient.init( \`\`application = this , \`\`projectId = \"i5UTx96jw6c1C9LvdlE4cdNrWHMNyRBt\" , \`\`logger = DefaultLogger() \`\`) \`\`}}
application
- Application 类的实例;projectId
- 您在 VKPNS 系统中项目的标识符;logger
- 日志器,默认使用 logcat 输出。
事件日志记录
如果要记录推送通知库事件,请在 RuStorePushClient.init 调用中添加 logger 参数(初始化时该参数为可选参数)。
为此, 您需要实现 Logger 接口:
接口Logger
interface Logger { \`\`fun verbose(message: String, throwable: Throwable? = null ) \`\`fun debug(message: String, throwable: Throwable? = null ) \`\`fun info(message: String, throwable: Throwable? = null ) \`\`fun warn(message: String, throwable: Throwable? = null ) \`\`fun error(message: String, throwable: Throwable? = null ) \`\`fun createLogger(tag: String): Logger}
果 Logger 没有被传递,将使用默认的实现,它使用 AndroidLog。
日志器实现示例
public class DefaultLogger( \`\`private val tag: String? = null ,) : Logger { \`\`override fun verbose(message: String, throwable: Throwable?) { \`\`Log.v(tag, message, throwable) \`\`} \`\`override fun debug(message: String, throwable: Throwable?) { \`\`Log.d(tag, message, throwable) \`\`} \`\`override fun info(message: String, throwable: Throwable?) { \`\`Log.i(tag, message, throwable) \`\`} \`\`override fun warn(message: String, throwable: Throwable?) { \`\`Log.w(tag, message, throwable) \`\`} \`\`override fun error(message: String, throwable: Throwable?) { \`\`Log.e(tag, message, throwable) \`\`} \`\`override fun createLogger(tag: String): Logger { \`\`val newTag = if ( this .tag != null ) { \`\`\"\${this.tag}:\$tag\" \`\`} else { \`\`tag \`\`} \`\`return DefaultLogger(newTag) \`\`}}
检查获得推送通知的可能性
关键词:RuStore,开发者,文档,RuStoreSDK,推送通知,检查获得推送通知。为了推送通知的运行,必须遵守几个条件:
- 用户的设备上必须安装 RuStore。
- RuStore 必须支持推送通知功能。
- RuStore 应用程序应允许在后台模式下运行。
- 用户必须在 RuStore 中获得授权。
了检查设备上是否安装了 RuStore,可以使用 RuStorePushClient.checkPushAvailability 方法:
RuStorePushClient.checkPushAvailability() \`\`.addOnCompleteListener(object : OnCompleteListener\<FeatureAvailabilityResult\> { \`\`override fun onSuccess(result: FeatureAvailabilityResult) { \`\`when (result) { \`\`FeatureAvailabilityResult.Available -\> { \`\`// Process push available \`\`} \`\`is FeatureAvailabilityResult.Unavailable -\> { \`\`result.cause.resolveForPush(requireContext()) \`\`} \`\`} \`\`} \`\`override fun onFailure(throwable: Throwable) { \`\`// Process error \`\`} \`\`})
使用推送令牌的方法
关键词:RuStore,开发者,文档,RuStoreSDK,推送通知,使用推送令牌,推送令牌#####。用户获取推送令牌
在库初始化之后,您可以使用 RuStorePushClient.getToken() 方法来获取当前用户的push令牌。
如果用户没有推送令牌,则该方法将创建并返回新的推送令牌。
RuStorePushClient.getToken().addOnCompleteListener(object : OnCompleteListener\<String?\> { \`\`override fun onFailure(throwable: Throwable) { \`\`// Process error \`\`} \`\`override fun onSuccess(result: String?) { \`\`// Process success \`\`}})