main.dart 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'package:flutter/services.dart';
  4. import 'package:flutter_aliyun_push/flutter_aliyun_push.dart';
  5. void main() {
  6. runApp(MyApp());
  7. }
  8. class MyApp extends StatefulWidget {
  9. @override
  10. _MyAppState createState() => _MyAppState();
  11. }
  12. class _MyAppState extends State<MyApp> {
  13. String _platformVersion = 'Unknown';
  14. @override
  15. void initState() {
  16. super.initState();
  17. initPlatformState();
  18. }
  19. // Platform messages are asynchronous, so we initialize in an async method.
  20. Future<void> initPlatformState() async {
  21. String platformVersion;
  22. // Platform messages may fail, so we use a try/catch PlatformException.
  23. try {
  24. platformVersion = await FlutterAliyunPush.platformVersion;
  25. // await FlutterAliyunPush.initPush;
  26. } on PlatformException {
  27. platformVersion = 'Failed to get platform version.';
  28. }
  29. FlutterAliyunPush.reigistOnRegistSuccess((msg){
  30. platformVersion = msg;
  31. setState(() {
  32. _platformVersion = platformVersion;
  33. });
  34. });
  35. FlutterAliyunPush.reigistOnReceiveNotification((msg){
  36. platformVersion = msg.title;
  37. setState(() {
  38. _platformVersion = platformVersion;
  39. });
  40. });
  41. FlutterAliyunPush.reigistOnReceiveMessage((msg){
  42. platformVersion = msg.title;
  43. setState(() {
  44. _platformVersion = platformVersion;
  45. });
  46. });
  47. // If the widget was removed from the tree while the asynchronous platform
  48. // message was in flight, we want to discard the reply rather than calling
  49. // setState to update our non-existent appearance.
  50. if (!mounted) return;
  51. setState(() {
  52. _platformVersion = platformVersion;
  53. });
  54. }
  55. @override
  56. Widget build(BuildContext context) {
  57. return MaterialApp(
  58. home: Scaffold(
  59. appBar: AppBar(
  60. title: const Text('Plugin example app'),
  61. ),
  62. body: Center(
  63. child: Text('Running on: $_platformVersion\n'),
  64. ),
  65. ),
  66. );
  67. }
  68. }