123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- import 'dart:async';
- import 'dart:convert';
- import 'dart:io';
- import 'package:flutter/services.dart';
- import 'push_message.dart';
- typedef OnReceiveMessage = Function(PushMessage);
- typedef OnReceiveNotification = Function(PushNotification);
- typedef ApiCallback = Function(bool,Object);
- class FlutterAliyunPush {
- static const MethodChannel _channel =
- const MethodChannel('aliyun_push');
- static const EventChannel eventChannel = EventChannel("App/Event/Channel", const StandardMethodCodec());
-
- 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;
- }
- /**
- * 注册原生调用dart
- */
- static void registCallHandler() {
- if(registCallback) {
- return;
- }
- print("registCallHandler---------------");
- 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) {
- if(Platform.isIOS) {
- if(param['aps'] != null && param['aps']['alert'] != null) {
- var content = param['aps']['alert'];
- var title = content['title'];
- String body = content['body'];
- param = PushNotification(title,body,param);
- }
- }else {
- 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;
- }
- });
- //告诉原生已经有监听了
- if(Platform.isAndroid) {
- _channel.invokeMethod('listened');
- }
- }
- 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();
- }
- //api 相关接口
- //绑定账号
- static void bindAccount(String account, ApiCallback callback) {
- _channel.invokeMethod("bindAccount",account).then((value) => {
- callback(true,value)
- }).catchError((e)=>{
- callback(false,e)
- });
- }
- //解绑账号
- static void unbindAccount(ApiCallback callback) {
- _channel.invokeMethod("unbindAccount").then((value) => {
- callback(true,value)
- }).catchError((e)=>{
- callback(false,e)
- });
- }
- //绑定标签
- static void bindTag(int target, List<String> tags, String alias, ApiCallback callback) {
- var params = {target:target,tags:tags,alias:alias};
- _channel.invokeMethod("bindTag",params).then((value) => {
- callback(true,value)
- }).catchError((e)=>{
- callback(false,e)
- });
- }
- //解绑标签
- static void unbindTag(int target, List<String> tags, String alias, ApiCallback callback) {
- var params = {target:target,tags:tags,alias:alias};
- _channel.invokeMethod("unbindTag",params).then((value) => {
- callback(true,value)
- }).catchError((e)=>{
- callback(false,e)
- });
- }
- //查询标签
- static void listTags(int target, ApiCallback callback) {
- _channel.invokeMethod("listTags",target).then((value) => {
- callback(true,value)
- }).catchError((e)=>{
- callback(false,e)
- });
- }
- //添加别名
- static void addAlias(String alias, ApiCallback callback) {
- _channel.invokeMethod("addAlias",alias).then((value) => {
- callback(true,value)
- }).catchError((e)=>{
- callback(false,e)
- });
- }
- //删除设备别名。
- static void removeAlias(String alias, ApiCallback callback) {
- _channel.invokeMethod("removeAlias",alias).then((value) => {
- callback(true,value)
- }).catchError((e)=>{
- callback(false,e)
- });
- }
- //查询设备别名
- static void listAliases(ApiCallback callback) {
- _channel.invokeMethod("listAliases").then((value) => {
- callback(true,value)
- }).catchError((e)=>{
- callback(false,e)
- });
- }
- }
|