flutter_plugin_mipush.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import 'dart:async';
  2. import 'package:flutter/services.dart';
  3. class FlutterPluginMipush {
  4. static const MethodChannel _channel = MethodChannel('flutter_plugin_mipush');
  5. static const EventChannel _event = EventChannel('flutter_plugin_mipush_event');
  6. static const EventChannel _state = EventChannel('flutter_plugin_mipush_state');
  7. static Stream<String> get stream => _event
  8. .receiveBroadcastStream()
  9. .map((event) => event as String);
  10. static Stream<String> get state => _state
  11. .receiveBroadcastStream()
  12. .map((event) => event as String);
  13. /// 初始化,如果没有appId或appKey,请登录小米开发者中心申请
  14. /// [appId] 应用appId
  15. /// [appKey] 应用appKey
  16. static Future<bool> init({
  17. required String appId,
  18. required String appKey,
  19. }) async {
  20. return await _channel.invokeMethod('init', {
  21. "appId": appId,
  22. "appKey": appKey,
  23. });
  24. }
  25. /// 设置别名
  26. /// [alias] 别名
  27. /// [category] 分类
  28. static Future<bool> setAlias({
  29. required String alias,
  30. required String category,
  31. }) async {
  32. return await _channel.invokeMethod('setAlias', {
  33. "alias": alias,
  34. "category": category,
  35. });
  36. }
  37. /// 撤销别名
  38. /// [alias] 别名
  39. /// [category] 分类
  40. static Future<void> unsetAlias({
  41. required String alias,
  42. required String category,
  43. }) async {
  44. return await _channel.invokeMethod('unsetAlias', {
  45. "alias": alias,
  46. "category": category,
  47. });
  48. }
  49. /// 获得所有别名
  50. static Future<List<String>> getAllAlias() async {
  51. return (await _channel.invokeMethod('getAllAlias')).cast<String>();
  52. }
  53. /// 设置用户账户
  54. /// [userAccount] 用户账户
  55. /// [category] 分类
  56. static Future<void> setUserAccount({
  57. required String userAccount,
  58. required String category,
  59. }) async {
  60. return await _channel.invokeMethod('setUserAccount', {
  61. "userAccount": userAccount,
  62. "category": category,
  63. });
  64. }
  65. /// 撤销设置用户账户
  66. /// [userAccount] 用户账户
  67. /// [category] 分类
  68. static Future<void> unsetUserAccount({
  69. required String userAccount,
  70. required String category,
  71. }) async {
  72. return await _channel.invokeMethod('unsetUserAccount', {
  73. "userAccount": userAccount,
  74. "category": category,
  75. });
  76. }
  77. /// 获得所有用户账户
  78. static Future<List<String>> getAllUserAccount() async {
  79. return (await _channel.invokeMethod('getAllUserAccount')).cast<String>();
  80. }
  81. /// 设置标签
  82. /// [topic] 标签
  83. /// [category] 分类
  84. static Future<void> subscribe({
  85. required String topic,
  86. required String category,
  87. }) async {
  88. return await _channel.invokeMethod('subscribe', {
  89. "topic": topic,
  90. "category": category,
  91. });
  92. }
  93. /// 撤销设置标签
  94. /// [topic] 标签
  95. /// [category] 分类
  96. static Future<void> unsubscribe({
  97. required String topic,
  98. required String category,
  99. }) async {
  100. return await _channel.invokeMethod('unsubscribe', {
  101. "topic": topic,
  102. "category": category,
  103. });
  104. }
  105. /// 获得所有标签
  106. static Future<List<String>> getAllTopic() async {
  107. return (await _channel.invokeMethod('getAllTopic')).cast<String>();
  108. }
  109. /// 获取客户端的 RegId
  110. static Future<String?> getRegId() async {
  111. return await _channel.invokeMethod('getRegId');
  112. }
  113. }