Flutter移动SDK集成插件ummobile_sdk的使用

Flutter移动SDK集成插件ummobile_sdk的使用

初始化

要初始化一个新的实例,需要一个令牌。

UMMobileSDK sdk = UMMobileSDK(token: 'YOUR_TOKEN');

鉴权

获取令牌可以使用静态函数 UMMobileSDK.auth(),该函数返回用于鉴权的API部分。

// 获取令牌
Token token = await UMMobileSDK
  .auth()
  .getToken(username: 1234567, password: 'YOUR_PASSWORD');

// 获取测试用的沙箱服务令牌
Token sandboxToken = await UMMobileSDK
  .auth()
  .getToken(
    username: 1234567,
    password: 'YOUR_PASSWORD',
    sandbox: true,
  );

// 使用访问令牌进行初始化
UMMobileSDK sdk = UMMobileSDK(token: token.accessToken);

使用

UMMobileSDK 包含每个API部分的属性。

用户信息

用户信息可以在 UMMobileSDK 类的 user 属性中找到,也可以通过 UMMobileUser 类来使用。

获取用户信息

User user = await sdk.user.getInformation();

获取用户头像

String base64Image = await sdk.user.getProfilePicture();

课程表信息

课程表信息可以在 UMMobileSDK 类的 catalogue 属性中找到,也可以通过 UMMobileCatalogue 类来使用。

获取规则

List<Rule> rules = await sdk.catalogue.getRules();

获取国家列表

List<Country> countries = await sdk.catalogue.getCountries();

获取课程表

Calendar calendar = await sdk.catalogue.getCalendar();

print(calendar.events); // [Instance of Event, Instance of Event, Instance of Event, ...]
print(calendar.summary); // title: "Agenda institucional"

通讯信息

通讯信息可以在 UMMobileSDK 类的 communication 属性中找到,也可以通过 UMMobileCommunication 类来使用。

获取新闻

List<Post> posts = await sdk.communication.getNews(quantity: 3);

获取活动

List<Post> posts = await sdk.communication.getEvents(quantity: 3);

获取博客文章

List<Post> posts = await sdk.communication.getBlog(quantity: 3);

获取故事

List<Group> groups = await sdk.communication.getStories();

print(groups.stories); // [Instance of Story, Instance of Story, ...]

学术信息

学术信息可以在 UMMobileSDK 类的 academic 属性中找到,也可以通过 UMMobileAcademic 类来使用。

获取文档列表

List<Document> documents = await sdk.academic.getDocuments();

获取文档页面

DocumentPage page = await sdk.academic.getImagePage(1, 1);

print(page.page); // 1
print(page.base64Image); // the base64 page image 

获取所有学期信息

AllSemesters all = await sdk.academic.getAllSemesters();

print(all.semesters); // Example: [Instance of Semester, Instance of Semester]
print(all.average); // Example: 98.37

获取当前学期信息

Semester semester = await sdk.academic.getCurrentSemester();

print(semester.subjects); // List of current subjects
print(semester.name); // Example: "PRIMER SEMESTRE"

获取当前计划ID

String planId = await sdk.academic.getPlan();

获取全球平均分

double average = await sdk.academic.getGlobalAverage();

财务信息

财务信息可以在 UMMobileSDK 类的 financial 属性中找到,也可以通过 UMMobileFinancial 类来使用。

获取余额列表

List<Balance> balances = await sdk.financial.getBalances();
print(balances.first.movements); // null

// 只获取本年度的交易记录
List<Balance> balances = await sdk.financial.getBalances(includeMovements: IncludeMovements.OnlyCurrent);
print(balances.first.movements!.current); // [Instance of Movement, Instance of Movement, ...]
print(balances.first.movements!.lastYear); // null

// 获取本年度和上一年度的交易记录
List<Balance> balances = await sdk.financial.getBalances(includeMovements: IncludeMovements.CurrentAndLastYear);
print(balances.first.movements!.current); // [Instance of Movement, Instance of Movement, ...]
print(balances.first.movements!.lastYear); // [Instance of Movement, Instance of Movement, ...]

