You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
7.2 KiB

import 'dart:async';
import 'package:flutter/services.dart';
import 'dart:io' show Platform, stdout;
typedef Future<dynamic> EventHandler(Map<String, dynamic> event);
class JPush {
static const MethodChannel _channel =
const MethodChannel('jpush');
static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
static EventHandler _onReceiveNotification;
static EventHandler _onOpenNotification;
static EventHandler _onReceiveMessage;
// <String, dynamic>{
// 'source': source.index,
// 'maxWidth': maxWidth,
// 'maxHeight': maxHeight,
// }
/**
*
*/
static void setup({
String appKey,
String channel,
bool production,
}) {
_channel.invokeMethod('setup', { 'appKey': appKey, 'channel': channel, 'production': production });
}
/**
* JPush ()
*/
static void addEventHandler({
EventHandler onReceiveNotification,
EventHandler onOpenNotification,
EventHandler onReceiveMessage,
}) {
_onReceiveNotification = onReceiveNotification;
_onOpenNotification = onOpenNotification;
_onReceiveMessage = onReceiveMessage;
_channel.setMethodCallHandler(_handleMethod);
}
static Future<Null> _handleMethod(MethodCall call) async {
switch (call.method) {
case "onReceiveNotification":
return _onReceiveNotification(call.arguments.cast<String, dynamic>());
case "onOpenNotification":
return _onOpenNotification(call.arguments.cast<String, dynamic>());
case "onReceiveMessage":
return _onReceiveMessage(call.arguments.cast<String, dynamic>());
default:
throw new UnsupportedError("Unrecognized Event");
}
}
/**
*
*/
static Future applyPushAuthority([NotificationSettingsIOS iosSettings = const NotificationSettingsIOS()]) {
// TODO: 样式
// await _channel.invokeMethod('applyPushAuthority');
if (!Platform.isIOS) {
return;
}
_channel.invokeMethod('applyPushAuthority', iosSettings.toMap());
}
/**
* Tag tags
*
* @param {Array} params = [String]
* @param {Function} success = ({"tags":[String]}) => { }
* @param {Function} fail = ({"errorCode":int}) => { }
*/
static Future<Map<dynamic, dynamic>> setTags(List<String> tags) async {
final Map<dynamic, dynamic> result = await _channel.invokeMethod('setTags', tags);
return result;
}
/**
* tags
*
* @param {Function} success = ({"tags":[String]}) => { }
* @param {Function} fail = ({"errorCode":int}) => { }
*/
static Future<Map<dynamic, dynamic>> cleanTags() async {
final Map<dynamic, dynamic> result = await _channel.invokeMethod('cleanTags');
return result;
}
/**
* tags tags
*
* @param {Array} tags = [String]
* @param {Function} success = ({"tags":[String]}) => { }
* @param {Function} fail = ({"errorCode":int}) => { }
*/
static Future<Map<dynamic, dynamic>> addTags(List<String> tags) async {
final Map<dynamic, dynamic> result = await _channel.invokeMethod('addTags', tags);
return result;
}
/**
* tags
*
* @param {Array} tags = [String]
* @param {Function} success = ({"tags":[String]}) => { }
* @param {Function} fail = ({"errorCode":int}) => { }
*/
static Future<Map<dynamic, dynamic>> deleteTags(List<String> tags) async {
final Map<dynamic, dynamic> result = await _channel.invokeMethod('deleteTags', tags);
return result;
}
/**
* tags
*
* @param {Function} success = ({"tags":[String]}) => { }
* @param {Function} fail = ({"errorCode":int}) => { }
*/
static Future<Map<dynamic, dynamic>> getAllTags() async {
final Map<dynamic, dynamic> result = await _channel.invokeMethod('getAllTags');
return result;
}
/**
* alias.
*
* @param {String} alias
*
* @param {Function} success = ({"alias":String}) => { }
* @param {Function} fail = ({"errorCode":int}) => { }
*/
static Future<Map<dynamic, dynamic>> setAlias(String alias) async {
final Map<dynamic, dynamic> result = await _channel.invokeMethod('setAlias', alias);
return result;
}
/**
* alias
*
* @param {Function} success = ({"alias":String}) => { }
* @param {Function} fail = ({"errorCode":int}) => { }
*/
static Future<Map<dynamic, dynamic>> deleteAlias() async {
final Map<dynamic, dynamic> result = await _channel.invokeMethod('deleteAlias');
return result;
}
/**
* iOS Only
* Badge
*
* @param {Int} badge
*/
static Future setBadge(int badge) async {
await _channel.invokeMethod('setBadge', badge);
}
/**
* resumePush
*/
static Future stopPush() async {
await _channel.invokeMethod('stopPush');
}
/**
*
*/
static Future resumePush() async {
await _channel.invokeMethod('resumePush');
}
/**
*
*/
static Future clearAllNotifications() async {
await _channel.invokeMethod('clearAllNotifications');
}
/**
* iOS Only
* notification notification
* notification remoteNotification localNotification
* icon notification @{}
* @param {Function} callback = (Object) => {}
*/
static Future<Map<dynamic, dynamic>> getLaunchAppNotification() async {
final Map<dynamic, dynamic> result = await _channel.invokeMethod('getLaunchAppNotification');
return result;
}
/**
* RegistrationId, JPush RegistrationId
*
* * @param {Function} callback = (String) => {}
*/
static Future<String> getRegistrationID() async {
final String rid = await _channel.invokeMethod('getRegistrationID');
return rid;
}
}
class NotificationSettingsIOS {
final bool sound;
final bool alert;
final bool badge;
const NotificationSettingsIOS ({
this.sound = true,
this.alert = true,
this.badge = true,
});
Map<String, dynamic> toMap() {
return <String, bool>{'sound': sound, 'alert': alert, 'badge': badge};
}
}