極光推送的官方網址爲:https://www.jiguang.cn/css
註冊好後,進入'服務中心',而後再進入'開發者平臺',點擊建立應用。android
這時候會出現新頁面,讓你填寫「應用名稱」和上傳「應用圖標」。git
建立完成,極光平臺就會給咱們兩個key。github
咱們這裏只作移動端不作服務端,因此只須要appKey。獲得這個Key也算是極光平臺操做完了web
github網址:https://github.com/jpush/jpush-flutter-pluginbash
要使用極光推送插件必須先下載包,要下載包就須要先添加依賴,直接把下面的代碼加入pubspec.yaml文件中。app
jpush_flutter: 0.0.11
寫完代碼後,選擇Android Studio右上角的Packages get進行下載,下載完成後進行操做。async
打開android/app/src/build.gradle文件,加入以下代碼:ide
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測試
在 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;
});
});
}),
]
)
),
),
);
}
}
效果圖:
收到推送提醒
監聽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"); }); }); }