一、到极光后台注册应用
登录极光网页的服务中心,选择最下面的应用设置,
填写应用的appid,注册应用
注册之后获得AppKey
二、更改build.gradle文件
在app/src/build.gradle文件的defaultConfig 中添加下面代码
/*新加入的*/
ndk {
abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64'// 'arm64-v8a',
}
manifestPlaceholders = [
JPUSH_PKGNAME: applicationId,
JPUSH_APPKEY : "bfbb3108f9555cd0b3d96a74", /*NOTE: JPush 上注册的包名对应的 Appkey.*/
JPUSH_CHANNEL: "developer-default", /*暂时填写默认值即可.*/
]
/*新加入的*/
三、yaml文件中引入极光pub集成
四、在main程序文件入口中初始化极光平台插件
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 onReceiveNotification: $message");
setState(() {
debugLable = "flutter onReceiveNotification: $message";
});
}, onOpenNotification: (Map<String, dynamic> message) async {
print("flutter onOpenNotification: $message");
setState(() {
debugLable = "flutter onOpenNotification: $message";
});
}, onReceiveMessage: (Map<String, dynamic> message) async {
print("flutter onReceiveMessage: $message");
setState(() {
debugLable = "flutter onReceiveMessage: $message";
});
}, onReceiveNotificationAuthorization: (Map<String, dynamic> message) async {
print("flutter onReceiveNotificationAuthorization: $message");
setState(() {
debugLable = "flutter onReceiveNotificationAuthorization: $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: '我是测试极光push的标题',
buildId: 1,
content: '看到了说明已经成功了',
fireTime: fireDate,
subtitle: '真的只是个测试啊',
);
jpush.sendLocalNotification(localNotification).then((res) {
setState(() {
debugLable = res;
});
});
}),
])),
),
);
}
六、main文件完整代码
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 onReceiveNotification: $message");
setState(() {
debugLable = "flutter onReceiveNotification: $message";
});
}, onOpenNotification: (Map<String, dynamic> message) async {
print("flutter onOpenNotification: $message");
setState(() {
debugLable = "flutter onOpenNotification: $message";
});
}, onReceiveMessage: (Map<String, dynamic> message) async {
print("flutter onReceiveMessage: $message");
setState(() {
debugLable = "flutter onReceiveMessage: $message";
});
}, onReceiveNotificationAuthorization: (Map<String, dynamic> message) async {
print("flutter onReceiveNotificationAuthorization: $message");
setState(() {
debugLable = "flutter onReceiveNotificationAuthorization: $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: '我是测试极光push的标题',
buildId: 1,
content: '看到了说明已经成功了',
fireTime: fireDate,
subtitle: '真的只是个测试啊',
);
jpush.sendLocalNotification(localNotification).then((res) {
setState(() {
debugLable = res;
});
});
}),
])),
),
);
}
}