flutter_aliyun_push.dart 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:flutter/services.dart';
  4. import 'push_message.dart';
  5. typedef OnReceiveMessage = Function(PushMessage);
  6. typedef OnReceiveNotification = Function(PushNotification);
  7. class FlutterAliyunPush {
  8. static const MethodChannel _channel =
  9. const MethodChannel('aliyun_push');
  10. static bool registCallback = false;
  11. static Function onRegistSuccess;
  12. static Function onRegistError;
  13. static OnReceiveNotification onReceiveNotification;
  14. static OnReceiveMessage onReceiveMessage;
  15. static Future<String> get platformVersion async {
  16. final String version = await _channel.invokeMethod('getPlatformVersion');
  17. return version;
  18. }
  19. static Future<String> get initPush async {
  20. final String version = await _channel.invokeMethod('initPush');
  21. return version;
  22. }
  23. /**
  24. * 注册原生调用dart
  25. */
  26. static void registCallHandler() {
  27. if(registCallback) {
  28. return;
  29. }
  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. param = PushNotification.fromJson(json.decode(param));
  49. }
  50. onReceiveNotification(param);
  51. }
  52. break;
  53. case "onReceiverMessage":
  54. if(onReceiveMessage != null) {
  55. var param = call.arguments;
  56. if(param != null) {
  57. param = PushMessage.fromJson(json.decode(param));
  58. }
  59. onReceiveMessage(param);
  60. }
  61. break;
  62. }
  63. });
  64. }
  65. static void reigistOnRegistSuccess(Function callback) {
  66. onRegistSuccess = callback;
  67. registCallHandler();
  68. }
  69. static void reigistOnRegistError(Function callback) {
  70. onRegistError = callback;
  71. registCallHandler();
  72. }
  73. static void reigistOnReceiveNotification(OnReceiveNotification callback) {
  74. onReceiveNotification = callback;
  75. registCallHandler();
  76. }
  77. static void reigistOnReceiveMessage(OnReceiveMessage callback) {
  78. onReceiveMessage = callback;
  79. registCallHandler();
  80. }
  81. }