Flutter 极光推送android实现

 

Flutter集成极光推送

目前众多推送厂家只有极光支持了flutter,支持一下!!!
废话不多说,开始撸代码

第一步

因为设备的原因目前只在安卓上测试成功,就先分享安卓的配置过程,首先在极光官网创建应用,完成之后在 android/app/build.gradle文件下添加配置:
















 






manifestPlaceholders = [
        JPUSH_PKGNAME : "您的应用包名",
        JPUSH_APPKEY : "38a101c09adf57ef70eda9be", // NOTE: JPush 上注册的包名对应的 Appkey.
        JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
]
  • 第二步

老规矩,添加依赖

dependencies:
  flutter:
    sdk: flutter
    flutter_jpush: ^0.0.4
  • 第三步
  • import 'package:flutter/material.dart';
    import 'package:flutter_jpush/flutter_jpush.dart';
    import 'jpushpage.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
    
      @override
      void initState() {
        // TODO: implement initState
        super.initState();
    
        _startupJpush();
        _ReceiveNotification();
        _OpenNotification();
        _ReceiveCustomMsg();
      }
    
      void _startupJpush() async {
        print("初始化jpush");
        await FlutterJPush.startup();
        print("初始化jpush成功");
      }
    
      /*
    * 收到推送提醒
    * */
      void _ReceiveNotification() async {
        FlutterJPush.addReceiveNotificationListener(
                (JPushNotification notification) {
              setState(() {
                /// 收到推送
                print("收到推送提醒: $notification");
              });
            });
      }
    
      /*
      * 监听接收自定义消息
      * */
    
      void _ReceiveCustomMsg() async {
        FlutterJPush.addReceiveCustomMsgListener((JPushMessage msg) {
          setState(() {
            print("收到推送消息提醒: $msg");
          });
        });
      }
    
      /*
      * 打开推送提醒
      * */
    
      void _OpenNotification() async {
        FlutterJPush.addReceiveOpenNotificationListener(
                (JPushNotification notification) {
              setState(() {
                print("打开了推送提醒: $notification");
                Navigator.push(context,MaterialPageRoute(builder: (context){
                  return jpushPage();
                })).then((String value){
    
                });
              });
            });
      }
    
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
    
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                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),
          ),
        );
      }
    }
    

 

你可能感兴趣的:(Flutter 极光推送android实现)