jpush_flutter 3.1.2 jpush_flutter: ^3.1.2 copied to clipboard
JIGUANG officially supported JPush Flutter plugin (Android & iOS). 极光推送官方支持的 Flutter 插件(Android & iOS)(https://www.jiguang.cn).
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
String? debugLable = 'Unknown';
final JPush jpush = new JPush();
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String? platformVersion;
try {
jpush.addEventHandler(
onReceiveNotification: (Map<String, dynamic> message) async {
print("flutter onReceiveNotification: $message");
printMap(message);
try {
// 将数据转换为 Map<String, dynamic>
// Map<String, dynamic> safeData = _castMap(data);
// 提取 extras 和 cn.jpush.android.EXTRA 的内容
// var extras = _castMap(data['extras']);
// var extraDetails = _castMap(extras['cn.jpush.android.EXTRA']);
// var extras = data['extras'];
// var extraDetails = extras['cn.jpush.android.EXTRA'];
//
// // 打印提取到的信息
// print('Alert: ${data['alert']}');
// print('Title: ${data['title']}');
// print('Alert Type: ${extras['cn.jpush.android.ALERT_TYPE']}');
// print(
// 'Notification ID: ${extras['cn.jpush.android.NOTIFICATION_ID']}');
// print('Message ID: ${extras['cn.jpush.android.MSG_ID']}');
// print('Extra Alert: ${extras['cn.jpush.android.ALERT']}');
//
// // 提取 cn.jpush.android.EXTRA 的信息
// print('Type from EXTRA: ${extraDetails['type']}');
// print('通知通知key from EXTRA: ${extraDetails['通知通知key']}');
// 打印所有值
print('alert alert: ${message['alert']}');
print('title: ${message['title']}');
print(
'ALERT_TYPE: ${message['extras']?['cn.jpush.android.ALERT_TYPE']}');
print(
'NOTIFICATION_ID: ${message['extras']?['cn.jpush.android.NOTIFICATION_ID']}');
print('MSG_ID: ${message['extras']?['cn.jpush.android.MSG_ID']}');
print('Alert: ${message['extras']?['cn.jpush.android.ALERT']}');
print(
'type: ${message['extras']?['cn.jpush.android.EXTRA']?['type']}');
print(
'通知通知key: ${message['extras']?['cn.jpush.android.EXTRA']?['通知通知key']}');
} catch (e) {
print('Error occurred while extracting data: $e');
}
setState(() {
debugLable = "flutter onReceiveNotification: $message";
});
}, onOpenNotification: (Map<String, dynamic> message) async {
print("flutter onOpenNotification: $message");
printMap(message);
setState(() {
debugLable = "flutter onOpenNotification: $message";
});
}, onReceiveMessage: (Map<String, dynamic> message) async {
print("flutter onReceiveMessage: $message");
setState(() {
debugLable = "flutter onReceiveMessage: $message";
});
}, onReceiveNotificationAuthorization:
(Map<String, dynamic> message) async {
print("flutter onReceiveNotificationAuthorization: $message");
setState(() {
debugLable = "flutter onReceiveNotificationAuthorization: $message";
});
}, onNotifyMessageUnShow: (Map<String, dynamic> message) async {
print("flutter onNotifyMessageUnShow: $message");
setState(() {
debugLable = "flutter onNotifyMessageUnShow: $message";
});
}, onInAppMessageShow: (Map<String, dynamic> message) async {
print("flutter onInAppMessageShow: $message");
setState(() {
debugLable = "flutter onInAppMessageShow: $message";
});
}, onCommandResult: (Map<String, dynamic> message) async {
print("flutter onCommandResult: $message");
setState(() {
debugLable = "flutter onCommandResult: $message";
});
}, onInAppMessageClick: (Map<String, dynamic> message) async {
print("flutter onInAppMessageClick: $message");
setState(() {
debugLable = "flutter onInAppMessageClick: $message";
});
}, onConnected: (Map<String, dynamic> message) async {
print("flutter onConnected: $message");
setState(() {
debugLable = "flutter onConnected: $message";
});
});
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
jpush.setAuth(enable: true);
jpush.setup(
appKey: "xxxxx", //你自己应用的 AppKey
channel: "theChannel",
production: false,
debug: true,
);
jpush.applyPushAuthority(
new NotificationSettingsIOS(sound: true, alert: true, badge: true));
// Platform messages may fail, so we use a try/catch PlatformException.
jpush.getRegistrationID().then((rid) {
print("flutter get registration id : $rid");
setState(() {
debugLable = "flutter getRegistrationID: $rid";
});
});
// iOS要是使用应用内消息,请在页面进入离开的时候配置pageEnterTo 和 pageLeave 函数,参数为页面名。
jpush.pageEnterTo("HomePage"); // 在离开页面的时候请调用 jpush.pageLeave("HomePage");
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
debugLable = platformVersion;
});
}
// 编写视图
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Plugin example app'),
),
body: new Center(
child: new Column(children: [
Container(
margin: EdgeInsets.fromLTRB(10, 10, 10, 10),
color: Colors.brown,
child: Text(debugLable ?? "Unknown"),
width: 350,
height: 100,
),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(" "),
new CustomButton(
title: "发本地推送",
onPressed: () {
// 三秒后出发本地推送
var fireDate = DateTime.fromMillisecondsSinceEpoch(
DateTime.now().millisecondsSinceEpoch + 3000);
var localNotification = LocalNotification(
id: 234,
title: 'fadsfa',
buildId: 1,
content: 'fdas',
fireTime: fireDate,
subtitle: 'fasf',
badge: 5,
extra: {"fa": "0"});
jpush
.sendLocalNotification(localNotification)
.then((res) {
setState(() {
debugLable = res;
});
});
}),
new Text(" "),
new CustomButton(
title: "getLaunchAppNotification",
onPressed: () {
jpush.getLaunchAppNotification().then((map) {
print("flutter getLaunchAppNotification:$map");
setState(() {
debugLable = "getLaunchAppNotification success: $map";
});
}).catchError((error) {
setState(() {
debugLable = "getLaunchAppNotification error: $error";
});
});
}),
]),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(" "),
new CustomButton(
title: "setTags",
onPressed: () {
jpush.setTags(["lala", "haha"]).then((map) {
var tags = map['tags'];
setState(() {
debugLable = "set tags success: $map $tags";
});
}).catchError((error) {
setState(() {
debugLable = "set tags error: $error";
});
});
}),
new Text(" "),
new CustomButton(
title: "addTags",
onPressed: () {
jpush.addTags(["lala", "haha"]).then((map) {
var tags = map['tags'];
setState(() {
debugLable = "addTags success: $map $tags";
});
}).catchError((error) {
setState(() {
debugLable = "addTags error: $error";
});
});
}),
new Text(" "),
new CustomButton(
title: "deleteTags",
onPressed: () {
jpush.deleteTags(["lala", "haha"]).then((map) {
var tags = map['tags'];
setState(() {
debugLable = "deleteTags success: $map $tags";
});
}).catchError((error) {
setState(() {
debugLable = "deleteTags error: $error";
});
});
}),
]),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(" "),
new CustomButton(
title: "getAllTags",
onPressed: () {
jpush.getAllTags().then((map) {
setState(() {
debugLable = "getAllTags success: $map";
});
}).catchError((error) {
setState(() {
debugLable = "getAllTags error: $error";
});
});
}),
new Text(" "),
new CustomButton(
title: "cleanTags",
onPressed: () {
jpush.cleanTags().then((map) {
var tags = map['tags'];
setState(() {
debugLable = "cleanTags success: $map $tags";
});
}).catchError((error) {
setState(() {
debugLable = "cleanTags error: $error";
});
});
}),
]),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(" "),
new CustomButton(
title: "setAlias",
onPressed: () {
jpush.setAlias("thealias11").then((map) {
setState(() {
debugLable = "setAlias success: $map";
});
}).catchError((error) {
setState(() {
debugLable = "setAlias error: $error";
});
});
}),
new Text(" "),
new CustomButton(
title: "deleteAlias",
onPressed: () {
jpush.deleteAlias().then((map) {
setState(() {
debugLable = "deleteAlias success: $map";
});
}).catchError((error) {
setState(() {
debugLable = "deleteAlias error: $error";
});
});
}),
new Text(" "),
new CustomButton(
title: "getAlias",
onPressed: () {
jpush.getAlias().then((map) {
setState(() {
debugLable = "getAlias success: $map";
});
}).catchError((error) {
setState(() {
debugLable = "getAlias error: $error";
});
});
}),
]),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(" "),
new CustomButton(
title: "stopPush",
onPressed: () {
jpush.stopPush();
}),
new Text(" "),
new CustomButton(
title: "resumePush",
onPressed: () {
jpush.resumePush();
}),
],
),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(" "),
new CustomButton(
title: "clearAllNotifications",
onPressed: () {
jpush.clearAllNotifications();
}),
new Text(" "),
new CustomButton(
title: "setBadge",
onPressed: () {
jpush.setBadge(66).then((map) {
setState(() {
debugLable = "setBadge success: $map";
});
}).catchError((error) {
setState(() {
debugLable = "setBadge error: $error";
});
});
}),
],
),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(" "),
new CustomButton(
title: "通知授权是否打开",
onPressed: () {
jpush.isNotificationEnabled().then((bool value) {
setState(() {
debugLable = "通知授权是否打开: $value";
});
}).catchError((onError) {
setState(() {
debugLable = "通知授权是否打开: ${onError.toString()}";
});
});
}),
new Text(" "),
new CustomButton(
title: "打开系统设置",
onPressed: () {
jpush.openSettingsForNotification();
}),
],
),
])),
),
);
}
void printMap(Map<String, dynamic> map, [String prefix = '']) {
try {
map.forEach((key, value) {
String fullKey = prefix.isEmpty ? key : '$prefix.$key';
if (value is Map) {
printMap(_castMap(value), fullKey);
} else {
// 打印键和值
print('$fullKey: $value');
}
});
} catch (e) {
print("printMap catch e: $e");
}
}
Map<String, dynamic> _castMap(Map<Object?, Object?> original) {
return original.map((key, value) => MapEntry(key.toString(), value));
}
T getValue<T>(Map<String, dynamic> map, String key) {
if (!map.containsKey(key)) {
throw Exception('Key "$key" not found in the map.');
}
var value = map[key];
if (value is T) {
return value; // 如果类型匹配直接返回
} else if (value is String && T == int) {
return int.tryParse(value) as T; // 尝试将 String 转换为 int
} else if (value is String && T == double) {
return double.tryParse(value) as T; // 尝试将 String 转换为 double
} else if (value is num && T == int) {
return value.toInt() as T; // 将 num 转为 int
} else if (value is num && T == double) {
return value.toDouble() as T; // 将 num 转为 double
} else {
throw Exception(
'Value type mismatch for key "$key". Expected type: $T, Found type: ${value.runtimeType}');
}
}
}
/// 封装控件
class CustomButton extends StatelessWidget {
final VoidCallback? onPressed;
final String? title;
const CustomButton({@required this.onPressed, @required this.title});
@override
Widget build(BuildContext context) {
return new TextButton(
onPressed: onPressed,
child: new Text("$title"),
style: new ButtonStyle(
foregroundColor: MaterialStateProperty.all(Colors.white),
overlayColor: MaterialStateProperty.all(Color(0xff888888)),
backgroundColor: MaterialStateProperty.all(Color(0xff585858)),
padding: MaterialStateProperty.all(EdgeInsets.fromLTRB(10, 5, 10, 5)),
),
);
}
}