获取余额交易记录

Movements movements = await sdk.financial.getMovements('BALANCE_ID');
print(movements.current); // [Instance of Movement, Instance of Movement, ...]
print(movements.lastYear); // null

// 获取本年度和上一年度的交易记录
Movements movements = await sdk.financial.getMovements('BALANCE_ID', includeLastYear: true);
print(movements.current); // [Instance of Movement, Instance of Movement, ...]
print(movements.lastYear); // [Instance of Movement, Instance of Movement, ...]

生成支付URL

Payment payment = Payment(
  reference: '1130745-SFORMA01-123098123098123',
  amount: 10,
  clientMail: '1130745@alumno.um.edu.mx',
  additionalData: [
    PaymentAdditionalData(
      id: 1,
      label: 'UMMobile',
      value: 'true',
    ),
  ],
);

String urlA = await student.financial.generatePaymentUrl(payment);

// 生成URL并请求发票
String urlB = await student.financial.generatePaymentUrl(
  payment,
  requestInvoice: true,
);

通知信息

通知信息可以在 UMMobileSDK 类的 notifications 属性中找到,也可以通过 UMMobileNotifications 类来使用。

获取所有通知

List<Notification> defaultValues =
          await sdk.notifications.getAll();

print(defaultValues.first.heading); // Hi
print(defaultValues.any((notification) => notification.isDeleted)); // false

// 使用英语语言
List<Notification> english =
          await sdk.notifications.getAll(languageCode: 'en');

print(english.first.heading); // Hi

// 更改为西班牙语
List<Notification> spanish =
          await sdk.notifications.getAll(languageCode: 'es');

print(spanish.first.heading); // Hola

// 获取不同默认值的通知
print(spanish.first.headingTr('en')); // Hi
print(spanish.first.headingTr('es')); // Hola

// 包含已删除的通知
List<Notification> withDeletedNotifications =
          await sdk.notifications.getAll(ignoreDeleted: false);

print(withDeletedNotifications.any((notification) => notification.isDeleted)); // true if user had deleted at least one notification.

获取单个通知

Notification notification =
          await sdk.notifications.getOne('NOTIFICATION_ID');

// 同时接收语言代码或忽略已删除的通知
Notification notification =
      await sdk.notifications.getOne(
          'NOTIFICATION_ID',
          languageCode: 'es', // 使用西班牙语
          ignoreDelete: false, // 允许搜索已删除的通知。
      );

将通知标记为已读

Notification notification =
          await sdk.notifications.getOne('NOTIFICATION_ID');

print(notification.isReceived); // false

Notification receivedNotification =
          await sdk.notifications.markAsReceived(notification.id);

print(receivedNotification.isReceived); // true

将通知标记为已查看

Notification notification =
          await sdk.notifications.getOne('NOTIFICATION_ID');

print(notification.isSeen); // false

Notification seenNotification =
          await sdk.notifications.markAsSeen(notification.id);

print(seenNotification.isSeen); // true

删除通知

Notification notification =
          await sdk.notifications.getOne('NOTIFICATION_ID');

print(notification.isDeleted); // false

Notification deletedNotification =
          await sdk.notifications.delete(notification.id);

print(deletedNotification.isDeleted); // true

新冠问卷调查

新冠问卷调查信息可以在 UMMobileSDK 类的 questionnaire.covid 属性中找到,也可以通过 UMMobileCovid 类来使用。

获取所有答案

List<CovidQuestionnaireAnswerDatabase> answers =
          await sdk.questionnaire.covid.getAnswers();

// 获取当前日期的答案
List<CovidQuestionnaireAnswerDatabase> todayAnswers =
          await sdk.questionnaire.covid.getAnswers(filter: Answers.Today);

获取当前日期的答案

