极光push使用流程(记录)

一、到极光后台注册应用

登录极光网页的服务中心,选择最下面的应用设置,
填写应用的appid,注册应用
在这里插入图片描述
注册之后获得AppKey
在这里插入图片描述

二、更改build.gradle文件

在这里插入图片描述
在app/src/build.gradle文件的defaultConfig 中添加下面代码

		/*新加入的*/
        ndk {
            abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64'// 'arm64-v8a',
        }

        manifestPlaceholders = [
            JPUSH_PKGNAME: applicationId,
            JPUSH_APPKEY : "bfbb3108f9555cd0b3d96a74", /*NOTE: JPush 上注册的包名对应的 Appkey.*/
            JPUSH_CHANNEL: "developer-default", /*暂时填写默认值即可.*/
        ]
        /*新加入的*/  

三、yaml文件中引入极光pub集成

在这里插入图片描述

四、在main程序文件入口中初始化极光平台插件

String debugLable = 'Unknown'; /*错误信息*/
  final JPush jpush = new JPush(); /* 初始化极光插件*/
  @override
  void initState() {
    super.initState();
    initPlatformState(); /*极光插件平台初始化*/
  }

  Future<void> initPlatformState() async {
    String platformVersion;

    try {
      /*监听响应方法的编写*/
      jpush.addEventHandler(onReceiveNotification: (Map<String, dynamic> message) async {
        print("flutter onReceiveNotification: $message");
        setState(() {
          debugLable = "flutter onReceiveNotification: $message";
        });
      }, onOpenNotification: (Map<String, dynamic> message) async {
        print("flutter onOpenNotification: $message");
        setState(() {
          debugLable = "flutter onOpenNotification: $message";
        });
      }, onReceiveMessage: (Map<String, dynamic> message) async {
        print("flutter onReceiveMessage: $message");
        setState(() {
          debugLable = "flutter onReceiveMessage: $message";
        });
      }, onReceiveNotificationAuthorization: (Map<String, dynamic> message) async {
        print("flutter onReceiveNotificationAuthorization: $message");
        setState(() {
          debugLable = "flutter onReceiveNotificationAuthorization: $message";
        });
      });
    } on PlatformException {
      platformVersion = '平台版本获取失败,请检查!';
    }

    if (!mounted) {
      return;
    }

    setState(() {
      debugLable = platformVersion;
    });
  }

五、编写build视图代码

/*编写视图*/
  @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 RaisedButton(
              child: new Text(
                '点击发送推送消息\n',
              ),
              onPressed: () {
                /*三秒后出发本地推送*/
                var fireDate = DateTime.fromMillisecondsSinceEpoch(DateTime.now().millisecondsSinceEpoch + 3000);
                var localNotification = LocalNotification(
                  id: 234,
                  title: '我是测试极光push的标题',
                  buildId: 1,
                  content: '看到了说明已经成功了',
                  fireTime: fireDate,
                  subtitle: '真的只是个测试啊',
                );
                jpush.sendLocalNotification(localNotification).then((res) {
                  setState(() {
                    debugLable = res;
                  });
                });
              }),
        ])),
      ),
    );
  }

六、main文件完整代码

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

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String debugLable = 'Unknown'; /*错误信息*/
  final JPush jpush = new JPush(); /* 初始化极光插件*/
  @override
  void initState() {
    super.initState();
    initPlatformState(); /*极光插件平台初始化*/
  }

  Future<void> initPlatformState() async {
    String platformVersion;

    try {
      /*监听响应方法的编写*/
      jpush.addEventHandler(onReceiveNotification: (Map<String, dynamic> message) async {
        print("flutter onReceiveNotification: $message");
        setState(() {
          debugLable = "flutter onReceiveNotification: $message";
        });
      }, onOpenNotification: (Map<String, dynamic> message) async {
        print("flutter onOpenNotification: $message");
        setState(() {
          debugLable = "flutter onOpenNotification: $message";
        });
      }, onReceiveMessage: (Map<String, dynamic> message) async {
        print("flutter onReceiveMessage: $message");
        setState(() {
          debugLable = "flutter onReceiveMessage: $message";
        });
      }, onReceiveNotificationAuthorization: (Map<String, dynamic> message) async {
        print("flutter onReceiveNotificationAuthorization: $message");
        setState(() {
          debugLable = "flutter onReceiveNotificationAuthorization: $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 RaisedButton(
              child: new Text(
                '点击发送推送消息\n',
              ),
              onPressed: () {
                /*三秒后出发本地推送*/
                var fireDate = DateTime.fromMillisecondsSinceEpoch(DateTime.now().millisecondsSinceEpoch + 3000);
                var localNotification = LocalNotification(
                  id: 234,
                  title: '我是测试极光push的标题',
                  buildId: 1,
                  content: '看到了说明已经成功了',
                  fireTime: fireDate,
                  subtitle: '真的只是个测试啊',
                );
                jpush.sendLocalNotification(localNotification).then((res) {
                  setState(() {
                    debugLable = res;
                  });
                });
              }),
        ])),
      ),
    );
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值