flutter_aliyun_push.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. class FlutterAliyunPush {
  9. static const MethodChannel _channel =
  10. const MethodChannel('aliyun_push');
  11. static const EventChannel eventChannel = EventChannel("App/Event/Channel", const StandardMethodCodec());
  12. static bool registCallback = false;
  13. static Function onRegistSuccess;
  14. static Function onRegistError;
  15. static OnReceiveNotification onReceiveNotification;
  16. static OnReceiveMessage onReceiveMessage;
  17. static Future<String> get platformVersion async {
  18. final String version = await _channel.invokeMethod('getPlatformVersion');
  19. return version;
  20. }
  21. /**
  22. * 注册原生调用dart
  23. */
  24. static void registCallHandler() {
  25. if(registCallback) {
  26. return;
  27. }
  28. print("registCallHandler---------------");
  29. registCallback = true;
  30. _channel.setMethodCallHandler((call) {
  31. print("setMethodCallHandler:"+call.method);
  32. switch(call.method) {
  33. case "onPushRegistSuccess":
  34. if(onRegistSuccess != null) {
  35. onRegistSuccess(call.arguments);
  36. }
  37. break;
  38. case "onPushRegistError":
  39. if(onRegistError != null) {
  40. onRegistError(call.arguments);
  41. }
  42. break;
  43. case "onReceiverNotification":
  44. if(onReceiveNotification != null) {
  45. var param = call.arguments;
  46. if(param != null) {
  47. if(Platform.isIOS) {
  48. if(param['aps'] != null && param['aps']['alert'] != null) {
  49. var content = param['aps']['alert'];
  50. var title = content['title'];
  51. String body = content['body'];
  52. param = PushNotification(title,body,param);
  53. }
  54. }else {
  55. param = PushNotification.fromJson(json.decode(param));
  56. }
  57. }
  58. onReceiveNotification(param);
  59. }
  60. break;
  61. case "onReceiverMessage":
  62. if(onReceiveMessage != null) {
  63. var param = call.arguments;
  64. if(param != null) {
  65. param = PushMessage.fromJson(json.decode(param));
  66. }
  67. onReceiveMessage(param);
  68. }
  69. break;
  70. }
  71. });
  72. //告诉原生已经有监听了
  73. if(Platform.isAndroid) {
  74. _channel.invokeMethod('listened');
  75. }
  76. }
  77. static void reigistOnRegistSuccess(Function callback) {
  78. onRegistSuccess = callback;
  79. registCallHandler();
  80. }
  81. static void reigistOnRegistError(Function callback) {
  82. onRegistError = callback;
  83. registCallHandler();
  84. }
  85. static void reigistOnReceiveNotification(OnReceiveNotification callback) {
  86. onReceiveNotification = callback;
  87. registCallHandler();
  88. }
  89. static void reigistOnReceiveMessage(OnReceiveMessage callback) {
  90. onReceiveMessage = callback;
  91. registCallHandler();
  92. }
  93. }