简单学习使用,记录一下过程,实际生产环境中,配合服务一起使用推送

使用极光推送插件前的准备工作

  1. 注册极光开发者服务账号密码 点击注册
  2. 创建应用
  3. 获取到AppKey和Master Secret

安装jpush_flutter插件

git文档地址

    • 查看文档,在pubspec.yaml添加最新版本依赖
    dependencies:
     jpush_flutter: 0.5.1
    • 打开项目下andriod包下面的app目录下的build.gradle文件

    image.png
    defaultConfig中添加以下代码

    ndk {  
        //选择要添加的对应 cpu 类型的 .so 库。  
      abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64', 'arm64-v8a'  
    }  
      
    manifestPlaceholders = [  
            JPUSH_PKGNAME : applicationId,  
            JPUSH_APPKEY : "ee83a3c272f13f77469b57b3", // NOTE: JPush 上注册的包名对应的 Appkey.  
      JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.  
    ]

    应用

      
    import 'package:flutter/material.dart';  
    import 'dart:async';  
      
    import 'package:flutter/services.dart';  
    import 'package:jpush_flutter/jpush_flutter.dart';  
      
    void main() => runApp(new JPushPage());  
      
    class JPushPage extends StatefulWidget {  
      @override  
      _JPushPageState createState() => new _JPushPageState();  
    }  
      
    class _JPushPageState extends State<JPushPage> {  
      String debugLable;   //错误信息  
      final JPush jpush = new JPush();  //初始化极光插件  
      @override  
      void initState() {  
        super.initState();  
        initPlatformState();  //极光插件平台初始化  
      }  
        
        
      Future<void> initPlatformState() async {  
        String platformVersion = 'hello';  
          
        try {  
          //监听响应方法的编写  
      jpush.addEventHandler(  
              onReceiveNotification: (Map<String, dynamic> message) async {  
                print(">>>>>>>>>>>>>>>>>flutter 接收到推送: $message");  
                setState(() {  
                  debugLable = "接收到推送: $message";  
                });  
              }  
          );  
            
        } on PlatformException {  
          platformVersion = '平台版本获取失败,请检查!';  
        }  
          
          
        if (!mounted) return;  
          
        setState(() {  
          debugLable = platformVersion;  
        });  
      }  
        
        
        
      // 编写视图  
      @override  
      Widget build(BuildContext context) {  
        return new MaterialApp(  
          home: new Scaffold(  
            appBar: new AppBar(  
              title: const Text('极光推送'),  
            ),  
            body: new Center(  
                child: new Column(  
                    children:[  
                      new Text('结果: $debugLable\\n'),  
                      new FlatButton(  
                          child: new Text('发送推送消息\\n'),  
                          onPressed: () {  
                            // 三秒后出发本地推送  
      var fireDate = DateTime.fromMillisecondsSinceEpoch(DateTime.now().millisecondsSinceEpoch + 3000);  
                            var localNotification = LocalNotification(  
                              id: 234,  
                              title: '王某的飞鸽传说',  
                              buildId: 1,  
                              content: '看到了说明已经成功了',  
                              fireTime: fireDate,  
                              subtitle: '一个测试',  
                            );  
                            jpush.sendLocalNotification(localNotification).then((res) {  
                              setState(() {  
                                debugLable = res;  
                              });  
                            });  
                              
                          }),  
                    ]  
                )  
            ),  
          ),  
        );  
      }  
    }

    MangoGoing
    774 声望1.2k 粉丝

    开源项目:详见个人详情