Flutter推送服务插件luo_push_getui的使用
luo_push_getui 的使用 #
个推集成
快速开始 #
此项目是一个用于 Flutter 的插件包起点,包含针对 Android 和/或 iOS 平台的具体实现代码。
有关 Flutter 开发的帮助,请查看 官方文档,其中包含教程、示例、移动开发指南以及完整的 API 参考。
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:luo_push_getui/luo_push_getui.dart'; // 导入 luo_push_getui 插件
void main() {
runApp(const MyApp()); // 运行应用
}
class MyApp extends StatefulWidget {
const MyApp({super.key}); // 初始化 MyAPP
[@override](/user/override)
State<MyApp> createState() => _MyAppState(); // 创建状态
}
class _MyAppState extends State<MyApp> {
String _platformVersion = '未知平台版本'; // 存储平台版本
final _luoPushGetuiPlugin = LuoPushGetui(); // 初始化插件实例
[@override](/user/override)
void initState() {
super.initState();
initPlatformState(); // 初始化平台状态
}
// 异步方法初始化平台状态
Future<void> initPlatformState() async {
String platformVersion;
try {
// 获取平台版本,处理可能的异常
platformVersion = await _luoPushGetuiPlugin.getPlatformVersion() ??
'未知平台版本';
} on PlatformException {
platformVersion = '获取平台版本失败。';
}
// 如果组件从树中移除,则不更新 UI
if (!mounted) return;
// 更新 UI
setState(() {
_platformVersion = platformVersion;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('插件示例应用'), // 设置应用标题
),
body: Center(
child: Column(
children: [
// 显示当前运行的平台版本
Text('运行在: $_platformVersion\n'),
// 测试按钮,调用 vibrate 方法
ElevatedButton(
onPressed: () async {
String res = await _luoPushGetuiPlugin.vibrate() ??
'未知平台版本';
setState(() {
_platformVersion = res;
});
},
child: Text("测试"), // 按钮文本
)
],
),
),
),
);
}
}
更多关于Flutter推送服务插件luo_push_getui的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter推送服务插件luo_push_getui的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
luo_push_getui
是一个基于 Flutter 的插件,用于集成个推(Getui)推送服务。个推是一种常用的消息推送服务,适用于 Android 和 iOS 平台。以下是如何在 Flutter 项目中使用 luo_push_getui
插件的步骤:
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 luo_push_getui
插件的依赖:
dependencies:
flutter:
sdk: flutter
luo_push_getui: ^版本号
注意:请将 ^版本号
替换为最新的插件版本号。
2. 配置 Android 项目
在 Android 项目中,需要进行以下配置:
2.1 修改 AndroidManifest.xml
在 android/app/src/main/AndroidManifest.xml
文件中添加以下配置:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp">
<application
android:name=".MyApplication"
...>
<!-- 个推配置 -->
<meta-data
android:name="com.getui.push.appId"
android:value="YOUR_APP_ID" />
<meta-data
android:name="com.getui.push.appKey"
android:value="YOUR_APP_KEY" />
<meta-data
android:name="com.getui.push.appSecret"
android:value="YOUR_APP_SECRET" />
<!-- 个推服务 -->
<service
android:name="com.igexin.sdk.PushService"
android:exported="true"
android:label="PushService"
android:process=":pushservice" />
<service
android:name="com.igexin.sdk.PushServiceUser"
android:exported="true"
android:label="PushServiceUser" />
<receiver
android:name="com.igexin.sdk.PushReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="com.igexin.sdk.action.refreshls" />
</intent-filter>
</receiver>
<receiver
android:name="com.igexin.sdk.PushManagerReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.igexin.sdk.action.pushmanager" />
</intent-filter>
</receiver>
<receiver
android:name="com.igexin.sdk.GActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="gtpush" />
</intent-filter>
</receiver>
</application>
</manifest>
将 YOUR_APP_ID
、YOUR_APP_KEY
和 YOUR_APP_SECRET
替换为你在个推平台注册应用时获取的相应值。
2.2 修改 build.gradle
在 android/app/build.gradle
文件中,确保 minSdkVersion
至少为 16:
defaultConfig {
applicationId "com.example.yourapp"
minSdkVersion 16
targetSdkVersion 30
...
}
3. 配置 iOS 项目
在 iOS 项目中,需要进行以下配置:
3.1 修改 Info.plist
在 ios/Runner/Info.plist
文件中添加以下配置:
<key>CFBundleIdentifier</key>
<string>com.example.yourapp</string>
<key>CFBundleVersion</key>
<string>1.0.0</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleDisplayName</key>
<string>YourApp</string>
<!-- 个推配置 -->
<key>GtAppId</key>
<string>YOUR_APP_ID</string>
<key>GtAppKey</key>
<string>YOUR_APP_KEY</string>
<key>GtAppSecret</key>
<string>YOUR_APP_SECRET</string>
将 YOUR_APP_ID
、YOUR_APP_KEY
和 YOUR_APP_SECRET
替换为你在个推平台注册应用时获取的相应值。
3.2 修改 AppDelegate.swift
在 ios/Runner/AppDelegate.swift
文件中,添加以下代码:
import UIKit
import Flutter
import luo_push_getui
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
LuoPushGetuiPlugin.register(with: self.registrar(forPlugin: "LuoPushGetuiPlugin")!)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
4. 在 Flutter 中使用插件
在 Flutter 代码中,你可以使用 luo_push_getui
插件来初始化个推服务并处理推送消息。
import 'package:flutter/material.dart';
import 'package:luo_push_getui/luo_push_getui.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _pushMessage = 'No message yet';
[@override](/user/override)
void initState() {
super.initState();
_initGetui();
}
void _initGetui() async {
// 初始化个推服务
await LuoPushGetui.init(
appId: 'YOUR_APP_ID',
appKey: 'YOUR_APP_KEY',
appSecret: 'YOUR_APP_SECRET',
);
// 监听推送消息
LuoPushGetui.onMessageReceived.listen((message) {
setState(() {
_pushMessage = message;
});
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Getui Push Example'),
),
body: Center(
child: Text('Push Message: $_pushMessage'),
),
),
);
}
}