集成预备

这是一个基于 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 onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
  }

SDK API

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

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

MobPush for Flutter

例:

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地址

推送证书制造

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