Flutter集成極光 全踩坑 Flutter集成極光 全踩坑

[toc]

Flutter集成極光 全踩坑

普通集成(非付費版本,走極光通道)

普通集成沒有什麼難度,直接走極光的plugin就可以了:

導入對應的包

插件包地址

直接yaml依賴,get一波

上報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);
      }
    });
  }

也可以使用其他的方式,更常見的是設置別名之類的方式來推送。

處理對應的回調

  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("---->獲取平臺版本失敗");
    }

遇到的一些問題

這樣基本就是已經集成了,但是也會有一些問題:

  1. ios點擊不觸發事件
  2. ios殺死進程後獲取不到extras
  3. 更多問題歡迎來到issues圍觀

而且pub上的極光好像不是最新的,依賴了

  jpush_flutter:
    git:
      url: git://github.com/jpush/jpush-flutter-plugin.git
      ref: master

問題1得到了解決。

但是其他問題依然存在,且!!

<img src="/Users/ly/Library/Application Support/typora-user-images/image-20200723145056878.png" alt="image-20200723145056878" style="zoom:50%;" />

付費廠商通道集成

重要的事情說三次:

plugin是基礎免費版本,沒有付費版本!!!!

plugin是基礎免費版本,沒有付費版本!!!!

plugin是基礎免費版本,沒有付費版本!!!!

付費版本需要自己實現plugin,

基礎集成

基礎的廠商集成需要coder 在其中的Android項目中手動集成各個廠商的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')

基礎集成的文檔可以參考這部分:

各個廠商集成文檔

插件集成

因爲是走廠商渠道,所以集成了之後,大部分的推送消息都不會走上面的callback,除了像魅族這樣沒排面的,都集成了sdk還走普通通道(狗頭),

極光是希望開發者實現自己的插件,在原生端接收到推送消息,橋接到futter端進行消費:

  1. 極光發送推送消息
  2. 中轉ac被拉起,拿到對應的推送字段
  3. 跳轉到mainActivity,並把數據傳遞過去,MainActivity拿到數據通過MethodChannel傳遞到flutter端
  4. flutter端進行消費處理。

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);
            };
            //主線程中調用:
            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" />

需要注意的點

  1. 中轉ac的問題
    如果中轉ac配置出錯的話,點擊通知可以拉起app,但是不會獲取到extras,這個很噁心,對於中轉ac在AndroidManifest中的配置,千萬不要配錯,不然很容易拉起了拿不到數據。

  2. 需要後臺配合

    之前版本的服務器sdk並沒有提供uri_activity這個配置,如果發現沒有這個字段,更新下版本:<img src="/Users/ly/Library/Application Support/typora-user-images/image-20200723153625161.png" alt="image-20200723153625161" style="zoom:50%;" />

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章