flutter_aliyun_push.dart 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. /**
  20. * 注册原生调用dart
  21. */
  22. static void registCallHandler() {
  23. if(registCallback) {
  24. return;
  25. }
  26. registCallback = true;
  27. _channel.setMethodCallHandler((call) {
  28. print("setMethodCallHandler:"+call.method);
  29. switch(call.method) {
  30. case "onPushRegistSuccess":
  31. if(onRegistSuccess != null) {
  32. onRegistSuccess(call.arguments);
  33. }
  34. break;
  35. case "onPushRegistError":
  36. if(onRegistError != null) {
  37. onRegistError(call.arguments);
  38. }
  39. break;
  40. case "onReceiverNotification":
  41. if(onReceiveNotification != null) {
  42. var param = call.arguments;
  43. if(param != null) {
  44. param = PushNotification.fromJson(json.decode(param));
  45. }
  46. onReceiveNotification(param);
  47. }
  48. break;
  49. case "onReceiverMessage":
  50. if(onReceiveMessage != null) {
  51. var param = call.arguments;
  52. if(param != null) {
  53. param = PushMessage.fromJson(json.decode(param));
  54. }
  55. onReceiveMessage(param);
  56. }
  57. break;
  58. }
  59. });
  60. _channel.invokeMethod('initPush');
  61. }
  62. static void reigistOnRegistSuccess(Function callback) {
  63. onRegistSuccess = callback;
  64. registCallHandler();
  65. }
  66. static void reigistOnRegistError(Function callback) {
  67. onRegistError = callback;
  68. registCallHandler();
  69. }
  70. static void reigistOnReceiveNotification(OnReceiveNotification callback) {
  71. onReceiveNotification = callback;
  72. registCallHandler();
  73. }
  74. static void reigistOnReceiveMessage(OnReceiveMessage callback) {
  75. onReceiveMessage = callback;
  76. registCallHandler();
  77. }
  78. }