flutter项目中极光推送插件jpush_flutter使用

3,003 阅读1分钟

一. 注册极光账号,并配置

  • 极光网站进行注册

  • 创建应用 填入应用名称和上传图片,生成应用

  • 点击红色箭头应用设置,进入开发者服务页面

  • 推送设置,配送推送的平台 Android,填入包名(注意这个填了之后不能改)

  • 左侧导航,到极光推送页面,发送通知页面

创建发送信息,点击发送预览,即可发送

二、flutter项目端配置

安装官网安装配置

github.com/jpush/jpush…

pub.dev/packages/jp…

三、flutter项目代码

main.dart代码,注意addEventHandler一定要在setup前面

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

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: JpushPage(),
    );
  }
}

class JpushPage extends StatefulWidget {
  JpushPage({Key key}) : super(key: key);

  _JpushPageState createState() => _JpushPageState();
}

class _JpushPageState extends State<JpushPage> {
  JPush jpush = new JPush();

  @override
  void initState() {
    super.initState();
    _initJpush();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("极光推送demo"),
      ),
      body: Column(
        children: <Widget>[
          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((message) {
                print("flutter onReceiveNotification: $message");
              });
            }),
        ],
      )
    );
  }

  //监听极光推送 (自定义的方法)
  //https://github.com/jpush/jpush-flutter-plugin/blob/master/documents/APIs.md
  Future<void> _initJpush() async {
    try {
      //监听消息通知
      jpush.addEventHandler(
        // 接收通知回调方法。
        onReceiveNotification: (Map<String, dynamic> message) async {
          print("flutter onReceiveNotification: $message");
        },
        // 点击通知回调方法。
        onOpenNotification: (Map<String, dynamic> message) async {
          print("flutter onOpenNotification: $message");
        },
        // 接收自定义消息回调方法。
        onReceiveMessage: (Map<String, dynamic> message) async {
          print("flutter onReceiveMessage: $message");
        },
      );
    } catch(error){
      print('平台版本获取失败,请检查!');
    }
    //初始化
    jpush.setup(
      appKey: "926f98023d3f9f209f7f9515",
      channel: "theChannel",
      production: false,
      debug: true, // 设置是否打印 debug 日志
    );
    //获取注册的id
    jpush.getRegistrationID().then((rid) {
      print("获取注册的id:$rid");
    });
    //设置别名  实现指定用户推送
    // jpush.setAlias("jg123").then((map) {
    //   print("设置别名成功");
    // });
  }
}

效果图