Flutter服务器通知插件serverchan_sdk的使用
Flutter服务器通知插件serverchan_sdk
的使用
serverchan-sdk
是一个用于调用 Server 酱推送服务的 Dart SDK。该 SDK 允许你通过简单的接口将消息发送到 Server 酱,并支持可选的自定义参数。
功能
- 支持 Server 酱
<code>SendKey</code>
推送。 - 支持自定义消息内容、描述、频道等选项。
- 支持 API 请求结果的解析。
安装
在 pubspec.yaml
文件中添加依赖项:
dependencies:
http: ^0.13.0
serverchan_sdk:
path: ./serverchan_sdk
然后运行以下命令:
dart pub get
使用方法
简单示例
import 'package:serverchan_sdk/serverchan_sdk.dart';
void main() async {
const sendkey = 'your-sendkey'; // 替换为你的 SendKey
const title = '测试标题'; // 消息标题
const desp = '这是消息的详细内容'; // 消息内容
try {
ScSendResponse response = await scSend(sendkey, title, desp: desp);
print('Response Code: ${response.code}');
print('Response Message: ${response.message}');
} catch (e) {
print('Error: $e');
}
}
带有可选参数的示例
import 'package:serverchan_sdk/serverchan_sdk.dart';
void main() async {
const sendkey = 'your-sendkey'; // 替换为你的 SendKey
const title = '带选项的测试标题'; // 消息标题
const desp = '这是带选项的消息内容'; // 消息内容
ScSendOptions options = ScSendOptions(
tags: '测试,标签', // 添加标签
shortDesc: '简短描述', // 简短描述
noip: 1, // 不显示 IP 地址
);
try {
ScSendResponse response = await scSend(sendkey, title, desp: desp, options: options);
print('Response Code: ${response.code}');
print('Response Message: ${response.message}');
} catch (e) {
print('Error: $e');
}
}
API 参考
scSend
Future<ScSendResponse> scSend(String sendkey, String title, {String desp = '', ScSendOptions? options})
发送消息到 Server 酱。
-
参数说明:
sendkey
: Server 酱的 SendKey,用于认证推送。title
: 消息的标题。desp
(可选): 消息的详细内容,默认为空字符串。options
(可选): 一个ScSendOptions
对象,包含推送的可选参数。
-
返回值:
- 返回一个
Future<ScSendResponse>
,其中包含推送请求的结果。
- 返回一个
ScSendOptions
可选的推送参数,包含以下字段:
tags
: 消息的标签。shortDesc
: 简短的描述。noip
: 是否不显示 IP 地址(1 表示不显示)。channel
: 自定义推送频道。openid
: 指定的用户 OpenID。
ScSendResponse
响应对象,包含以下字段:
code
: API 返回的状态码。message
: API 返回的消息。data
: API 返回的数据(如果有的话)。
错误处理
在请求过程中可能会抛出异常,因此建议使用 try-catch
进行异常处理。
try {
// 发送推送请求
} catch (e) {
print('Error: $e');
}
示例代码
以下是完整的示例代码:
import 'package:serverchan_sdk/serverchan_sdk.dart';
void main() async {
const sendkey = 'your-sendkey'; // 替换为你的 SendKey
const title = 'Hello, Server Chan!'; // 消息标题
const desp = 'This is a test message from Dart SDK.'; // 消息内容
// 可选参数
ScSendOptions options = ScSendOptions(
tags: 'test,serverchan', // 添加标签
shortDesc: 'Test Message', // 简短描述
noip: 1, // 不显示 IP 地址
);
try {
ScSendResponse response =
await scSend(sendkey, title, desp: desp, options: options);
print('Response Code: ${response.code}');
print('Response Message: ${response.message}');
if (response.data != null) {
print('Response Data: ${response.data}');
}
} catch (e) {
print('Error: $e');
}
}
更多关于Flutter服务器通知插件serverchan_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
serverchan_sdk
是一个用于在 Flutter 应用中集成 ServerChan 通知服务的插件。ServerChan 是一个简单易用的消息推送服务,可以通过微信接收通知。使用 serverchan_sdk
,你可以轻松地在 Flutter 应用中发送通知到微信。
安装
首先,你需要在 pubspec.yaml
文件中添加 serverchan_sdk
依赖:
dependencies:
flutter:
sdk: flutter
serverchan_sdk: ^0.1.0 # 请检查最新版本
然后运行 flutter pub get
来安装依赖。
使用
-
获取 SCKEY
在使用
serverchan_sdk
之前,你需要在 ServerChan 上注册并获取你的 SCKEY。SCKEY 是用于标识你的应用并发送通知的唯一密钥。 -
初始化
在你的 Flutter 应用中,首先需要初始化
ServerChan
实例:import 'package:serverchan_sdk/serverchan_sdk.dart'; final serverChan = ServerChan('YOUR_SCKEY');
将
YOUR_SCKEY
替换为你从 ServerChan 获取的 SCKEY。 -
发送通知
你可以使用
send
方法来发送通知。send
方法接受两个参数:title
和content
。void sendNotification() async { final response = await serverChan.send( title: '通知标题', content: '通知内容', ); if (response.isSuccess) { print('通知发送成功'); } else { print('通知发送失败: ${response.error}'); } }
send
方法返回一个ServerChanResponse
对象,你可以通过isSuccess
属性来判断通知是否发送成功,并通过error
属性获取错误信息。 -
发送 Markdown 格式的通知
如果你需要发送 Markdown 格式的通知,可以使用
sendMarkdown
方法:void sendMarkdownNotification() async { final response = await serverChan.sendMarkdown( title: 'Markdown 通知标题', content: '**这是加粗的文本**\n\n[这是一个链接](https://example.com)', ); if (response.isSuccess) { print('Markdown 通知发送成功'); } else { print('Markdown 通知发送失败: ${response.error}'); } }
示例
以下是一个完整的示例,展示了如何在 Flutter 应用中使用 serverchan_sdk
发送通知:
import 'package:flutter/material.dart';
import 'package:serverchan_sdk/serverchan_sdk.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('ServerChan SDK Example'),
),
body: Center(
child: ElevatedButton(
onPressed: sendNotification,
child: Text('发送通知'),
),
),
),
);
}
void sendNotification() async {
final serverChan = ServerChan('YOUR_SCKEY');
final response = await serverChan.send(
title: '通知标题',
content: '通知内容',
);
if (response.isSuccess) {
print('通知发送成功');
} else {
print('通知发送失败: ${response.error}');
}
}
}