Flutter通知推送插件getuiflut_noifa的使用
1、引用
Pub.dev:
getui-flutter-plugin
增加依赖:
flutter pub add getuiflut
或者手动在工程 pubspec.yaml
中加入 dependencies:
dependencies:
getuiflut: ^0.2.16
下载依赖:
flutter pub get
flutter run
2、配置
2.1、Android
参考官网文档中心进行配置:https://docs.getui.com/getui/mobile/android/overview/
flutter插件默认包含自定义组件,Flutter用户不用处理以下配置:
- Android->集成指南-> 3.配置推送服务-> FlutterPushService (“继承自 com.igexin.sdk.PushService 的自定义 Service”)
- Android->集成指南-> 6.编写集成代码-> FlutterIntentService (“继承自 com.igexin.sdk.GTIntentService 的自定义 Service”)
2.2、iOS
在你项目的 main.dart
中添加下列代码:
Getuiflut().startSdk(
appId: "8eLAkGIYnGAwA9fVYZU93A",
appKey: "VFX8xYxvVF6w59tsvY6XN",
appSecret: "Kv3TeED8z19QwnMLdzdI35"
);
启用 notification:Xcode 主工程配置 -> Signing & Capabilities -> + Push Notifications
3、使用
3.1、公共 API
导入插件:
import 'package:getuiflut/getuiflut.dart';
公共 API
/**
* 绑定别名功能: 后台可以根据别名进行推送
*
* @param alias 别名字符串
* @param aSn 绑定序列码, Android中无效,仅在iOS有效
*/
bindAlias(alias, sn);
unbindAlias(alias, sn);
/**
* 给用户打标签, 后台可以根据标签进行推送
*
* @param tags 别名数组
*/
setTag(tags);
/**
* 停止SDK服务
*
*/
turnOffPush();
/**
* 开启SDK服务
*
*/
turnOnPush();
回调方法
Getuiflut().addEventHandler(
// 注册收到 cid 的回调
onReceiveClientId: (String message) async {
print("flutter onReceiveClientId: $message");
setState(() {
_getClientId = "ClientId: $message";
});
},
// 注册 DeviceToken 回调
onRegisterDeviceToken: (String message) async {
setState(() {
_getDeviceToken = "DeviceToken: $message";
});
},
// SDK收到透传消息回调
onReceivePayload: (Map<String, dynamic> message) async {
setState(() {
_onReceivePayload = "$message";
});
},
// 点击通知回调
onReceiveNotificationResponse: (Map<String, dynamic> message) async {
setState(() {
_onReceiveNotificationResponse = "$message";
});
},
// APPLink中携带的透传payload信息
onAppLinkPayload: (String message) async {
setState(() {
_onAppLinkPayLoad = "$message";
});
},
// 通知服务开启\关闭回调
onPushModeResult: (Map<String, dynamic> message) async {
print("flutter onPushModeResult: $message");
},
// SetTag回调
onSetTagResult: (Map<String, dynamic> message) async {
print("flutter onSetTagResult: $message");
},
// 设置别名回调
onAliasResult: (Map<String, dynamic> message) async {
print("flutter onAliasResult: $message");
},
// 查询别名回调
onQueryTagResult: (Map<String, dynamic> message) async {
print("flutter onQueryTagResult: $message");
},
// APNs通知即将展示回调
onWillPresentNotification: (Map<String, dynamic> message) async {
print("flutter onWillPresentNotification: $message");
},
// APNs通知设置跳转回调
onOpenSettingsForNotification: (Map<String, dynamic> message) async {
print("flutter onOpenSettingsForNotification: $message");
},
// 授权回调
onGrantAuthorization: (String granted) async {
print("flutter onGrantAuthorization: $granted");
},
);
3.2、Android API
/**
* 初始化个推sdk
*/
Getuiflut.initGetuiSdk();
/**
* 设置角标
*/
setBadge(badge);
3.3、iOS API
GTSDK <= 2.4.6.0 版本,需要使用插件版本 <= 0.2.5
GTSDK > 2.4.6.0 版本,需要使用最新插件版本
/**
* 启动sdk+通知授权
*
*/
startSdk(appId, appKey, appSecret);
/**
* 启动sdk
*
*/
startSdkSimple(appId, appKey, appSecret);
/**
* 通知授权, 需要先启动sdk
*
*/
registerRemoteNotification(appId, appKey, appSecret);
/**
* 获取冷启动Apns参数
*
*/
getLaunchNotification();
/**
* 同步服务端角标
*
*/
setBadge(badge);
/**
* 复位服务端角标
*
*/
resetBadge();
/**
* 同步App本地角标
*
*/
setLocalBadge(badge);
示例代码
以下是完整的示例代码,展示如何使用 getuiflut_noifa
插件:
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:getuiflut/getuiflut.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _payloadInfo = 'Null';
String _userMsg = "";
String _notificationState = "";
String _getClientId = "";
String _getDeviceToken = "";
String _onReceivePayload = "";
String _onReceiveNotificationResponse = "";
String _onAppLinkPayLoad = "";
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
String platformVersion;
String payloadInfo = "default";
String notificationState = "default";
try {
platformVersion = await Getuiflut.platformVersion;
print('platformVersion' + platformVersion);
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
_payloadInfo = payloadInfo;
_notificationState = notificationState;
});
Getuiflut().addEventHandler(
onReceiveClientId: (String message) async {
print("flutter onReceiveClientId: $message");
setState(() {
_getClientId = "ClientId: $message";
});
},
onRegisterDeviceToken: (String message) async {
print("flutter onRegisterDeviceToken: $message");
setState(() {
_getDeviceToken = "$message";
});
},
onReceivePayload: (Map<String, dynamic> message) async {
print("flutter onReceivePayload: $message");
setState(() {
_onReceivePayload = "$message";
});
},
onReceiveNotificationResponse: (Map<String, dynamic> message) async {
print("flutter onReceiveNotificationResponse: $message");
setState(() {
_onReceiveNotificationResponse = "$message";
});
},
onAppLinkPayload: (String message) async {
print("flutter onAppLinkPayload: $message");
setState(() {
_onAppLinkPayLoad = "$message";
});
},
);
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: ListView(
children: <Widget>[
Container(
alignment: Alignment.center,
child: Column(
children: <Widget>[
Text('platformVersion: $_platformVersion\n'),
Text('clientId: $_getClientId\n'),
ElevatedButton(
onPressed: () {
Getuiflut().startSdk(
appId: "your-app-id",
appKey: "your-app-key",
appSecret: "your-app-secret",
);
},
child: const Text('Start SDK'),
),
ElevatedButton(
onPressed: () {
Getuiflut().turnOffPush();
},
child: const Text('Stop Push'),
),
ElevatedButton(
onPressed: () {
Getuiflut().turnOnPush();
},
child: const Text('Resume Push'),
),
ElevatedButton(
onPressed: () {
Getuiflut().bindAlias('test', 'test');
},
child: const Text('Bind Alias'),
),
ElevatedButton(
onPressed: () {
Getuiflut().unbindAlias('test', 'test');
},
child: const Text('Unbind Alias'),
),
ElevatedButton(
onPressed: () {
Getuiflut().setTag(['tag1', 'tag2']);
},
child: const Text('Set Tag'),
),
],
),
),
],
),
),
);
}
}
更多关于Flutter通知推送插件getuiflut_noifa的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter通知推送插件getuiflut_noifa的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
getuiflut_noifa
是一个用于在 Flutter 应用中集成个推(Getui)推送服务的插件。个推是国内常用的推送服务之一,支持 Android 和 iOS 平台。通过 getuiflut_noifa
插件,开发者可以方便地在 Flutter 应用中实现消息推送功能。
以下是使用 getuiflut_noifa
插件的基本步骤:
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 getuiflut_noifa
插件的依赖:
dependencies:
flutter:
sdk: flutter
getuiflut_noifa: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 配置 Android 和 iOS 项目
Android 配置
在 android/app/build.gradle
文件中,确保 minSdkVersion
至少为 16:
defaultConfig {
applicationId "com.example.yourapp"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
}
在 AndroidManifest.xml
文件中添加必要的权限和服务:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".MyApplication"
android:label="YourApp"
android:icon="@mipmap/ic_launcher">
<service
android:name="com.igexin.sdk.PushService"
android:exported="true"
android:process=":pushservice" />
<receiver android:name="com.igexin.sdk.PushReceiver">
<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" />
</intent-filter>
</receiver>
</application>
</manifest>
iOS 配置
在 ios/Runner/Info.plist
文件中添加以下内容:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>
3. 初始化个推服务
在 Flutter 应用的 main.dart
文件中初始化个推服务:
import 'package:flutter/material.dart';
import 'package:getuiflut_noifa/getuiflut_noifa.dart';
void main() {
runApp(MyApp());
initGetui();
}
void initGetui() {
GetuiflutNoifa.initGetuiSdk(
appId: "your_app_id",
appKey: "your_app_key",
appSecret: "your_app_secret",
);
GetuiflutNoifa.onPushMessageReceived.listen((message) {
print("Received push message: $message");
});
GetuiflutNoifa.onRegisterClientId.listen((clientId) {
print("Client ID: $clientId");
});
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Getui Push Demo"),
),
body: Center(
child: Text("Hello, Getui Push!"),
),
);
}
}