Flutter集成極光推送
目前眾多推送廠家只有極光支持了flutter,支持一下!!!
廢話不多說,開始擼代碼
第一步
因為設(shè)備的原因目前只在安卓上測試成功,就先分享安卓的配置過程,首先在極光官網(wǎng)創(chuàng)建應(yīng)用,完成之后在 android/app/build.gradle文件下添加配置:
android {
? ? .... 你的代碼
? ? defaultConfig {
? ? ? ? .....
? ? ? ? manifestPlaceholders = [
? ? ? ? ? ? ? JPUSH_PKGNAME : applicationId,
? ? ? ? ? ? ? JPUSH_APPKEY : "你的極光推送key", //JPush上注冊的包名對應(yīng)的appkey.
? ? ? ? ? ? ? JPUSH_CHANNEL : "你的推送渠道,如果不知道填寫developer-default即可",
? ? ? ? ]
? ? }
1
2
3
4
5
6
7
8
9
10
11
12
第二步
老規(guī)矩,添加依賴
dependencies:
? flutter:
? ? sdk: flutter
? ? flutter_jpush: ^0.0.4
1
2
3
4
第三步
導(dǎo)包
import 'package:flutter_jpush/flutter_jpush.dart';
1
第四步
在程序入口初始化Jpush,也就是在 main頁面初始化的時候添加:
void _startupJpush() async {
? ? print("初始化jpush");
? ? await FlutterJPush.startup();
? ? print("初始化jpush成功");
? }
1
2
3
4
5
第五步
在沒有后臺的情況下,可以在官網(wǎng)進行在線測試,
點擊發(fā)送手機就可以收到消息啦!!!
注意
如果真機運行報錯:couldn’t find “l(fā)ibflutter.so”
在android/app/build.gradle添加配置:
ndk{
? ? abiFilters 'armeabi', 'armeabi-v7a'//, 'arm64-v8a'
}
1
2
3
或者可以增加編譯選項:
--target-platform android-arm64 或者 --target-platform android-arm
1
如果運行沒有報錯,則不用添加;添加之后會啟動不起來!!!
接下來有幾個擴展方法一并介紹一下:
收到推送提醒
監(jiān)聽addReceiveNotificationListener方法:
/*
* 收到推送提醒
* */
? void _ReceiveNotification() async {
? ? FlutterJPush.addReceiveNotificationListener(
? ? ? ? (JPushNotification notification) {
? ? ? setState(() {
? ? ? ? /// 收到推送
? ? ? ? print("收到推送提醒: $notification");
? ? ? });
? ? });
? }
1
2
3
4
5
6
7
8
9
10
11
12
打開推送提醒
監(jiān)聽 addReceiveNotificationListener方法:
/*
? * 打開推送提醒
? * */
? void _OpenNotification() async {
? ? FlutterJPush.addReceiveOpenNotificationListener(
? ? ? ? (JPushNotification notification) {
? ? ? setState(() {
? ? ? ? print("打開了推送提醒: $notification");
? ? ? });
? ? });
? }
1
2
3
4
5
6
7
8
9
10
11
12
監(jiān)聽接收自定義消息
一般項目這個方法會用的比較多吧!!!
監(jiān)聽 addReceiveCustomMsgListener方法:
? /*
? * 監(jiān)聽接收自定義消息
? * */
? void _ReceiveCustomMsg() async {
? ? FlutterJPush.addReceiveCustomMsgListener((JPushMessage msg) {
? ? ? setState(() {
? ? ? ? print("收到推送消息提醒: $msg");
? ? ? });
? ? });
? }
1
2
3
4
5
6
7
8
9
10
11
其他方法很少用到,就不再贅述了;
---------------------
作者:尼伯特
來源:CSDN
原文:https://blog.csdn.net/weixin_36250061/article/details/86667274
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!