1.0.0
该门户网站正在开发中.
实现示例
请参阅示例应用程序,了解如何正确集成用于处理 push 通知的包:https://gitflic.ru/project/rustore/flutter-rustore-push
推送通知的运行条件
为了推送通知的运行,必须遵守以下条件:
- 用户的设备上必须安装 RuStore 应用程序。
- RuStore 应用程序必须支持推送通知的功能。
- RuStore 应用程序应允许在后台模式下运行。
- 用户必须在 RuStore 应用程序中获得授权。
- 应用程序的签名指纹必须与 RuStore 控制台中添加的指纹匹配。
在项目中集成
要将包集成到项目,请执行以下命令:
flutter pub add flutter_rustore_push
个命令将在 pubspec.yaml 文件中添加一个字符串。
dependencies: \`\`flutter_rustore_push: \^1.0.0
初始化
为了初始化推送通知服务,请在您的 android 项目的 values 中添加值:
\<resources\> \`\`\<string name= \"flutter_rustore_push_project\" translatable= \"false\" \>xxx\</string\>\</resources\>
xxx - 这是项目标识符。在 RuStore 控制台系统中,该字段被称为"项目 ID",位于"推送通知 -> 项目"部分。
要启动推送通知服务,需要添加一个从 FlutterRustoreApplication 继承的 Application 类。
在 Kotlin 中这样做的例子:
package ru.rustore.flutter_rustore_push_exampleimport ru.rustore.flutter_rustore_push.FlutterRustoreApplicationopen class Application: FlutterRustoreApplication() {}
AndroidManifest.xml 中需要指定这个类:
\<application \`\`android:label= \"flutter_rustore_push_example\" \`\`android:name= \".Application\" \`\`android:icon= \"@mipmap/ic_launcher\" \> \`\`// \... \`\`\</application\>
设置ProGuard
要配置 ProGuard,请添加以下规则:
android/app/build.gradle 文件中添加:
```JavaScript
buildTypes { \`\`release { \`\`// \... \`\`proguardFiles getDefaultProguardFile( \'proguard-android.txt\' ), \'proguard-rules.pro\' \`\`} \`\`// \...}
检查获得推送通知的可能性
为了推送通知的运行,必须遵守几个条件:
- 用户的设备上必须安装 RuStore。
- RuStore 必须支持推送通知功能。
- RuStore 应用程序应允许在后台模式下运行。
- 用户必须在 RuStore 中获得授权。
检查上述条件,可以使用 RustorePushClient.available() 方法:
RustorePushClient.available().then((value) { \`\`print( \"available success: \${value}\" );}, onError: (err) { \`\`print( \"available error: \${err}\" );});
用于处理推送令牌和推送通知的方法
获取用户的推送令牌
在初始化库之后,您可以使用 RustorePushClient.getToken() 方法来获取用户当前的 push 令牌。
如果用户没有推送令牌,则该方法将创建并返回新的推送令牌。
RustorePushClient.getToken().then((value) { \`\`print( \"get token success: \${value}\" );}, onError: (err) { \`\`print( \"get token error: \${err}\" );})
删除用户的推送令牌
您可以使用 RustorePushClient.deleteToken() 方法来删除用户当前的 push 令牌。
RustorePushClient.deleteToken().then(() { \`\`print( \"delete success:\" );}, onError: (err) { \`\`print( \"delete error: \${err}\" );})
令牌更改事件
旧令牌可能会定期失效。可以重新发放令牌。为了了解是否生成了新的令牌,需要使用回调函数 RustorePushClient.onNewToken()。
RustorePushClient.onNewToken((value) { \`\`print( \"on new token success: \${value}\" );}, error: (err) { \`\`print( \"on new token err: \${err}\" );});
处理推送通知
要从推送通知中获取信息,需要添加回调函数 RustorePushClient.onMessageReceived()。
RustorePushClient.onMessageReceived((value) { \`\`print( \"on message received success: id=\${value.messageId}, data=\${value.data}, notification.body: \${value.notification?.body}\" );}, error: (err) { \`\`print( \"on message received error: \${err}\" );});
删除推送通知
要删除推送通知,需要添加回调函数 RustorePushClient.onDeletedMessages()。
RustorePushClient.onDeletedMessages(() { \`\`print( \"deleted messages\" );}, error: (err) { \`\`print( \"on message received error: \${err}\" );});
错误处理
要处理错误,需要使用回调函数 RustorePushClient.onError()。
RustorePushClient.onError((err) { \`\`print( \"on error: \${err}\" );});
通知结构体
通知结构体
class Message { \`\`String? messageId; \`\`int priority; \`\`int ttl; \`\`String? collapseKey; \`\`Map\<String?, String?\> data; \`\`Notification? notification;}
-
messageId
- 消息的唯一ID。它是每个消息的标识符; -
priority
- 返回优先级值。目前有以下选项:0
-UNKNOWN
.
| - 1
- HIGH
.
| - 2
- NORMAL
.
ttl
- Int类型的推送通知生命周期(以秒为单位);collapseKey
- 通知组的标识符;data
- 一个字典,在其中可以传递额外的通知数据;notification
- 通知对象。
结构体Notification
class Notification { \`\`String? title; \`\`String? body; \`\`String? channelId; \`\`String? imageUrl; \`\`String? color; \`\`String? icon; \`\`String? clickAction;}
-
title
- 通知的标题; -
body
- 通知的正文; -
channelId
- 可以指定的渠道,用于发送通知(适用于Android 8.0及更高版本); -
imageUrl
- 插入通知中的图片的直接链接(图片大小不超过1兆字节); -
color
- 通知的颜色(Notification.color)。颜色需要以十六进制格式传递,为字符串格式(例如:"#A52A2A"); -
icon
- 通知的图标。图标应存放在应用程序的资源中(res/drawable)。参数的值是一个字符串,与资源的名称匹配:- 在res/drawable中有一个名为small_icon.xml的图标,在代码中通过R.drawable.small_icon访问。为了在通知中显示此图标,服务器需要在"icon"参数中设置值"small_icon"。
-
clickAction
- intent点击通知时,用于打开活动的intent action。