mobpush_plugin 1.0.3 copy "mobpush_plugin: ^1.0.3" to clipboard
mobpush_plugin: ^1.0.3 copied to clipboard

outdated

A new flutter plugin project.

example/lib/main.dart

import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:mobpush_plugin/mobpush_plugin.dart';
import 'package:mobpush_plugin/mobpush_custom_message.dart';
import 'package:mobpush_plugin/mobpush_notify_message.dart';
import 'package:mobpush_plugin_example/app_notify_page.dart';
import 'package:mobpush_plugin_example/click_container.dart';
import 'package:mobpush_plugin_example/local_notify_page.dart';
import 'package:mobpush_plugin_example/notify_page.dart';
import 'package:mobpush_plugin_example/other_api_page.dart';
import 'package:mobpush_plugin_example/timing_notify_page.dart';


void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: MainApp(),
    );
  }
}

class MainApp extends StatefulWidget {
  @override
  _MainAppState createState() {
    return _MainAppState();
  }
}

class _MainAppState extends State<MainApp> {
  String _platformVersion = 'Unknown';
  String _registrationId = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
    if (Platform.isIOS) {
      MobpushPlugin.setCustomNotification();
      MobpushPlugin.setAPNsForProduction(false);
    }
    MobpushPlugin.addPushReceiver(_onEvent, _onError);
  }

  void _onEvent(Object event) {
    print('>>>>>>>>>>>>>>>>>>>>>>>>>>>onEvent:' + event.toString());
    setState(() {
      Map<String, dynamic> eventMap = json.decode(event);
      String result = eventMap['result'];
      int action = eventMap['action'];
      switch (action) {
        case 0:
          MobPushCustomMessage message = new MobPushCustomMessage.fromJson(json.decode(result));
          showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                content: Text(message.content),
                actions: <Widget>[
                  FlatButton(
                    child: Text('OK'),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  )
                ],
              );
            }
          );
          break;
        case 1:
          MobPushNotifyMessage message = new MobPushNotifyMessage.fromJson(json.decode(result));
          showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text(message.title),
                content: Text(message.content),
                actions: <Widget>[
                  FlatButton(
                    child: Text('OK'),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  )
                ],
              );
            }
          );
        break;
        case 2:
          MobPushNotifyMessage message = new MobPushNotifyMessage.fromJson(json.decode(result));
          showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text(message.title),
                content: Text(message.content),
                actions: <Widget>[
                  FlatButton(
                    child: Text('OK'),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  )
                ],
              );
            }
          );
        break;
        case 3:
          List tags = eventMap['tags'];
          int operation = eventMap['operation'];
          int errorCode = eventMap['errorCode'];
          String errorDesc = eventMap['errorDesc'];
          print("tag:>>>>> $tags");
          print("tags callback: ${operation == 0? "获取": operation==1?"设置" : "删除" }别名${errorCode==0?"成功":"失败"}");
        break;
        case 4:
          String alias = eventMap['alias'];
          int operation = eventMap['operation'];
          int errorCode = eventMap['errorCode'];
          print("alias:>>>>>$alias");
          print("alias callback: ${operation == 0? "获取":operation==1?"设置":"删除"}别名${errorCode==0?"成功":"失败"}");
        break;
        default:
        break;
      }
    });
  }

  void _onError(Object event) {
    setState(() {
      print('>>>>>>>>>>>>>>>>>>>>>>>>>>>onError:' + event.toString());
    });
  }

  void _onAppNotifyPageTap() {
    print('###########ssssss');
    setState(() {
      Navigator.push(
        context,
        new MaterialPageRoute(builder: (context) => new AppNotifyPage())
      );
    });
  }

  void _onNotifyPageTap() {
    setState(() {
      Navigator.push(
        context, 
        new MaterialPageRoute(builder: (context) => new NotifyPage())
      );
    });
  }

  void _onTimingNotifyPageTap() {
    setState(() {
      Navigator.push(
        context, new MaterialPageRoute(builder: (context) => new TimingNotifyPage())
      );
    });
  }

  void _onLocalNotifyPageTap() {
    setState(() {
      Navigator.push(
        context, 
        new MaterialPageRoute(builder: (context) => new LocalNotifyPage())
      );
    });
  }

  void _onOtherAPITap() {
    setState(() {
      Navigator.push(
        context,
        new MaterialPageRoute(builder: (context) => new OtherApiPage())
      );
    });
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    String registrationId;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await MobpushPlugin.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }
    try {
      registrationId = await MobpushPlugin.getRegistrationId();
      print('------>#### registrationId: ' + registrationId);
    } on PlatformException {
      registrationId = 'Failed to get registrationId.';
    }

    // 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(() {
      _platformVersion = platformVersion;
      _registrationId = registrationId;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('MobPushPlugin example app'),
        ),
        body: Column(
          children: <Widget>[
            Expanded(
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: ClickContainer(
                      content: 'App内推送',
                      res: 'assets/images/ic_item_app_nitify.png',
                      left: 15.0,
                      top: 15.0,
                      right: 7.5,
                      bottom: 7.5,
                      onTap: _onAppNotifyPageTap,
                    ),
                  ),
                  Expanded(
                    child: ClickContainer(
                      content: '通知',
                      res: 'assets/images/ic_item_notify.png',
                      left: 7.5,
                      top: 15.0,
                      right: 15.0,
                      bottom: 7.5,
                      onTap: _onNotifyPageTap,
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: ClickContainer(
                      content: '定时推送',
                      res: 'assets/images/ic_item_timing.png',
                      left: 15.0,
                      top: 7.5,
                      right: 7.5,
                      bottom: 7.5,
                      onTap: _onTimingNotifyPageTap,
                    ),
                  ),
                  Expanded(
                    child: ClickContainer(
                      content: '本地通知',
                      res: 'assets/images/ic_item_local.png',
                      left: 7.5,
                      top: 7.5,
                      right: 15.0,
                      bottom: 7.5,
                      onTap: _onLocalNotifyPageTap,
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: ClickContainer(
                      content: '其他API接口',
                      res: 'assets/images/ic_item_media.png',
                      left: 15.0,
                      top: 7.5,
                      right: 15.0,
                      bottom: 7.5,
                      onTap: _onOtherAPITap,
                    ),
                  ),
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.all(15.0),
              height: 40,
              color: Colors.yellow,
              child: Row(
                children: <Widget>[
                  Text(
                    'Running on: $_platformVersion\nRegistrationId: $_registrationId',
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
25
likes
0
pub points
82%
popularity

Publisher

unverified uploader

A new flutter plugin project.

Homepage

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on mobpush_plugin