官网文档
https://github.com/TencentCloud/TPNS-Flutter-Plugin
- 添加插件:
$ flutter tpns_flutter_plugin
- 配置 android\app\build.gradle
android: {
....
defaultConfig {
applicationId "替换成自己应用 ID" //申请的时包名
...
ndk {
/// 选择要添加的对应.so 库。
abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64', 'arm64-v8a',
}
// 注意是 +号
manifestPlaceholders += [
XG_ACCESS_ID : "替换自己的ACCESS_ID", // 信鸽官网注册所得ACCESS_ID
XG_ACCESS_KEY : "替换自己的ACCESS_KEY", // 信鸽官网注册所得ACCESS_KEY
]
}
}
- 代码初始化
import 'package:flutter/material.dart';
import 'package:tpns_flutter_plugin/tpns_flutter_plugin.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final XgFlutterPlugin tpush = XgFlutterPlugin();
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
if (!mounted) return;
tpush.setEnableDebug(true);
tpush.addEventHandler(
onRegisteredDeviceToken: (String msg) async {
print("flutter onRegisteredDeviceToken: $msg");
print("=====1==============");
},
onRegisteredDone: (String msg) async {
print("flutter onRegisteredDone: $msg");
print("注册成功");
print("=====2==============");
},
unRegistered: (String msg) async {
print("flutter unRegistered: $msg");
print(msg);
print("=====3==============");
},
onReceiveNotificationResponse: (Map<String, dynamic> msg) async {
print("flutter onReceiveNotificationResponse $msg");
print("=====4==============");
},
onReceiveMessage: (Map<String, dynamic> msg) async {
print("flutter onReceiveMessage $msg");
print("=====5==============");
},
xgPushDidSetBadge: (String msg) async {
print("flutter xgPushDidSetBadge: $msg");
print("=====6==============");
print(msg);
},
xgPushDidBindWithIdentifier: (String msg) async {
print("flutter xgPushDidBindWithIdentifier: $msg");
print(msg);
print("=====7==============");
},
xgPushDidUnbindWithIdentifier: (String msg) async {
print("flutter xgPushDidUnbindWithIdentifier: $msg");
print(msg);
print("=====8==============");
},
xgPushDidUpdatedBindedIdentifier: (String msg) async {
print("flutter xgPushDidUpdatedBindedIdentifier: $msg");
print(msg);
print("=====9==============");
},
xgPushDidClearAllIdentifiers: (String msg) async {
print("flutter xgPushDidClearAllIdentifiers: $msg");
print(msg);
print("=====10==============");
},
xgPushClickAction: (Map<String, dynamic> msg) async {
print("flutter xgPushClickAction $msg");
print("clickResponse");
print("=====11==============");
print(msg["title"] + "," + msg["content"]);
print("点击了推送,在这里可以做页面跳转判断");
print("=====11==============");
},
);
tpush.startXg("xxx", "xxx");
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(),
);
}
}
flutter内置 图标网:
https://www.fluttericon.cn