飞道的博客

MobPush for Flutter

586人阅读  评论(0)

集成准备

这是一个基于 MobPush 功能的扩展的 Flutter 插件。使用此插件能够帮助您在使用 Flutter 开发应用时,快速地实现推送功能。

在pubspec.yaml文件中加入下面依赖


  
  1. dependencies:
  2. mobcommonlib:
  3. mobpush_plugin:

然后执行:flutter packages get 导入package 在你的dart工程文件中,导入下面头文件,开始使用


  
  1. import 'package:mobcommonlib/mobcommonlib.dart';
  2. import 'package:mobpush_plugin/mobpush_plugin.dart';

iOS

平台配置参考 iOS集成文档

实现文档中 Xcode配置:配置AppKey和AppSecret

Android

导入 MobPush 相关依赖

在项目根目录的build.gradle中添加以下代码:


  
  1. buildscript {
  2. repositories {
  3. // 配置Mob Maven库
  4. maven {
  5. url "https://mvn.mob.com/android"
  6. }
  7. // 配置HMS Core SDK的Maven仓地址。(集成华为厂商需要添加)
  8. maven {
  9. url 'https: //developer.huawei.com/repo/'}
  10. }
  11. ...
  12. }
  13. dependencies {
  14. ...
  15. // 集成MobPush
  16. classpath "com.mob.sdk:MobSDK:2018.0319.1724"
  17. }
  18. }

在 /android/app/build.gradle 中添加以下代码:


  
  1. apply plugin: 'com.android.application'
  2. apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
  3. // 导入MobSDK
  4. apply plugin: 'com.mob.sdk'

平台相关集成 在项目的/android/app/build.gradle中添加:


  
  1. MobSDK {
  2. appKey "您的MobTech平台appKey"
  3. appSecret "您的MobTech平台appSecret"
  4. //配置MobPush
  5. MobPush {
  6. //配置厂商推送(可选配置,不需要厂商推送可不配置,需要哪些厂商推送只需配置哪些厂商配置即可)
  7. devInfo {
  8. //配置小米厂商推送
  9. XIAOMI {
  10. appId "您的小米平台appId"
  11. appKey "您的小米平台appKey"
  12. }
  13. //配置华为厂商推送
  14. HUAWEI {
  15. appId "您的华为平台appId"
  16. }
  17. //配置魅族厂商推送
  18. MEIZU {
  19. appId "您的魅族平台appId"
  20. appKey "您的魅族平台appKey"
  21. }
  22. //配置FCM厂商推送
  23. FCM {
  24. //设置默认推送通知显示图标
  25. iconRes "@mipmap/default_ic_launcher"
  26. }
  27. //配置OPPO厂商推送
  28. OPPO {
  29. appKey "您的OPPO平台appKey"
  30. appSecret "您的OPPO平台appSecret"
  31. }
  32. //配置VIVO厂商推送
  33. VIVO {
  34. appId "您的VIVO平台appId"
  35. appKey "您的VIVO平台appKey"
  36. }
  37. }
  38. }
  39. }

添加代码

在MainActivity的onCreate中添加以下代码:


  
  1. @Override
  2. protected void onCreate( Bundle savedInstanceState) {
  3. super. onCreate(savedInstanceState);
  4. GeneratedPluginRegistrant. registerWith( this);
  5. }

SDK API

回传用户隐私授权结果 (submitPrivacyGrantResult)


  
  1. /**
  2. * 回传用户隐私授权结果
  3. * @param status 用户是否同意隐私协议
  4. * @param result 默认传null
  5. */
  6. Mobcommonlib. submitPolicyGrantResult( bool status, Function( bool)? result)

 

例:

Mobcommonlib.submitPolicyGrantResult(true, null);

设置远程推送环境,向用户授权(setCustomNotification仅 iOS)


  
  1. setCustomNotification
  2. if (Platform.isIOS) {
  3. MobpushPlugin .setCustomNotification();
  4. }

设置远程推送环境 (setAPNsForProduction仅 iOS)


  
  1. setAPNsForProduction
  2. if (Platform.isIOS) {
  3. // 开发环境 false, 线上环境 true
  4. MobpushPlugin.setAPNsForProduction( false)
  5. }

添加推送回调监听(addPushReceiver 接收自定义透传消息回调、接收通知消息回调、接收点击通知消息回调、接收别名或标签操作回调)


  
  1. addPushReceiver
  2. MobpushPlugin. addPushReceiver(_onEvent, _onError);
  3. void _onEvent( Object event) {
  4. }
  5. void _onError( Object event) {
  6. }

停止推送(stopPush)


  
  1. stopPush
  2. MobpushPlugin .stopPush();

重新打开推送服务(restartPush)


  
  1. restartPush
  2. MobpushPlugin .restartPush();

是否已停止接收推送(isPushStopped)


  
  1. isPushStopped
  2. MobpushPlugin .isPushStopped();

设置别名(setAlias)


  
  1. setAlias
  2. MobpushPlugin.setAlias( "别名").then(( Map< String, dynamic> aliasMap){
  3. String res = aliasMap[ 'res'];
  4. String error = aliasMap[ 'error'];
  5. String errorCode = aliasMap[ 'errorCode'];
  6. print( ">>>>>>>>>>>>>>>>>>>>>>>>>>> setAlias -> res: $res error: $error");
  7. });

获取别名(getAlias)


  
  1. getAlias
  2. MobpushPlugin.getAlias().then(( Map< String, dynamic> aliasMap){
  3. String res = aliasMap[ 'res'];
  4. String error = aliasMap[ 'error'];
  5. print( ">>>>>>>>>>>>>>>>>>>>>>>>>>> getAlias -> res: $res error: $error");
  6. });

