共计 5828 个字符,预计需要花费 15 分钟才能阅读完成。
集成筹备
这是一个基于 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
平台配置参考 iOS 集成文档
实现文档中 Xcode 配置:配置 AppKey 和 AppSecret
Android
导入 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 onCrSDK APIeate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); | |
GeneratedPluginRegistrant.registerWith(this); | |
} |
SDK API
回传用户隐衷受权后果 (submitPrivacyGrantResult)
/** | |
* 回传用户隐衷受权后果 | |
* @param status 用户是否批准隐衷协定 | |
* @param result 默认传 null | |
*/ | |
Mobcommonlib.submitPolicyGrantResult(bool status, Function(bool)? result) |
例:
Mobcommonlib.submitPolicyGrantResult(true, null);
设置近程推送环境,向用户受权(setCustomNotification 仅 iOS)
setCustomNotification | |
if (Platform.isIOS) {MobpushPlugin.setCustomNotification(); | |
} |
设置近程推送环境 (setAPNsForProduction 仅 iOS)
setAPNsForProduction | |
if (Platform.isIOS) { | |
// 开发环境 false, 线上环境 true | |
MobpushPlugin.setAPNsForProduction(false) | |
} |
增加推送回调监听(addPushReceiver 接管自定义透传音讯回调、接管告诉音讯回调、接管点击告诉音讯回调、接管别名或标签操作回调)
addPushReceiver | |
MobpushPlugin.addPushReceiver(_onEvent, _onError); | |
void _onEvent(Object event) { | |
} | |
void _onError(Object event) {} |
进行推送(stopPush)
`stopPush
MobpushPlugin.stopPush();
`
从新关上推送服务(restartPush)
restartPush | |
MobpushPlugin.restartPush(); |
是否已进行接管推送(isPushStopped)
isPushStopped | |
MobpushPlugin.isPushStopped(); |
设置别名(setAlias)
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)
getAlias | |
MobpushPlugin.getAlias().then((Map<String, dynamic> aliasMap){String res = aliasMap['res']; | |
String error = aliasMap['error']; | |
print(">>>>>>>>>>>>>>>>>>>>>>>>>>> getAlias -> res: $res error: $error"); | |
}); |
删除别名(deleteAlias)
deleteAlias | |
MobpushPlugin.deleteAlias().then((Map<String, dynamic> aliasMap){String res = aliasMap['res']; | |
String error = aliasMap['error']; | |
print(">>>>>>>>>>>>>>>>>>>>>>>>>>> deleteAlias -> res: $res error: $error"); | |
}); |
增加标签(addTags)
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)
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)
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)
cleanTags | |
MobpushPlugin.cleanTags().then((Map<String, dynamic> tagsMap){String res = tagsMap['res']; | |
String error = tagsMap['error']; | |
print(">>>>>>>>>>>>>>>>>>>>>>>>>>> cleanTags -> res: $res error: $error"); | |
}); |
发送本地告诉(addLocalNotification)
addLocalNotification | |
MobpushPlugin.addLocalNotification(); |
绑定手机号(bindPhoneNum)
bindPhoneNum | |
MobpushPlugin.bindPhoneNum("110"); |
测试模仿推送,用于测试(send)
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 仅 Android)
setClickNotificationToLaunchMainActivity | |
MobpushPlugin.setClickNotificationToLaunchMainActivity (bool enable); |
移除本地告诉 (removeLocalNotification 仅 Android)
removeLocalNotification | |
MobpushPlugin.removeLocalNotification(int notificationId); |
清空本地告诉 (clearLocalNotifications 仅)
clearLocalNotifications | |
MobpushPlugin.clearLocalNotifications(); |
设置告诉栏 icon,不设置默认取利用 icon(setNotifyIcon 仅 Android)
setNotifyIcon | |
MobpushPlugin.setNotifyIcon(String resId); |
设置告诉静音时段(推送选项)(setSilenceTime 仅 Android)
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 仅 iOS)
setBadge | |
MobpushPlugin.setBadge(int badge); |
清空角标,不革除告诉栏音讯记录 (clearBadge 仅 iOS)
`clearBadge | |
MobpushPlugin.clearBadge();` |
获取注册 Id(getRegistrationId)
getRegistrationId | |
MobpushPlugin.getRegistrationId().then((Map<String, dynamic> ridMap) {print(ridMap); | |
String regId = ridMap['res'].toString(); | |
print('------>#### registrationId:' + regId); | |
}); |
Flutter iOS 端注意事项
因为插件更新,SDK 的 Pod 依赖被替换,Flutter 自身写入 Pod 文件不会先执行删除原有依赖,导致可能会呈现原有本地库仍然存在,请查看 Pod 文件夹下文件,间接手动删除 mob_pushsdk 以及 MOBFoundation 文件即可,如有疑难,请间接通过官网和咱们分割。
其余问题
demo 地址
demo: GitHub 地址
推送证书制作
推送证书申请流程见:推送证书文档
正文完
发表至: javascript
2022-11-27