main.dart 2.0 KB

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