Flutter - 本地推送
2020-02-13 本文已影响0人
神灬渐入嘉靜
使用库:flutter_local_notifications
在pubspec.yaml中导入:(由于JPush有bug 暂时注释 等待修复)
dependencies:
# jpush_flutter: 0.0.8
# jpush_flutter:
# git:
# url: git://github.com/jpush/jpush-flutter-plugin.git
# ref: master
#本地推送
flutter_local_notifications: ^1.1.5+1
示例代码:
import 'package:flutter/material.dart';
void main() => runApp(LocalNotification());
//本地推送
// import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class LocalNotification extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: LocalNotificationPage(title: 'Flutter Demo Home Page'),
);
}
}
class LocalNotificationPage extends StatefulWidget {
LocalNotificationPage({Key key, this.title}) : super(key: key);
final String title;
@override
LocalNotificationPageState createState() => LocalNotificationPageState();
}
class LocalNotificationPageState extends State<LocalNotificationPage> {
int _counter = 0;
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
new FlutterLocalNotificationsPlugin();
// JPush jpush = new JPush();
void _incrementCounter() async {
// var fireDate = DateTime.fromMillisecondsSinceEpoch(
// DateTime.now().millisecondsSinceEpoch);
// var localNotification = LocalNotification(
// id: 234,
// title: '测试本地推送',
// buildId: 1,
// content: '我是本地推送的消息',
// fireTime: fireDate,
// subtitle: 'ios 消息推送', // 该参数只有在 iOS 有效
// badge: 5, // 该参数只有在 iOS 有效
// extra: {"fa": "0"} // 设置 extras ,extras 需要是 Map<String, String>
// );
// jpush.sendLocalNotification(localNotification).then((res) {});
var scheduledNotificationDateTime =
new DateTime.now().add(new Duration(seconds: 5));
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'your other channel id',
'your other channel name',
'your other channel description');
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
NotificationDetails platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.schedule(
0,
'scheduled title',
'scheduled body',
scheduledNotificationDateTime,
platformChannelSpecifics);
setState(() {
_counter++;
});
}
Future onDidReceiveLocalNotification(
int i, String payload, String payloa3, String payload2) async {
print('onDidReceiveLocalNotification');
}
Future onSelectNotification(String payload) async {
print('onSelectNotification');
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
// await Navigator.push(
// context,
// new MaterialPageRoute(builder: (context) => new SecondScreen(payload)),
// );
}
@override
Widget build(BuildContext context) {
var initializationSettingsAndroid =
new AndroidInitializationSettings('app_icon');
var initializationSettingsIOS = IOSInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveLocalNotification);
var initializationSettings = InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
// jpush.setup(
// appKey: "替换成你自己的 appKey",
// channel: "theChannel",
// production: false,
// debug: false, // 设置是否打印 debug 日志
// );
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}