这是一个基于 MobPush 功能的扩展的 Flutter 插件。使用此插件能够帮助您在使用 Flutter 开发应用时,快速地实现推送功能。
在pubspec.yaml文件中加入下面依赖
- dependencies:
- mobcommonlib:
- mobpush_plugin:
然后执行:flutter packages get 导入package 在你的dart工程文件中,导入下面头文件,开始使用
- import 'package:mobcommonlib/mobcommonlib.dart';
- import 'package:mobpush_plugin/mobpush_plugin.dart';
平台配置参考 iOS集成文档
实现文档中 Xcode配置:配置AppKey和AppSecret
导入 MobPush 相关依赖
在项目根目录的build.gradle中添加以下代码:
- buildscript {
- repositories {
- // 配置Mob Maven库
- maven {
- url "https://mvn.mob.com/android"
- }
- // 配置HMS Core SDK的Maven仓地址。(集成华为厂商需要添加)
- maven {
- url 'https://developer.huawei.com/repo/'}
- }
- ...
- }
- dependencies {
- ...
- // 集成MobPush
- classpath "com.mob.sdk:MobSDK:2018.0319.1724"
- }
- }
在 /android/app/build.gradle 中添加以下代码:
- apply plugin: 'com.android.application'
- apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
- // 导入MobSDK
- apply plugin: 'com.mob.sdk'
平台相关集成 在项目的/android/app/build.gradle中添加:
- MobSDK {
- appKey "您的MobTech平台appKey"
- appSecret "您的MobTech平台appSecret"
-
- //配置MobPush
- MobPush {
- //配置厂商推送(可选配置,不需要厂商推送可不配置,需要哪些厂商推送只需配置哪些厂商配置即可)
- devInfo {
- //配置小米厂商推送
- XIAOMI {
- appId "您的小米平台appId"
- appKey "您的小米平台appKey"
- }
-
- //配置华为厂商推送
- HUAWEI {
- appId "您的华为平台appId"
- }
-
- //配置魅族厂商推送
- MEIZU {
- appId "您的魅族平台appId"
- appKey "您的魅族平台appKey"
- }
-
- //配置FCM厂商推送
- FCM {
- //设置默认推送通知显示图标
- iconRes "@mipmap/default_ic_launcher"
- }
-
- //配置OPPO厂商推送
- OPPO {
- appKey "您的OPPO平台appKey"
- appSecret "您的OPPO平台appSecret"
- }
-
- //配置VIVO厂商推送
- VIVO {
- appId "您的VIVO平台appId"
- appKey "您的VIVO平台appKey"
- }
- }
- }
- }
添加代码
在MainActivity的onCreate中添加以下代码:
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- GeneratedPluginRegistrant.registerWith(this);
- }
- /**
- * 回传用户隐私授权结果
- * @param status 用户是否同意隐私协议
- * @param result 默认传null
- */
- Mobcommonlib.submitPolicyGrantResult(bool status, Function(bool)? result)
例:
Mobcommonlib.submitPolicyGrantResult(true, null);
- setCustomNotification
-
- if (Platform.isIOS) {
- MobpushPlugin.setCustomNotification();
- }
- setAPNsForProduction
-
- if (Platform.isIOS) {
- // 开发环境 false, 线上环境 true
- MobpushPlugin.setAPNsForProduction(false)
- }
- addPushReceiver
-
- MobpushPlugin.addPushReceiver(_onEvent, _onError);
-
- void _onEvent(Object event) {
-
- }
-
- void _onError(Object event) {
-
- }
- stopPush
-
- MobpushPlugin.stopPush();
- restartPush
-
- MobpushPlugin.restartPush();
- isPushStopped
-
- MobpushPlugin.isPushStopped();
- setAlias
-
- MobpushPlugin.setAlias("别名").then((Map<String, dynamic> aliasMap){
- String res = aliasMap['res'];
- String error = aliasMap['error'];
- String errorCode = aliasMap['errorCode'];
- print(">>>>>>>>>>>>>>>>>>>>>>>>>>> setAlias -> res: $res error: $error");
- });
- getAlias
-
- MobpushPlugin.getAlias().then((Map<String, dynamic> aliasMap){
- String res = aliasMap['res'];
- String error = aliasMap['error'];
- print(">>>>>>>>>>>>>>>>>>>>>>>>>>> getAlias -> res: $res error: $error");
- });
- deleteAlias
-
- MobpushPlugin.deleteAlias().then((Map<String, dynamic> aliasMap){
- String res = aliasMap['res'];
- String error = aliasMap['error'];
- print(">>>>>>>>>>>>>>>>>>>>>>>>>>> deleteAlias -> res: $res error: $error");
- });
- addTags
-
- List tags = new List();
- tags.add("tag1");
- tags.add("tag2");
- MobpushPlugin.addTags(tags).then((Map<String, dynamic> tagsMap){
- String res = tagsMap['res'];
- String error = tagsMap['error'];
- print(">>>>>>>>>>>>>>>>>>>>>>>>>>> addTags -> res: $res error: $error");
- });
- getTags
-
- MobpushPlugin.getTags().then((Map<String, dynamic> tagsMap) {
- List<String> resList;
- if (tagsMap['res'] == null) {
- resList = [];
- } else {
- resList = tagsMap['res'].toList();
- }
- String error = tagsMap['error'];
-
- print(
- ">>>>>>>>>>>>>>>>>>>>>>>>>>> getTags -> res: $resList error: $error");
- });
- deleteTags
-
- List tags = new List();
- tags.add("tag1");
- tags.add("tag2");
- MobpushPlugin.deleteTags(tags).then((Map<String, dynamic> tagsMap){
- String res = tagsMap['res'];
- String error = tagsMap['error'];
- print(">>>>>>>>>>>>>>>>>>>>>>>>>>> deleteTags -> res: $res error: $error");
- });
- cleanTags
-
- MobpushPlugin.cleanTags().then((Map<String, dynamic> tagsMap){
- String res = tagsMap['res'];
- String error = tagsMap['error'];
- print(">>>>>>>>>>>>>>>>>>>>>>>>>>> cleanTags -> res: $res error: $error");
- });
- addLocalNotification
-
- MobpushPlugin.addLocalNotification();
- bindPhoneNum
-
- MobpushPlugin.bindPhoneNum("110");
- send
-
- /**
- * 测试模拟推送,用于测试
- * type:模拟消息类型,1、通知测试;2、内推测试;3、定时
- * content:模拟发送内容,500字节以内,UTF-8
- * space:仅对定时消息有效,单位分钟,默认1分钟
- * extras: 附加数据,json字符串
- */
- MobpushPlugin.send(int type, String content, int space, String extras).then((Map<String, dynamic> sendMap){
- String res = sendMap['res'];
- String error = sendMap['error'];
- print(">>>>>>>>>>>>>>>>>>>>>>>>>>> send -> res: $res error: $error");
- });
- setClickNotificationToLaunchMainActivity
-
- MobpushPlugin.setClickNotificationToLaunchMainActivity (bool enable);
- removeLocalNotification
-
- MobpushPlugin.removeLocalNotification(int notificationId);
- clearLocalNotifications
-
- MobpushPlugin.clearLocalNotifications();
- setNotifyIcon
-
- MobpushPlugin.setNotifyIcon(String resId);
- setSilenceTime
-
- /**
- * 设置通知静音时段(推送选项)(仅Android)
- * @param startHour 开始时间[0~23] (小时)
- * @param startMinute 开始时间[0~59](分钟)
- * @param endHour 结束时间[0~23](小时)
- * @param endMinute 结束时间[0~59](分钟)
- */
- MobpushPlugin.setSilenceTime(int startHour, int startMinute, int endHour, int endMinute)
- setBadge
-
- MobpushPlugin.setBadge(int badge);
- clearBadge
-
- MobpushPlugin.clearBadge();
- getRegistrationId
- MobpushPlugin.getRegistrationId().then((Map<String, dynamic> ridMap) {
- print(ridMap);
- String regId = ridMap['res'].toString();
- print('------>#### registrationId: ' + regId);
- });
由于插件更新,SDK的Pod依赖被替换,Flutter 本身写入Pod文件不会先执行删除原有依赖,导致可能会出现原有本地库依然存在,请检查Pod文件夹下文件,直接手动删除mob_pushsdk 以及 MOBFoundation文件即可,如有疑问,请直接通过官网和我们联系。
demo: GitHub地址
推送证书申请流程见:推送证书文档