flutter_aliyun_push.dart 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import 'dart:async';
  2. import 'package:flutter/services.dart';
  3. class FlutterAliyunPush {
  4. static const MethodChannel _channel =
  5. const MethodChannel('aliyun_push');
  6. static bool registCallback = false;
  7. static Function onRegistSuccess;
  8. static Function onRegistError;
  9. static Function onReceiveNotification;
  10. static Function onReceiveMessage;
  11. static Future<String> get platformVersion async {
  12. final String version = await _channel.invokeMethod('getPlatformVersion');
  13. return version;
  14. }
  15. static Future<String> get initPush async {
  16. final String version = await _channel.invokeMethod('initPush');
  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. onReceiveNotification(call.arguments);
  43. }
  44. break;
  45. case "onReceiverMessage":
  46. if(onReceiveMessage != null) {
  47. onReceiveMessage(call.arguments);
  48. }
  49. break;
  50. }
  51. });
  52. }
  53. static void reigistOnRegistSuccess(Function callback) {
  54. onRegistSuccess = callback;
  55. registCallHandler();
  56. }
  57. static void reigistOnRegistError(Function callback) {
  58. onRegistError = callback;
  59. registCallHandler();
  60. }
  61. static void reigistOnReceiveNotification(Function callback) {
  62. onReceiveNotification = callback;
  63. registCallHandler();
  64. }
  65. static void reigistOnReceiveMessage(Function callback) {
  66. onReceiveMessage = callback;
  67. registCallHandler();
  68. }
  69. }