68-Flutter中极光推送的使用
1、申請極光賬號和建立應(yīng)用
極光推送的官方網(wǎng)址為:https://www.jiguang.cn/
注冊好后,進(jìn)入'服務(wù)中心',然后再進(jìn)入'開發(fā)者平臺',點(diǎn)擊創(chuàng)建應(yīng)用。
這時(shí)候會(huì)出現(xiàn)新頁面,讓你填寫“應(yīng)用名稱”和上傳“應(yīng)用圖標(biāo)”。
創(chuàng)建完成,極光平臺就會(huì)給我們兩個(gè)key。
- appKey : 移動(dòng)客戶端使用的key
- Master Secret : 服務(wù)端使用的key
我們這里只做移動(dòng)端不做服務(wù)端,所以只需要appKey。得到這個(gè)Key也算是極光平臺操作完了
2、加入dependencies依賴
github網(wǎng)址:https://github.com/jpush/jpush-flutter-plugin
要使用極光推送插件必須先下載包,要下載包就需要先添加依賴,直接把下面的代碼加入pubspec.yaml文件中。
jpush_flutter:?0.0.11寫完代碼后,選擇Android Studio右上角的Packages get進(jìn)行下載,下載完成后進(jìn)行操作。
3、build.gradle添加可以和cpu型號代碼
打開android/app/src/build.gradle文件,加入如下代碼:
defaultConfig?{applicationId?"sscai.club.flutter_shop"minSdkVersion?16targetSdkVersion?28versionCode?flutterVersionCode.toInteger()versionName?flutterVersionNametestInstrumentationRunner?"android.support.test.runner.AndroidJUnitRunner"/*新加入的*/ndk?{/*選擇要添加的對應(yīng)?cpu?類型的?.so?庫。abiFilters?'armeabi',?'armeabi-v7a',?'x86',?'x86_64',?'mips',?'mips64'//?'arm64-v8a',/*還可以添加}manifestPlaceholders?=?[JPUSH_PKGNAME:?applicationId,JPUSH_APPKEY?:?"這里寫入你自己申請的Key哦",?/*NOTE:?JPush?上注冊的包名對應(yīng)的?Appkey.*/JPUSH_CHANNEL:?"developer-default",?/*暫時(shí)填寫默認(rèn)值即可.*/]/*新加入的*/ }詳細(xì)請參考: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?{/*監(jiān)聽響應(yīng)方法的編寫*/jpush.addEventHandler(onReceiveNotification:?(Map<String,?dynamic>?message)?async?{print(">>>>>>>>>>>>>>>>>flutter?接收到推送:?$message");setState(()?{debugLable?=?"接收到推送:?$message";});});}?on?PlatformException?{platformVersion?=?'平臺版本獲取失敗,請檢查!';}if?(!mounted){return;}setState(()?{debugLable?=?platformVersion;}); }編寫build的視圖
@overrideWidget?build(BuildContext?context)?{return?new?MaterialApp(home:?new?Scaffold(appBar:?new?AppBar(title:?const?Text('極光推送'),),body:?new?Center(child:?new?Column(children:[new?Text('結(jié)果:?$debugLable\n'),new?RaisedButton(child:?new?Text('點(diǎn)擊發(fā)送推送消息\n',),onPressed:?()?{/*三秒后出發(fā)本地推送*/var?fireDate?=?DateTime.fromMillisecondsSinceEpoch(DateTime.now().millisecondsSinceEpoch?+?3000);var?localNotification?=?LocalNotification(id:?234,title:?'我是推送測試標(biāo)題',buildId:?1,content:?'看到了說明已經(jīng)成功了',fireTime:?fireDate,subtitle:?'一個(gè)測試',);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';???/*錯(cuò)誤信息*/final?JPush?jpush?=?new?JPush();?/*?初始化極光插件*/@overridevoid?initState()?{super.initState();initPlatformState();??/*極光插件平臺初始化*/}Future<void>?initPlatformState()?async?{String?platformVersion;try?{/*監(jiān)聽響應(yīng)方法的編寫*/jpush.addEventHandler(onReceiveNotification:?(Map<String,?dynamic>?message)?async?{print(">>>>>>>>>>>>>>>>>flutter?接收到推送:?$message");setState(()?{debugLable?=?"接收到推送:?$message";});});}?on?PlatformException?{platformVersion?=?'平臺版本獲取失敗,請檢查!';}if?(!mounted){return;}setState(()?{debugLable?=?platformVersion;});}/*編寫視圖*/@overrideWidget?build(BuildContext?context)?{return?new?MaterialApp(home:?new?Scaffold(appBar:?new?AppBar(title:?const?Text('極光推送'),),body:?new?Center(child:?new?Column(children:[new?Text('結(jié)果:?$debugLable\n'),new?RaisedButton(child:?new?Text('點(diǎn)擊發(fā)送推送消息\n',),onPressed:?()?{/*三秒后出發(fā)本地推送*/var?fireDate?=?DateTime.fromMillisecondsSinceEpoch(DateTime.now().millisecondsSinceEpoch?+?3000);var?localNotification?=?LocalNotification(id:?234,title:?'我是推送測試標(biāo)題',buildId:?1,content:?'看到了說明已經(jīng)成功了',fireTime:?fireDate,subtitle:?'一個(gè)測試',);jpush.sendLocalNotification(localNotification).then((res)?{setState(()?{debugLable?=?res;});});}),])),),);} }效果圖:
4、擴(kuò)展幾個(gè)方法
收到推送提醒
監(jiān)聽addReceiveNotificationListener方法:
/* *?收到推送提醒 *?*/void?_ReceiveNotification()?async?{FlutterJPush.addReceiveNotificationListener((JPushNotification?notification)?{setState(()?{///?收到推送print("收到推送提醒:?$notification");});});}打開推送提醒
監(jiān)聽 addReceiveNotificationListener方法:
?/**?打開推送提醒*?*/void?_OpenNotification()?async?{FlutterJPush.addReceiveOpenNotificationListener((JPushNotification?notification)?{setState(()?{print("打開了推送提醒:?$notification");});});}監(jiān)聽接收自定義消息
一般項(xiàng)目這個(gè)方法會(huì)用的比較多吧!!!
監(jiān)聽 addReceiveCustomMsgListener方法:
??/**?監(jiān)聽接收自定義消息*?*/void?_ReceiveCustomMsg()?async?{FlutterJPush.addReceiveCustomMsgListener((JPushMessage?msg)?{setState(()?{print("收到推送消息提醒:?$msg");});});}總結(jié)
以上是生活随笔為你收集整理的68-Flutter中极光推送的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Josn
- 下一篇: Ip地址的配置与解释