| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import 'dart:async';
- import 'dart:convert';
- import 'package:flutter/services.dart';
- import 'push_message.dart';
- typedef OnReceiveMessage = Function(PushMessage);
- typedef OnReceiveNotification = Function(PushNotification);
- class FlutterAliyunPush {
- static const MethodChannel _channel =
- const MethodChannel('aliyun_push');
- static bool registCallback = false;
- static Function onRegistSuccess;
- static Function onRegistError;
- static OnReceiveNotification onReceiveNotification;
- static OnReceiveMessage onReceiveMessage;
- static Future<String> get platformVersion async {
- final String version = await _channel.invokeMethod('getPlatformVersion');
- return version;
- }
- static Future<String> get initPush async {
- final String version = await _channel.invokeMethod('initPush');
- return version;
- }
- /**
- * 注册原生调用dart
- */
- static void registCallHandler() {
- if(registCallback) {
- return;
- }
- registCallback = true;
- _channel.setMethodCallHandler((call) {
- print("setMethodCallHandler:"+call.method);
- switch(call.method) {
- case "onPushRegistSuccess":
- if(onRegistSuccess != null) {
- onRegistSuccess(call.arguments);
- }
- break;
- case "onPushRegistError":
- if(onRegistError != null) {
- onRegistError(call.arguments);
- }
- break;
- case "onReceiverNotification":
- if(onReceiveNotification != null) {
- var param = call.arguments;
- if(param != null) {
- param = PushNotification.fromJson(json.decode(param));
- }
- onReceiveNotification(param);
- }
- break;
- case "onReceiverMessage":
- if(onReceiveMessage != null) {
- var param = call.arguments;
- if(param != null) {
- param = PushMessage.fromJson(json.decode(param));
- }
- onReceiveMessage(param);
- }
- break;
- }
- });
- }
- static void reigistOnRegistSuccess(Function callback) {
- onRegistSuccess = callback;
- registCallHandler();
- }
- static void reigistOnRegistError(Function callback) {
- onRegistError = callback;
- registCallHandler();
- }
- static void reigistOnReceiveNotification(OnReceiveNotification callback) {
- onReceiveNotification = callback;
- registCallHandler();
- }
- static void reigistOnReceiveMessage(OnReceiveMessage callback) {
- onReceiveMessage = callback;
- registCallHandler();
- }
- }
|