1、申請極光賬號和建立應用
極光推送的官方網址為:https://www.jiguang.cn/
注冊好后,進入'服務中心',然后再進入'開發者平台',點擊創建應用。

這時候會出現新頁面,讓你填寫“應用名稱”和上傳“應用圖標”。

創建完成,極光平台就會給我們兩個key。
- appKey : 移動客戶端使用的key
- Master Secret : 服務端使用的key
我們這里只做移動端不做服務端,所以只需要appKey。得到這個Key也算是極光平台操作完了
2、加入dependencies依賴
github網址:https://github.com/jpush/jpush-flutter-plugin
要使用極光推送插件必須先下載包,要下載包就需要先添加依賴,直接把下面的代碼加入pubspec.yaml文件中。
jpush_flutter: 0.0.11
寫完代碼后,選擇Android Studio右上角的Packages get進行下載,下載完成后進行操作。

3、build.gradle添加可以和cpu型號代碼
打開android/app/src/build.gradle文件,加入如下代碼:
defaultConfig {
applicationId "sscai.club.flutter_shop"
minSdkVersion 16
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
/*新加入的*/
ndk {
/*選擇要添加的對應 cpu 類型的 .so 庫。
abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64'// 'arm64-v8a',
/*還可以添加
}
manifestPlaceholders = [
JPUSH_PKGNAME: applicationId,
JPUSH_APPKEY : "這里寫入你自己申請的Key哦", /*NOTE: JPush 上注冊的包名對應的 Appkey.*/
JPUSH_CHANNEL: "developer-default", /*暫時填寫默認值即可.*/
]
/*新加入的*/
}
詳細請參考:https://github.com/jpush/jpush-flutter-plugin
4、主要代碼編寫
在 main.dart 中引入依賴
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
編寫initPlatformState方法
Future<void> initPlatformState() async {
String platformVersion;
try {
/*監聽響應方法的編寫*/
jpush.addEventHandler(
onReceiveNotification: (Map<String, dynamic> message) async {
print(">>>>>>>>>>>>>>>>>flutter 接收到推送: $message");
setState(() {
debugLable = "接收到推送: $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: '我是推送測試標題',
buildId: 1,
content: '看到了說明已經成功了',
fireTime: fireDate,
subtitle: '一個測試',
);
jpush.sendLocalNotification(localNotification).then((res) {
setState(() {
debugLable = res;
});
});
}),
]
)
),
),
);
}
main.dart 完整代碼:
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 接收到推送: $message");
setState(() {
debugLable = "接收到推送: $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: '我是推送測試標題',
buildId: 1,
content: '看到了說明已經成功了',
fireTime: fireDate,
subtitle: '一個測試',
);
jpush.sendLocalNotification(localNotification).then((res) {
setState(() {
debugLable = res;
});
});
}),
]
)
),
),
);
}
}
效果圖:

4、擴展幾個方法
收到推送提醒
監聽addReceiveNotificationListener方法:
/*
* 收到推送提醒
* */
void _ReceiveNotification() async {
FlutterJPush.addReceiveNotificationListener(
(JPushNotification notification) {
setState(() {
/// 收到推送
print("收到推送提醒: $notification");
});
});
}
打開推送提醒
監聽 addReceiveNotificationListener方法:
/*
* 打開推送提醒
* */
void _OpenNotification() async {
FlutterJPush.addReceiveOpenNotificationListener(
(JPushNotification notification) {
setState(() {
print("打開了推送提醒: $notification");
});
});
}
監聽接收自定義消息
一般項目這個方法會用的比較多吧!!!
監聽 addReceiveCustomMsgListener方法:
/*
* 監聽接收自定義消息
* */
void _ReceiveCustomMsg() async {
FlutterJPush.addReceiveCustomMsgListener((JPushMessage msg) {
setState(() {
print("收到推送消息提醒: $msg");
});
});
}