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

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

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

安装jpush_flutter插件

git文档地址

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

    defaultConfig中添加以下代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    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", //暂时填写默认值即可.
    ]

    应用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
      
    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;
    });
    });

    }),
    ]
    )
    ),
    ),
    );
    }
    }