删除别名(deleteAlias)


  
  1. deleteAlias
  2. MobpushPlugin.deleteAlias().then(( Map< String, dynamic> aliasMap){
  3. String res = aliasMap[ 'res'];
  4. String error = aliasMap[ 'error'];
  5. print( ">>>>>>>>>>>>>>>>>>>>>>>>>>> deleteAlias -> res: $res error: $error");
  6. });

添加标签(addTags)


  
  1. addTags
  2. List tags = new List();
  3. tags.add( "tag1");
  4. tags.add( "tag2");
  5. MobpushPlugin.addTags(tags).then(( Map< String, dynamic> tagsMap){
  6. String res = tagsMap[ 'res'];
  7. String error = tagsMap[ 'error'];
  8. print( ">>>>>>>>>>>>>>>>>>>>>>>>>>> addTags -> res: $res error: $error");
  9. });

获取标签(getTags)


  
  1. getTags
  2. MobpushPlugin.getTags().then(( Map< String, dynamic> tagsMap) {
  3. List< String> resList;
  4. if (tagsMap[ 'res'] == null) {
  5. resList = [];
  6. } else {
  7. resList = tagsMap[ 'res'].toList();
  8. }
  9. String error = tagsMap[ 'error'];
  10. print(
  11. ">>>>>>>>>>>>>>>>>>>>>>>>>>> getTags -> res: $resList error: $error");
  12. });

删除标签(deleteTags)


  
  1. deleteTags
  2. List tags = new List();
  3. tags.add( "tag1");
  4. tags.add( "tag2");
  5. MobpushPlugin.deleteTags(tags).then(( Map< String, dynamic> tagsMap){
  6. String res = tagsMap[ 'res'];
  7. String error = tagsMap[ 'error'];
  8. print( ">>>>>>>>>>>>>>>>>>>>>>>>>>> deleteTags -> res: $res error: $error");
  9. });

清空标签(cleanTags)


  
  1. cleanTags
  2. MobpushPlugin.cleanTags().then(( Map< String, dynamic> tagsMap){
  3. String res = tagsMap[ 'res'];
  4. String error = tagsMap[ 'error'];
  5. print( ">>>>>>>>>>>>>>>>>>>>>>>>>>> cleanTags -> res: $res error: $error");
  6. });

发送本地通知(addLocalNotification)


  
  1. addLocalNotification
  2. MobpushPlugin .addLocalNotification();

绑定手机号(bindPhoneNum)


  
  1. bindPhoneNum
  2. MobpushPlugin .bindPhoneNum(" 110");

测试模拟推送,用于测试(send)


  
  1. send
  2. /**
  3. * 测试模拟推送,用于测试
  4. * type:模拟消息类型,1、通知测试;2、内推测试;3、定时
  5. * content:模拟发送内容,500字节以内,UTF-8
  6. * space:仅对定时消息有效,单位分钟,默认1分钟
  7. * extras: 附加数据,json字符串
  8. */
  9. MobpushPlugin.send( int type, String content, int space, String extras).then(( Map< String, dynamic> sendMap){
  10. String res = sendMap[ 'res'];
  11. String error = sendMap[ 'error'];
  12. print( ">>>>>>>>>>>>>>>>>>>>>>>>>>> send -> res: $res error: $error");
  13. });

设置点击通知是否跳转默认页 (setClickNotificationToLaunchMainActivity 仅Android)


  
  1. setClickNotificationToLaunchMainActivity
  2. MobpushPlugin .setClickNotificationToLaunchMainActivity (bool enable);

移除本地通知(removeLocalNotification 仅Android)


  
  1. removeLocalNotification
  2. MobpushPlugin .removeLocalNotification(int notificationId);

清空本地通知(clearLocalNotifications 仅)


  
  1. clearLocalNotifications
  2. MobpushPlugin .clearLocalNotifications();

设置通知栏icon,不设置默认取应用icon(setNotifyIcon 仅Android)


  
  1. setNotifyIcon
  2. MobpushPlugin .setNotifyIcon(String resId);

设置通知静音时段(推送选项)(setSilenceTime 仅Android)


  
  1. setSilenceTime
  2. /**
  3. * 设置通知静音时段(推送选项)(仅Android)
  4. * @param startHour 开始时间[0~23] (小时)
  5. * @param startMinute 开始时间[0~59](分钟)
  6. * @param endHour 结束时间[0~23](小时)
  7. * @param endMinute 结束时间[0~59](分钟)
  8. */
  9. MobpushPlugin. setSilenceTime( int startHour, int startMinute, int endHour, int endMinute)

设置角标 (setBadge仅 iOS)


  
  1. setBadge
  2. MobpushPlugin .setBadge(int badge);

清空角标,不清除通知栏消息记录 (clearBadge仅 iOS)


  
  1. clearBadge
  2. MobpushPlugin .clearBadge();

获取注册Id(getRegistrationId)


  
  1. getRegistrationId
  2. MobpushPlugin.getRegistrationId().then(( Map< String, dynamic> ridMap) {
  3. print(ridMap);
  4. String regId = ridMap[ 'res'].toString();
  5. print( '------>#### registrationId: ' + regId);
  6. });

Flutter iOS端注意事项

由于插件更新,SDK的Pod依赖被替换,Flutter 本身写入Pod文件不会先执行删除原有依赖,导致可能会出现原有本地库依然存在,请检查Pod文件夹下文件,直接手动删除mob_pushsdk 以及 MOBFoundation文件即可,如有疑问,请直接通过官网和我们联系。

其他问题

demo地址

demo: GitHub地址

推送证书制作

推送证书申请流程见:推送证书文档

 


转载:https://blog.csdn.net/apkkkk/article/details/128055453
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场