# flutter_jpush_demo **Repository Path**: LiuLinXi/flutter_jpush_demo ## Basic Information - **Project Name**: flutter_jpush_demo - **Description**: 极光推送本地推送使用Demo - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-05-11 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # flutter_jpush_demo # 极光推送本地使用Demo ## 1.获得Key 在[极光推送官网](https://www.jiguang.cn/)注册账号并创建应用获得Key - AppKey : 移动客户端使用的key - Master Secret : 服务端使用的key ## 2.依赖配置 插件GitHub:https://github.com/jpush/jpush-flutter-plugin ### 安装 在工程 pubspec.yaml 中加入 dependencies ```dart dependencies: jpush_flutter: 0.0.11 ``` ### 配置 ##### Android: 在 `/android/app/build.gradle` 中添加下列代码: ```java android: { .... defaultConfig { applicationId "替换成自己应用 ID" ... ndk { //选择要添加的对应 cpu 类型的 .so 库。 abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64', 'arm64-v8a', } manifestPlaceholders = [ JPUSH_PKGNAME : applicationId, JPUSH_APPKEY : "appkey", // NOTE: JPush 上注册的包名对应的 Appkey. JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可. ] } } ``` ##### iOS: - 在 xcode8 之后需要点开推送选项: TARGETS -> Capabilities -> Push Notification 设为 on 状态 ## 3.使用 ```dart import 'package:jpush_flutter/jpush_flutter.dart'; ``` 需要先调用 JPush.setup 来初始化插件,才能保证其他功能正常工作。[API]() ```dart String debugLabel = 'Unknown'; final JPush jpush = new JPush(); Future initJPushPlatformState() async { String platformVersion; try { jpush.addEventHandler( // 接收通知回调方法。 onReceiveNotification: (Map message) async { print("flutter onReceiveNotification: $message"); //接收推送的信息 setState(() { debugLabel = '接收到推送:$message'; }); }, // 点击通知回调方法。 onOpenNotification: (Map message) async { print("flutter onOpenNotification: $message"); }, // 接收自定义消息回调方法。 onReceiveMessage: (Map message) async { print("flutter onReceiveMessage: $message"); }, ); } catch (PlatformException) { platformVersion = '平台版本获取失败,请检查配置'; } jpush.setup( //前面获取的AppKey appKey: "01c42f57eae5403abebbd8f9", channel: "theChannel", production: false, debug: false, // 设置是否打印 debug 日志 ); //无错误返回 if (!mounted) return; //有错误则将标签设置为错误信息 setState(() { debugLabel = platformVersion; }); } @override void initState() { super.initState(); //初始化极光推送平台 initJPushPlatformState(); } ``` ```dart //点击3秒后发送的时间戳,这里使用DateTime获取当前时间戳,+3000毫秒也就是3秒后的时间戳 var fireDate = DateTime.fromMillisecondsSinceEpoch( DateTime.now().millisecondsSinceEpoch + 3000); //本地的一个推送 var localNotification = LocalNotification( id: 234, title: '推送标题', content: '推送的具体内容,看到说明已经推送成功', buildId: 1, fireTime: fireDate, subtitle: '一个副标题', ); //使用极光推送本地推送 jpush.sendLocalNotification(localNotification).then((res) { setState(() { //debugeLable是用来接收推送消息的测试String debugLabel = res; }); }); ```