Flutter docks with Jiguang push to realize basic local push
Original: @As.Kai
Blog address: https://blog.csdn.net/qq_42362997
If the following content is helpful to you, please like it~
First import the dependency package
需要导入极光推送依赖包:
#极光推送JPush
jpush_flutter: ^0.6.2
#接着pub get
View the latest pub version:
Portal
Configuration aspects:
Add some configuration in android/app/build.gradle
android: {
....
defaultConfig {
...
ndk {
//选择要添加的对应 cpu 类型的 .so 库。
abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64', 'arm64-v8a',
}
manifestPlaceholders = [
JPUSH_PKGNAME : 你的key,
JPUSH_APPKEY : "appkey", // NOTE: JPush 上注册的包名对应的 Appkey.
JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
]
}
}
No Aurora Developer Id?
Go to Jiguang's official website https://www.jiguang.cn/Registration and login Needless to say,
click on the service center in the upper right corner of the avatar => developer platform and click to enter => create an application and
fill in the name to confirm => select the message push of the Android platform One step => fill in your application package name
Can't find the package name?
Open your project and find
android/app/src/main/AndroidManifest.xml. Open
the package tag at the top. Copy your package name and fill it out on Jiguang and it will be done.
After the completion, you will be given an AppKey. After you get this AppKey, fill it in Just go to our build.gradle
After the configuration is done, let's take a look at how to use it
//极光推送
final JPush _jPush = new JPush();
//异步方法 推送消息监听
Future<void> initPlatformState() async {
//错误信息
String platformVersion;
try {
//响应时间
_jPush.addEventHandler(
//接收通知回调方法
onReceiveNotification: (Map<String, dynamic> message) async {
//接收到数据 //进程运行时候可以接受
// print('>>>>>>>>>flutter接收到推送:${message}');
setState(() {
debugLable = '接收到极光推送数据:$message';
// notifyRoute();
});
}, //点击通知毁掉方法
onOpenNotification: (Map<String, dynamic> message) async {
await print('触发onOpenNotification>>>>>>>${message.toString()}');
// notifyRoute();
},
//接收自定义消息回调方法
// onReceiveMessage: (Map<String,dynamic> message) async{
// print('触发onReceiveMessage');
// notifyRoute();
// }
);
} on PlatformException {
platformVersion = '平台版本获取失败,请检查配置';
}
if (!mounted) return;
setState(() {
debugLable = platformVersion;
print('deBug===${debugLable.toString()}');
});
}
Then add a button click event to the build to realize the button click to send a push:
child: FlatButton(
onPressed: () {
//3秒后时间
var fireDate = DateTime.fromMillisecondsSinceEpoch(
DateTime.now().millisecondsSinceEpoch + 3000);
var localNotification = LocalNotification(
id: 001,
title: '作者:@As丶Kai',
content: '看到了说明已经成功了',
buildId: 1,
fireTime: fireDate,
subtitle: '子标题');
_jPush
.sendLocalNotification(localNotification)
.then((value) {
//传递
setState(() {
debugLable = value;
print('value===${value.toString()}');
});
});
},
child: new Text('发送推送信息',
style: TextStyle(color: Colors.white))),
Make a 3 second time stamp and push after 3 seconds
var fireDate = DateTime.fromMillisecondsSinceEpoch(
DateTime.now().millisecondsSinceEpoch + 3000);
Run to see the effect! The above is Jiguang basic local push integration
The following is the complete code:
//极光推送
final JPush _jPush = new JPush();
String debugLable = 'Unknown';
//推送跳转
void notifyRoute() async {
await Navigator.of(context)
.push(new MaterialPageRoute(builder: (ctx) => new IndexPage()));
}
Future<void> initPlatformState() async {
//错误信息
String platformVersion;
try {
//响应时间
_jPush.addEventHandler(
//接收通知回调方法
onReceiveNotification: (Map<String, dynamic> message) async {
//接收到数据 //进程运行时候可以接受
// print('>>>>>>>>>flutter接收到推送:${message}');
setState(() {
debugLable = '接收到极光推送数据:$message';
// notifyRoute();
});
}, //点击通知毁掉方法
onOpenNotification: (Map<String, dynamic> message) async {
await print('触发onOpenNotification>>>>>>>${message.toString()}');
// notifyRoute();
},
//接收自定义消息回调方法
// onReceiveMessage: (Map<String,dynamic> message) async{
// print('触发onReceiveMessage');
// notifyRoute();
// }
);
} on PlatformException {
platformVersion = '平台版本获取失败,请检查配置';
}
if (!mounted) return;
setState(() {
debugLable = platformVersion;
print('deBug===${debugLable.toString()}');
});
}
//*******************END****************
@override
void initState() {
// TODO: implement initState
initPlatformState();
}
@override
Widget build(BuildContext context) {
setState(() {
print('deBug===${debugLable.toString()}');
});
return Scaffold(
backgroundColor: Colors.black,
appBar: new AppBar(
brightness: Brightness.dark,
title: new Text(widget.title),
),
body: return new Column(
children: [
new Text(
'结果:${debugLable}',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: ScreenUtil().setSp(30.0)),
),
new Center(
child: FlatButton(
onPressed: () {
//3秒后时间
var fireDate = DateTime.fromMillisecondsSinceEpoch(
DateTime.now().millisecondsSinceEpoch + 3000);
var localNotification = LocalNotification(
id: 001,
title: '作者:@As丶Kai',
content: '看到了说明已经成功了',
buildId: 1,
fireTime: fireDate,
subtitle: '子标题');
_jPush
.sendLocalNotification(localNotification)
.then((value) {
//传递
setState(() {
debugLable = value;
print('value===${value.toString()}');
});
});
},
child: new Text('发送推送信息',
style: TextStyle(color: Colors.white))),
)
],
),
);
}
effect:
Follow me and grow together!
@As.Kai