main.dart 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. });
  32. FlutterAliyunPush.reigistOnReceiveNotification((msg){
  33. platformVersion = msg.title;
  34. });
  35. // If the widget was removed from the tree while the asynchronous platform
  36. // message was in flight, we want to discard the reply rather than calling
  37. // setState to update our non-existent appearance.
  38. if (!mounted) return;
  39. setState(() {
  40. _platformVersion = platformVersion;
  41. });
  42. }
  43. @override
  44. Widget build(BuildContext context) {
  45. return MaterialApp(
  46. home: Scaffold(
  47. appBar: AppBar(
  48. title: const Text('Plugin example app'),
  49. ),
  50. body: Center(
  51. child: Text('Running on: $_platformVersion\n'),
  52. ),
  53. ),
  54. );
  55. }
  56. }