Flutter在Android平台实现消息推送
1. 极光推送平台申请应用
1. 注册极光推送平台;
2. 极光推送平台创建应用;

填写应用程序名称。

选择平台,选择服务,点击下一步。

需要输入应用包名(Android平台),选择渠道,点击下一步。

完成上面的操作在应用管理中就可以看到创建的应用程序了。

点击“应用设置”,就可以看到应用信息了,其中AppKey非常重要,在项目中使用极光推送会用到。

2. 集成极光推送SDK
1. 安装插件
dependencies:
jpush_flutter: 0.6.3
在pubspec.yaml中配置保存后,在VS Code环境中会自动下载依赖包。
如果无法正常下载,执行 flutter pub get 。
2. 配置
Android平台 在 android ▸ app ▸ build.gradle中修改代码如下:
android: {
defaultConfig {
applicationId "你的应用包名"
ndk {
// 选择要添加的对应CPU类型的.SO库。
abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64', 'arm64-v8a'
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
// 极光上注册的包名对应的 Appkey
JPUSH_APPKEY : "appkey",
// 暂时填写默认值即可.
JPUSH_CHANNEL : "developer-default"
}
3. 使用极光推送
import 'package:flutter/material.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@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> {
@override
void initState() {
super.initState();
// 初始化极光推送
this.initJpush();
// 监听极光推送 (自定义的方法)
// https://github.com/jpush/jpush-flutter-plugin/blob/master/documents/APIs.md
initJpush() async {
// 初始化
JPush jpush = new JPush();
// 获取注册的ID
jpush.getRegistrationID().then((rid) {
print("获取注册的id:$rid");
// 初始化
jpush.setup(
// 极光官方申请应用的APP KEY
appKey: "17d78ecf32c322db169a1d98",
channel: "theChannel",
production: false,
debug: true,
// 设置别名实现指定用户推送
jpush.setAlias("jg6666").then((map) {
print("设置别名成功");
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 (e) {
print('极光SDK配置异常');
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(