flutter_aliyun_push.dart 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 Future<String> get platformVersion async {
  10. final String version = await _channel.invokeMethod('getPlatformVersion');
  11. return version;
  12. }
  13. static Future<String> get initPush async {
  14. final String version = await _channel.invokeMethod('initPush');
  15. return version;
  16. }
  17. /**
  18. * 注册原生调用dart
  19. */
  20. static void registCallHandler() {
  21. if(registCallback) {
  22. return;
  23. }
  24. registCallback = true;
  25. _channel.setMethodCallHandler((call) {
  26. print("setMethodCallHandler:"+call.method);
  27. switch(call.method) {
  28. case "onPushRegistSuccess":
  29. if(onRegistSuccess != null) {
  30. onRegistSuccess(call.arguments);
  31. }
  32. break;
  33. case "onPushRegistError":
  34. if(onRegistError != null) {
  35. onRegistError(call.arguments);
  36. }
  37. break;
  38. }
  39. });
  40. }
  41. static void reigistOnRegistSuccess(Function callback) {
  42. onRegistSuccess = callback;
  43. registCallHandler();
  44. }
  45. static void reigistOnRegistError(Function callback) {
  46. onRegistError = callback;
  47. registCallHandler();
  48. }
  49. }