[toc]
Flutter集成極光 全踩坑
普通集成(非付費(fèi)版本智哀,走極光通道)
普通集成沒有什么難度,直接走極光的plugin就可以了:
導(dǎo)入對應(yīng)的包
直接yaml依賴荧恍,get一波
上報(bào)RegisterID
_initRegisterID(phoneModel) {
jPush.getRegistrationID().then((rid) {
if (rid != null && rid.isNotEmpty == true) {
print('---->rid:${rid}');
var params = {};
params["registrationId"] = rid;
params["phoneModel"] = Platform.isAndroid ? 'Android' : 'IOS';
LoginRepository.reportRegisterId(params);
}
});
}
也可以使用其他的方式瓷叫,更常見的是設(shè)置別名之類的方式來推送。
處理對應(yīng)的回調(diào)
jPush.applyPushAuthority(
NotificationSettingsIOS(sound: true, alert: true, badge: true));
try {
jPush.addEventHandler(
onReceiveNotification: (Map<String, dynamic> message) async {
print('---->onReceiveNotification:$message');
}, onOpenNotification: (Map<String, dynamic> message) async {
print('1111---->接收到推送:$message');
/// do some things
});
} on Exception {
print("---->獲取平臺版本失敗");
}
遇到的一些問題
這樣基本就是已經(jīng)集成了送巡,但是也會有一些問題:
而且pub上的極光好像不是最新的摹菠,依賴了
jpush_flutter:
git:
url: git://github.com/jpush/jpush-flutter-plugin.git
ref: master
問題1得到了解決。
但是其他問題依然存在骗爆,且4伟薄!
<img src="/Users/ly/Library/Application Support/typora-user-images/image-20200723145056878.png" alt="image-20200723145056878" style="zoom:50%;" />
付費(fèi)廠商通道集成
重要的事情說三次:
plugin是基礎(chǔ)免費(fèi)版本摘投,沒有付費(fèi)版本V蠊选!O簟幸撕!
plugin是基礎(chǔ)免費(fèi)版本,沒有付費(fèi)版本M獗邸W!宋光!
plugin是基礎(chǔ)免費(fèi)版本貌矿,沒有付費(fèi)版本!W锛选9渎!
付費(fèi)版本需要自己實(shí)現(xiàn)plugin菇民,
基礎(chǔ)集成
基礎(chǔ)的廠商集成需要coder 在其中的Android項(xiàng)目中手動集成各個廠商的sdk:
// 小米極光推送sdk
implementation 'cn.jiguang.sdk.plugin:xiaomi:3.6.8'
// 華為推送
implementation 'com.huawei.hms:push:4.0.2.300'
implementation 'cn.jiguang.sdk.plugin:huawei:3.6.8'
// 魅族推送
implementation 'cn.jiguang.sdk.plugin:meizu:3.6.8'
// vivo
implementation 'cn.jiguang.sdk.plugin:vivo:3.6.8'
// oppo
implementation 'cn.jiguang.sdk.plugin:oppo:3.6.8'
implementation fileTree(include: ['*.jar'], dir: 'libs')
基礎(chǔ)集成的文檔可以參考這部分:
插件集成
因?yàn)槭亲邚S商渠道尽楔,所以集成了之后,大部分的推送消息都不會走上面的callback第练,除了像魅族這樣沒排面的阔馋,都集成了sdk還走普通通道(狗頭),
極光是希望開發(fā)者實(shí)現(xiàn)自己的插件娇掏,在原生端接收到推送消息呕寝,橋接到futter端進(jìn)行消費(fèi):
- 極光發(fā)送推送消息
- 中轉(zhuǎn)ac被拉起,拿到對應(yīng)的推送字段
- 跳轉(zhuǎn)到mainActivity婴梧,并把數(shù)據(jù)傳遞過去下梢,MainActivity拿到數(shù)據(jù)通過MethodChannel傳遞到flutter端
- flutter端進(jìn)行消費(fèi)處理客蹋。
OpenClickActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// mTextView = new TextView(this);
// setContentView(mTextView);
// handleOpenClick();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
Intent intent = new Intent(this, MainActivity.class);
intent.setData(getIntent().getData());
if (getIntent().getExtras() != null) {
intent.putExtras(getIntent().getExtras());
}
startActivity(intent);
finish();
}
MainActivity
private void handleOpenClick(MethodChannel channel, Intent intent) {
String data = null;
//獲取華為平臺附帶的jpush信息
if (intent.getData() != null) {
data = intent.getData().toString();
}
//獲取fcm、oppo孽江、vivo讶坯、華碩、小米平臺附帶的jpush信息
if(TextUtils.isEmpty(data) && intent.getExtras() != null){
data = intent.getExtras().getString("JMessageExtra");
}
if (TextUtils.isEmpty(data)) return;
try {
JSONObject jsonObject = new JSONObject(data);
String msgId = jsonObject.optString(KEY_MSGID);
byte whichPushSDK = (byte) jsonObject.optInt(KEY_WHICH_PUSH_SDK);
String title = jsonObject.optString(KEY_TITLE);
String content = jsonObject.optString(KEY_CONTENT);
String extras = jsonObject.optString(KEY_EXTRAS);
final Handler mHandler = new Handler();
Runnable r = () -> {
//do something
Map<String, String> map = new HashMap<>();
map.put("msgId", msgId);
map.put("whichPushSDK", whichPushSDK + "");
map.put("title", title);
map.put("content", content);
map.put("extras", extras);
// Toast.makeText(MainActivity.this, map.toString(), Toast.LENGTH_SHORT).show();
channel.invokeMethod("notifyclick", map);
};
//主線程中調(diào)用:
mHandler.postDelayed(r, 100);//延時100毫秒
} catch (JSONException e) {
Log.w(TAG, "parse notification error");
}
}
Flutter
Future<Null> _handleMethod(MethodCall call) async {
print("_handleMethod: ${call.method}");
switch (call.method) {
case "notifyclick":
return notifyclick(call.arguments.cast<String, dynamic>());
default:
throw new UnsupportedError("Unrecognized Event");
}
}
Future<dynamic> notifyclick(Map<String, dynamic> message) async {
print('1111---->接收到推送:$message');
String pushType;
String afterSalesId;
String appMessageId;
String extra=message["extras"];
print(extra);
JpushModel jpushModel=JpushModel.fromJson(jsonDecode(extra));
pushType=jpushModel?.pushType;
afterSalesId=jpushModel?.afterSalesId;
appMessageId=jpushModel?.appMessageId;
if (pushType == "stationMessage") {
print('----------appMessageId= $appMessageId');
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MessageDetailPage(
messageCellList:
MessageCellList(appMessageId: int.parse(appMessageId)),
)));
} else {
// if (Platform.isAndroid) {
String _jumpUrl =
JPushConf.getJumpUrlByJpush(pushType, afterSalesId: afterSalesId);
WechatUtils().launchMiniPro(_jumpUrl);
// } else {
// WechatUtils().launchMiniPro('${WechatMiniUrl.choose_url}');
// }
}
AndroidManifest
<activity android:name=".OpenClickActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:exported="true">
<intent-filter>
<data android:path="/ypath" android:host="yhost" android:scheme="yscheme"></data>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
需要注意的點(diǎn)
中轉(zhuǎn)ac的問題
如果中轉(zhuǎn)ac配置出錯的話岗屏,點(diǎn)擊通知可以拉起app辆琅,但是不會獲取到extras,這個很惡心这刷,對于中轉(zhuǎn)ac在AndroidManifest中的配置婉烟,千萬不要配錯,不然很容易拉起了拿不到數(shù)據(jù)暇屋。-
需要后臺配合
之前版本的服務(wù)器sdk并沒有提供uri_activity這個配置似袁,如果發(fā)現(xiàn)沒有這個字段,更新下版本:<img src="/Users/ly/Library/Application Support/typora-user-images/image-20200723153625161.png" alt="image-20200723153625161" style="zoom:50%;" />