List<CovidQuestionnaireAnswerDatabase> todayAnswers =
          await sdk.questionnaire.covid.getTodayAnswers();

获取额外信息

UserCovidInformation extras =
          await sdk.questionnaire.covid.getExtras();

print(extras.isVaccinated); // false or true
print(extras.haveCovid); // false or true
print(extras.isSuspect); // false or true
print(extras.isInQuarantine); // false or true

获取验证结果

CovidValidation validation =
          await sdk.questionnaire.covid.getValidation();

print(validation.allowAccess); // if can or cannot enter
print(validation.reason); // the reason of the validations result
print(validation.qrUrl); // the URI to the QR image

检查是否上传了响应信

bool haveResponsiveLetter =
          await sdk.questionnaire.covid.haveResponsiveLetter();

更新额外信息

bool updated =
          await sdk.questionnaire.covid.updateExtras(isSuspect: false);

保存问卷答案

CovidQuestionnaireAnswer answer = CovidQuestionnaireAnswer(
  countries: [
    RecentCountry(
      country: 'México',
      city: 'Montemorelos',
    ),
  ],
  recentContact: RecentContact(yes: false),
  majorSymptoms: {
    'tos': false,
  },
  minorSymptoms: {
    'dolorDePancita': false,
  },
);

CovidValidation validation =
          await sdk.questionnaire.covid.saveAnswer(answer);

print(validation.allowAccess); // if can or cannot enter
print(validation.reason); // the reason of the validations result
print(validation.qrUrl); // the URI to the QR image

更多关于Flutter移动SDK集成插件ummobile_sdk的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter移动SDK集成插件ummobile_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


ummobile_sdk 是一个用于集成 Flutter 移动应用的 SDK 插件,通常用于与特定的移动服务或功能进行交互。以下是集成和使用 ummobile_sdk 的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 ummobile_sdk 插件的依赖。

dependencies:
  flutter:
    sdk: flutter
  ummobile_sdk: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来获取依赖。

2. 初始化 SDK

在应用程序的入口点(通常是 main.dart 文件)中初始化 ummobile_sdk

import 'package:flutter/material.dart';
import 'package:ummobile_sdk/ummobile_sdk.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化 SDK
  await UMMobileSdk.init(
    appKey: 'YOUR_APP_KEY',  // 替换为你的App Key
    appSecret: 'YOUR_APP_SECRET',  // 替换为你的App Secret
  );

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

3. 使用 SDK 功能

根据 ummobile_sdk 提供的 API,你可以在应用中使用各种功能。例如,调用某个服务或获取数据。

import 'package:flutter/material.dart';
import 'package:ummobile_sdk/ummobile_sdk.dart';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _data = 'No data';

  Future<void> _fetchData() async {
    try {
      var result = await UMMobileSdk.getSomeData();
      setState(() {
        _data = result;
      });
    } catch (e) {
      setState(() {
        _data = 'Failed to fetch data: $e';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('UMMobile SDK Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(_data),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: _fetchData,
              child: Text('Fetch Data'),
            ),
          ],
        ),
      ),
    );
  }
}

4. 处理权限和配置

根据 ummobile_sdk 的功能需求,你可能需要在 AndroidManifest.xmlInfo.plist 中添加权限或配置。

Android

android/app/src/main/AndroidManifest.xml 中添加必要的权限:

<uses-permission android:name="android.permission.INTERNET" />
<!-- 其他权限 -->

iOS

ios/Runner/Info.plist 中添加必要的权限:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>
<!-- 其他权限 -->

5. 调试和发布

在开发过程中,你可以使用 flutter run 来调试应用。在发布应用之前,确保所有的配置和权限都正确设置。

6. 处理错误和异常

在使用 ummobile_sdk 时,可能会遇到各种错误和异常。确保在代码中正确处理这些情况,以提供更好的用户体验。

try {
  var result = await UMMobileSdk.getSomeData();
  // 处理结果
} catch (e) {
  // 处理异常
  print('Error: $e');
}
回到顶部