Flutter第三方接入-极光推送
2020-10-23 本文已影响0人
小王在努力
1、Flutter使用第三方库
jpush_flutter: 0.6.0
2、Flutter代码实现
class ATPush{
static void iOSPush(Map<String, dynamic> message){
try{
Map<String, dynamic> jsonString = new Map<String, dynamic>.from(message);
print(jsonString);
Map<String, dynamic> aps = new Map<String, dynamic>.from(jsonString["aps"]);
print(aps);
Map<String, dynamic> alert = new Map<String, dynamic>.from(aps["alert"]);
print(alert);
print(alert["title"]);
Alert info = Alert.fromJson(alert);
print(info.title);
print(info.body);
ATAlertView.showAlertView(title:info.title ,message:info.body,hights: "取消",normals: "确定",completion: (index,title){
if(index == 1){
ATJump.jumpToHelp();
}
});
}catch (error){
print("error json = $error");
}
}
static Future<void> initPlatformState() async {
String platformVersion;
final JPush jpush = new JPush();
try {
jpush.addEventHandler(
onReceiveNotification: (Map<String, dynamic> message) async {
//极光发送通知
if(Platform.isAndroid){
ATAlertView.showAlertView(title:message["title"] ,message: message["alert"],hights: "取消",normals: "确定",completion: (index,title){
if(index == 1){
ATJump.jumpToHelp();
}
});
}else{
iOSPush(message);
}
print("flutter onReceiveNotification: $message");
}, onOpenNotification: (Map<String, dynamic> message) async {
print("flutter onOpenNotification: $message");
if(Platform.isIOS){
iOSPush(message);
}
}, onReceiveMessage: (Map<String, dynamic> message) async {
//极光自定义消息
if (Platform.isIOS){
iOSPush(message);
}else if(Platform.isAndroid){
ATAlertView.showAlertView(title: "消息推送",message: message["message"],hights: "取消",normals: "确定",completion: (index,title){
if(index == 1){
ATJump.jumpToHelp();
}
});
}
print("flutter onReceiveMessage: $message");
}, onReceiveNotificationAuthorization:
(Map<String, dynamic> message) async {
print("flutter onReceiveNotificationAuthorization: $message");
});
} on PlatformException{
platformVersion = 'Failed to get platform version.';
}
jpush.setup(
appKey: jShareKey, //你自己应用的 AppKey
channel: "developer-default",
production: false,
debug: true,
);
jpush.applyPushAuthority(
new NotificationSettingsIOS(sound: true, alert: true, badge: true));
jpush.getRegistrationID().then((rid) {
print("flutter get registration id : $rid");
});
}
}
//注意这里的appKey需要到极光的官网申请
3、Flutter工程的main文件调用
void initState() {
super.initState();
ATShare.share();
loginAction();
ATUMeng.flutterUmengInit();
ATUMeng.beignPageView("main");
ATPush.initPlatformState();
}
4、iOS工程设置
由于iOS工程推送需要涉及证书的配置,如果有不懂可以看这篇文章证书配置
配置完后将dev证书上传到极光推送平台。
同时xCode工程记得设置如下图
![](https://img.haomeiwen.com/i1376834/60ffb8737f178898.png)
添加完后就可以使用真机测试了
5、安卓工程设置-只支持应用内推送
由于android如果要在杀掉app情况下收到推送,需要在不同的手机渠道商处注册
这里没有注册目前先不使用
![](https://img.haomeiwen.com/i1376834/26e8494daeed780e.png)
6、成果展示
![](https://img.haomeiwen.com/i1376834/d7224222cc663c45.png)
具体代码分析在2里面