flutter_aliyun_push.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:flutter/services.dart';
  5. import 'push_message.dart';
  6. typedef OnReceiveMessage = Function(PushMessage);
  7. typedef OnReceiveNotification = Function(PushNotification);
  8. typedef ApiCallback = Function(bool,Object);
  9. class FlutterAliyunPush {
  10. static const MethodChannel _channel =
  11. const MethodChannel('aliyun_push');
  12. static const EventChannel eventChannel = EventChannel("App/Event/Channel", const StandardMethodCodec());
  13. static bool registCallback = false;
  14. static Function onRegistSuccess;
  15. static Function onRegistError;
  16. static OnReceiveNotification onReceiveNotification;
  17. static OnReceiveMessage onReceiveMessage;
  18. static Future<String> get platformVersion async {
  19. final String version = await _channel.invokeMethod('getPlatformVersion');
  20. return version;
  21. }
  22. /**
  23. * 注册原生调用dart
  24. */
  25. static void registCallHandler() {
  26. if(registCallback) {
  27. return;
  28. }
  29. print("registCallHandler---------------");
  30. registCallback = true;
  31. _channel.setMethodCallHandler((call) {
  32. print("setMethodCallHandler:"+call.method);
  33. switch(call.method) {
  34. case "onPushRegistSuccess":
  35. if(onRegistSuccess != null) {
  36. onRegistSuccess(call.arguments);
  37. }
  38. break;
  39. case "onPushRegistError":
  40. if(onRegistError != null) {
  41. onRegistError(call.arguments);
  42. }
  43. break;
  44. case "onReceiverNotification":
  45. if(onReceiveNotification != null) {
  46. var param = call.arguments;
  47. if(param != null) {
  48. if(Platform.isIOS) {
  49. if(param['aps'] != null && param['aps']['alert'] != null) {
  50. var content = param['aps']['alert'];
  51. var title = content['title'];
  52. String body = content['body'];
  53. param = PushNotification(title,body,param);
  54. }
  55. }else {
  56. param = PushNotification.fromJson(json.decode(param));
  57. }
  58. }
  59. onReceiveNotification(param);
  60. }
  61. break;
  62. case "onReceiverMessage":
  63. if(onReceiveMessage != null) {
  64. var param = call.arguments;
  65. if(param != null) {
  66. param = PushMessage.fromJson(json.decode(param));
  67. }
  68. onReceiveMessage(param);
  69. }
  70. break;
  71. }
  72. });
  73. //告诉原生已经有监听了
  74. if(Platform.isAndroid) {
  75. _channel.invokeMethod('listened');
  76. }
  77. }
  78. static void reigistOnRegistSuccess(Function callback) {
  79. onRegistSuccess = callback;
  80. registCallHandler();
  81. }
  82. static void reigistOnRegistError(Function callback) {
  83. onRegistError = callback;
  84. registCallHandler();
  85. }
  86. static void reigistOnReceiveNotification(OnReceiveNotification callback) {
  87. onReceiveNotification = callback;
  88. registCallHandler();
  89. }
  90. static void reigistOnReceiveMessage(OnReceiveMessage callback) {
  91. onReceiveMessage = callback;
  92. registCallHandler();
  93. }
  94. //api 相关接口
  95. //绑定账号
  96. static void bindAccount(String account, ApiCallback callback) {
  97. _channel.invokeMethod("bindAccount",account).then((value) => {
  98. callback(true,value)
  99. }).catchError((e)=>{
  100. callback(false,e)
  101. });
  102. }
  103. //解绑账号
  104. static void unbindAccount(ApiCallback callback) {
  105. _channel.invokeMethod("unbindAccount").then((value) => {
  106. callback(true,value)
  107. }).catchError((e)=>{
  108. callback(false,e)
  109. });
  110. }
  111. //绑定标签
  112. static void bindTag(int target, List<String> tags, String alias, ApiCallback callback) {
  113. var params = {target:target,tags:tags,alias:alias};
  114. _channel.invokeMethod("bindTag",params).then((value) => {
  115. callback(true,value)
  116. }).catchError((e)=>{
  117. callback(false,e)
  118. });
  119. }
  120. //解绑标签
  121. static void unbindTag(int target, List<String> tags, String alias, ApiCallback callback) {
  122. var params = {target:target,tags:tags,alias:alias};
  123. _channel.invokeMethod("unbindTag",params).then((value) => {
  124. callback(true,value)
  125. }).catchError((e)=>{
  126. callback(false,e)
  127. });
  128. }
  129. //查询标签
  130. static void listTags(int target, ApiCallback callback) {
  131. _channel.invokeMethod("listTags",target).then((value) => {
  132. callback(true,value)
  133. }).catchError((e)=>{
  134. callback(false,e)
  135. });
  136. }
  137. //添加别名
  138. static void addAlias(String alias, ApiCallback callback) {
  139. _channel.invokeMethod("addAlias",alias).then((value) => {
  140. callback(true,value)
  141. }).catchError((e)=>{
  142. callback(false,e)
  143. });
  144. }
  145. //删除设备别名。
  146. static void removeAlias(String alias, ApiCallback callback) {
  147. _channel.invokeMethod("removeAlias",alias).then((value) => {
  148. callback(true,value)
  149. }).catchError((e)=>{
  150. callback(false,e)
  151. });
  152. }
  153. //查询设备别名
  154. static void listAliases(ApiCallback callback) {
  155. _channel.invokeMethod("listAliases").then((value) => {
  156. callback(true,value)
  157. }).catchError((e)=>{
  158. callback(false,e)
  159. });
  160. }
  161. }