
消息推送涉及五个关键环节:业务服务器、第三方推送服务、操作系统推送服务、用户终端和手机应用。iOS使用苹果推送服务(APNs),Android使用Firebase云消息传递(FCM)。国内Android设备通常采用第三方推送服务(如极光推送、友盟推送)以适配本地化需求。
第三方服务通过共享推送通道提升消息送达率,即使部分应用被系统终止仍能保持连接。同时简化了业务服务器的集成工作,仅需API调用即可完成推送,统一了Android和iOS的推送方案。
使用Flutter插件工程封装原生推送功能,独立于主工程便于维护。插件工程包含:
android和ios目录:平台相关代码实现example目录:调试插件的示例应用封装三个核心方法:
setup:初始化推送SDKregistrationID:获取设备标识setOpenNotificationHandler:注册消息回调class FlutterPushPlugin {
static final _instance = FlutterPushPlugin.private(const MethodChannel('flutter_push_plugin'));
final MethodChannel _channel;
EventHandler _onOpenNotification;
FlutterPushPlugin.private(this._channel) {
_channel.setMethodCallHandler(_handleMethod);
}
setupWithAppID(String appID) => _channel.invokeMethod("setup", appID);
setOpenNotificationHandler(EventHandler handler) => _onOpenNotification = handler;
Future<Null> _handleMethod(MethodCall call) {
if (call.method == "onOpenNotification") return _onOpenNotification(call.arguments);
throw UnsupportedError("Unrecognized Event");
}
Future<String> get registrationID async => await _channel.invokeMethod('getRegistrationID');
}依赖配置
在build.gradle添加极光SDK依赖:
dependencies {
implementation 'cn.jiguang.sdk:jpush:3.3.4'
implementation 'cn.jiguang.sdk:jcore:2.1.2'
}方法通道实现
setup:空实现(配置通过打包参数完成)getRegistrationID:调用极光SDK获取设备IDonOpenNotification:处理消息回调依赖配置 通过CocoaPods集成极光SDK:
pod 'JPush', '~> 3.0'方法通道实现
setup:初始化极光SDK并绑定AppKey通过example工程模拟推送场景,验证以下功能: