Flutter极光推送插件fl_jpush_android的使用

Flutter极光推送插件fl_jpush_android的使用

JPush集成厂商推送通道

如需集成极光推送厂商通道请安装此插件。

具体查看极光原生文档。

  • 此插件仅添加原生SDK依赖和配置 AndroidManifest.xml 以及 consumer-rules.pro 混淆规则,其他配置信息需自行配置。
  • 此插件未集成FCM SDK,需要添加的请查看极光文档。

5.0.0开始无需配置以下内容,请移除

// 集成oppo配置
复制 `example/android/heytap_msp_push` 目录至 `${your project}/android/`
目录下,并在 `${your project}/android/settings.gradle` 中添加 `include ':heytap_msp_push'`

// 集成honor配置
复制 `example/android/hi_push` 目录至 `${your project}/android/`
目录下,并在 `${your project}/android/settings.gradle` 中添加 `include ':hi_push'`

* 不要修改目录和aar名字

添加以下内容至 `android/app/build.gradle` 中
android {
    defaultConfig {
        manifestPlaceholders = [
                JPUSH_PKGNAME : applicationId,
                JPUSH_APPKEY  : '',

                JPUSH_CHANNEL : 'developer-default',
                // 下面是多厂商配置,如需要开通使用请联系技术支持
                // 如果不需要使用,预留空字段即可
                MEIZU_APPKEY  : "MZ-",
                MEIZU_APPID   : "MZ-",
                XIAOMI_APPID  : "MI-",
                XIAOMI_APPKEY : "MI-",
                OPPO_APPKEY   : "OP-",
                OPPO_APPID    : "OP-",
                OPPO_APPSECRET: "OP-",
                VIVO_APPKEY   : "",
                VIVO_APPID    : "",
                HUAWEI_APPID  : "配置文件里的appid",
                HUAWEI_CPID   : "配置文件里的cp_id",
                HONOR_APPID   : "Honor平台注册的APP ID"
        ]
    }
}

完整示例Demo

以下是使用fl_jpush_android插件的一个简单示例。

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.light(useMaterial3: true),
      darkTheme: ThemeData.dark(useMaterial3: true),
      title: 'FlJPush with Android',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  void initState() {
    super.initState();

    // 初始化JPush
    initJPush();
  }

  void initJPush() async {
    // 初始化JPush插件
    await JPush().init();

    // 设置接收消息回调
    JPush().onReceiveNotification.listen((notification) {
      print("Received notification: ${notification}");
    });

    // 设置打开通知回调
    JPush().onOpenNotification.listen((notification) {
      print("Opened notification: ${notification}");
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('FlJPush with Android'),
      ),
      body: const Center(
        child: Text('集成厂商通道'),
      ),
    );
  }
}

更多关于Flutter极光推送插件fl_jpush_android的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter极光推送插件fl_jpush_android的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


fl_jpush_android 是一个用于在 Flutter 应用中集成极光推送(JPush)的插件。它提供了与极光推送 SDK 的 Android 平台的集成,使开发者能够在 Flutter 应用中接收和处理推送通知。

以下是使用 fl_jpush_android 插件的基本步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 fl_jpush_android 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  fl_jpush_android: ^1.0.0  # 请根据实际情况使用最新版本

然后运行 flutter pub get 来安装依赖。

2. 配置 Android 项目

android/app/build.gradle 文件中,确保已经配置了极光推送所需的权限和依赖:

android {
    ...
    defaultConfig {
        ...
        manifestPlaceholders = [
            JPUSH_PKGNAME : applicationId,
            JPUSH_APPKEY : "你的极光推送 AppKey",  // 替换为你的极光推送 AppKey
            JPUSH_CHANNEL : "developer-default",  // 默认渠道
        ]
    }
    ...
}

dependencies {
    ...
    implementation 'cn.jiguang.sdk:jpush:4.6.0'  // 极光推送 SDK
    implementation 'cn.jiguang.sdk:jcore:2.7.0'  // 极光推送核心库
}

3. 初始化极光推送

在 Flutter 应用的 main.dart 文件中,初始化极光推送:

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化极光推送
  await FlJpushAndroid.init(
    appKey: "你的极光推送 AppKey",  // 替换为你的极光推送 AppKey
    channel: "developer-default",
    production: false,  // 是否为生产环境
  );

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('JPush Demo'),
      ),
      body: Center(
        child: Text('JPush Initialized'),
      ),
    );
  }
}

4. 处理推送通知

你可以通过监听极光推送的事件来处理推送通知。例如,在 main.dart 中添加以下代码来监听通知事件:

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化极光推送
  await FlJpushAndroid.init(
    appKey: "你的极光推送 AppKey",  // 替换为你的极光推送 AppKey
    channel: "developer-default",
    production: false,  // 是否为生产环境
  );

  // 监听通知事件
  FlJpushAndroid.addNotificationListener((notification) {
    print("收到通知: $notification");
  });

  // 监听自定义消息事件
  FlJpushAndroid.addCustomMessageListener((message) {
    print("收到自定义消息: $message");
  });

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('JPush Demo'),
      ),
      body: Center(
        child: Text('JPush Initialized'),
      ),
    );
  }
}

5. 处理通知点击事件

你还可以监听用户点击通知的事件:

FlJpushAndroid.addNotificationOpenListener((notification) {
  print("用户点击了通知: $notification");
});

6. 其他功能

fl_jpush_android 插件还提供了其他功能,例如设置别名、标签、获取 Registration ID 等。你可以根据需要使用这些功能。

// 设置别名
FlJpushAndroid.setAlias("your_alias");

// 设置标签
FlJpushAndroid.setTags(["tag1", "tag2"]);

// 获取 Registration ID
String registrationId = await FlJpushAndroid.getRegistrationID();
print("Registration ID: $registrationId");
回到顶部