{"id":130,"date":"2018-11-29T16:20:51","date_gmt":"2018-11-29T08:20:51","guid":{"rendered":"http:\/\/blog.peergine.com\/?p=130"},"modified":"2018-11-29T16:20:51","modified_gmt":"2018-11-29T08:20:51","slug":"%e6%80%8e%e6%a0%b7%e5%88%a9%e7%94%a8p2p%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%8f%91%e9%80%81%e6%9e%81%e5%85%89%e6%8e%a8%e9%80%81%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/blog.peergine.com\/?p=130","title":{"rendered":"\u600e\u6837\u5229\u7528P2P\u670d\u52a1\u5668\u53d1\u9001\u6781\u5149\u63a8\u9001\uff1f"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_89 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\r\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/blog.peergine.com\/?p=130\/#1%E3%80%81%E6%9E%81%E5%85%89%E6%8E%A8%E9%80%81%E6%B5%81%E7%A8%8B%E5%9B%BE%E3%80%82\" >1\u3001\u6781\u5149\u63a8\u9001\u6d41\u7a0b\u56fe\u3002<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/blog.peergine.com\/?p=130\/#2%E3%80%81%E9%A6%96%E5%85%88%E6%9F%A5%E7%9C%8B%E6%9E%81%E5%85%89%E6%8E%A8%E9%80%81%E7%9A%84%E6%96%87%E6%A1%A3%EF%BC%8C%E9%9B%86%E6%88%90%E6%9E%81%E5%85%89%E6%8E%A8%E9%80%81%E9%85%8D%E7%BD%AE%E5%92%8C%E5%BA%93%E3%80%82\" >2\u3001\u9996\u5148\u67e5\u770b\u6781\u5149\u63a8\u9001\u7684\u6587\u6863\uff0c\u96c6\u6210\u6781\u5149\u63a8\u9001\u914d\u7f6e\u548c\u5e93\u3002<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/blog.peergine.com\/?p=130\/#3%E3%80%81_%E5%8F%82%E8%80%83%E6%9E%81%E5%85%89Demo%E5%88%9B%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E7%9B%91%E5%90%AC%E5%99%A8%E5%B9%B6%E5%A4%84%E7%90%86%E6%B6%88%E6%81%AF%E3%80%82\" >3\u3001 \u53c2\u8003\u6781\u5149Demo\u521b\u5efa\u81ea\u5df1\u7684\u76d1\u542c\u5668\u5e76\u5904\u7406\u6d88\u606f\u3002<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/blog.peergine.com\/?p=130\/#4%E3%80%81%E5%88%A9%E7%94%A8%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%AB%AF%E5%8F%91%E9%80%81%E6%9E%81%E5%85%89%E6%8E%A8%E9%80%81%E3%80%82\" >4\u3001\u5229\u7528\u670d\u52a1\u5668\u7aef\u53d1\u9001\u6781\u5149\u63a8\u9001\u3002<\/a><\/li><\/ul><\/nav><\/div>\r\n<h1><span class=\"ez-toc-section\" id=\"1%E3%80%81%E6%9E%81%E5%85%89%E6%8E%A8%E9%80%81%E6%B5%81%E7%A8%8B%E5%9B%BE%E3%80%82\"><\/span>1\u3001\u6781\u5149\u63a8\u9001\u6d41\u7a0b\u56fe\u3002<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-218 aligncenter\" src=\"http:\/\/blog.peergine.com\/wp-content\/uploads\/2018\/11\/pushliuchengtu.png\" alt=\"\" width=\"460\" height=\"252\" srcset=\"http:\/\/blog.peergine.com\/wp-content\/uploads\/2018\/11\/pushliuchengtu.png 460w, http:\/\/blog.peergine.com\/wp-content\/uploads\/2018\/11\/pushliuchengtu-300x164.png 300w\" sizes=\"auto, (max-width: 460px) 100vw, 460px\" \/><\/p>\n<p style=\"text-align: center;\">\u6781\u5149\u63a8\u9001\u7b80\u5355\u6d41\u7a0b\u56fe<\/p>\n<h1><span class=\"ez-toc-section\" id=\"2%E3%80%81%E9%A6%96%E5%85%88%E6%9F%A5%E7%9C%8B%E6%9E%81%E5%85%89%E6%8E%A8%E9%80%81%E7%9A%84%E6%96%87%E6%A1%A3%EF%BC%8C%E9%9B%86%E6%88%90%E6%9E%81%E5%85%89%E6%8E%A8%E9%80%81%E9%85%8D%E7%BD%AE%E5%92%8C%E5%BA%93%E3%80%82\"><\/span>2\u3001\u9996\u5148\u67e5\u770b\u6781\u5149\u63a8\u9001\u7684\u6587\u6863\uff0c\u96c6\u6210\u6781\u5149\u63a8\u9001\u914d\u7f6e\u548c\u5e93\u3002<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p><a href=\"https:\/\/docs.jiguang.cn\/jpush\/guideline\/intro\/\">\u6781\u5149\u63a8\u9001\u6587\u6863<\/a><\/p>\n<p><a href=\"https:\/\/docs.jiguang.cn\/jpush\/client\/Android\/android_guide\/\">\u6781\u5149\u63a8\u9001\u5b89\u5353\u96c6\u6210\u6307\u5357<\/a><\/p>\n<h1><span class=\"ez-toc-section\" id=\"3%E3%80%81_%E5%8F%82%E8%80%83%E6%9E%81%E5%85%89Demo%E5%88%9B%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E7%9B%91%E5%90%AC%E5%99%A8%E5%B9%B6%E5%A4%84%E7%90%86%E6%B6%88%E6%81%AF%E3%80%82\"><\/span>3\u3001 \u53c2\u8003\u6781\u5149Demo\u521b\u5efa\u81ea\u5df1\u7684\u76d1\u542c\u5668\u5e76\u5904\u7406\u6d88\u606f\u3002<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>MyReceiver.java \u5177\u4f53\u8fc7\u7a0b\u662f\u5c06\u6781\u5149\u63a8\u9001\u63a5\u6536\u5230\u7684\u81ea\u5b9a\u4e49\u6d88\u606f\u53d1\u9001\u5230Activity\u3002<\/p>\n<pre class=\"lang:java decode:true\">package com.peergine.demo.ext.renmulti;\r\n\r\nimport android.app.ActivityManager;\r\nimport android.content.BroadcastReceiver;\r\nimport android.content.ComponentName;\r\nimport android.content.Context;\r\nimport android.content.DialogInterface;\r\nimport android.content.Intent;\r\nimport android.graphics.PixelFormat;\r\nimport android.os.Bundle;\r\nimport android.support.v7.app.AlertDialog;\r\nimport android.text.TextUtils;\r\nimport android.util.Log;\r\nimport android.view.LayoutInflater;\r\nimport android.view.View;\r\nimport android.view.WindowManager;\r\nimport android.widget.Button;\r\nimport android.widget.TextView;\r\n\r\nimport org.json.JSONException;\r\nimport org.json.JSONObject;\r\n\r\nimport java.util.Iterator;\r\n\r\nimport cn.jpush.android.api.JPushInterface;\r\n\r\nimport static android.content.Context.ACTIVITY_SERVICE;\r\nimport static com.peergine.demo.ext.renmulti.MainActivity.isEmpty;\r\n\r\n\/**\r\n * \u81ea\u5b9a\u4e49\u63a5\u6536\u5668\r\n * \r\n * \u5982\u679c\u4e0d\u5b9a\u4e49\u8fd9\u4e2a Receiver\uff0c\u5219\uff1a\r\n * 1) \u9ed8\u8ba4\u7528\u6237\u4f1a\u6253\u5f00\u4e3b\u754c\u9762\r\n * 2) \u63a5\u6536\u4e0d\u5230\u81ea\u5b9a\u4e49\u6d88\u606f\r\n *\/\r\npublic class MyReceiver extends BroadcastReceiver {\r\n\tprivate static final String TAG = \"JIGUANG-Example\";\r\n\tprivate WindowManager wm = null;\r\n\tprivate View view = null;\r\n\r\n\t@Override\r\n\tpublic void onReceive(Context context, Intent intent) {\r\n\t\ttry {\r\n\t\t\tBundle bundle = intent.getExtras();\r\n\t\t\tLog.d(TAG, \"[MyReceiver] onReceive - \" + intent.getAction() + \", extras: \" + printBundle(bundle));\r\n\r\n\t\t\tif (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {\r\n\t\t\t\tString regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);\r\n\t\t\t\tLog.d(TAG, \"[MyReceiver] \u63a5\u6536Registration Id : \" + regId);\r\n\t\t\t\t\/\/send the Registration Id to your server...\r\n\r\n\t\t\t} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {\r\n\t\t\t\tLog.d(TAG, \"[MyReceiver] \u63a5\u6536\u5230\u63a8\u9001\u4e0b\u6765\u7684\u81ea\u5b9a\u4e49\u6d88\u606f: \" + bundle.getString(JPushInterface.EXTRA_MESSAGE));\r\n\t\t\t\tprocessCustomMessage(context, bundle);\r\n\r\n\t\t\t} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {\r\n\t\t\t\tLog.d(TAG, \"[MyReceiver] \u63a5\u6536\u5230\u63a8\u9001\u4e0b\u6765\u7684\u901a\u77e5\");\r\n\t\t\t\tint notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);\r\n\t\t\t\tLog.d(TAG, \"[MyReceiver] \u63a5\u6536\u5230\u63a8\u9001\u4e0b\u6765\u7684\u901a\u77e5\u7684ID: \" + notifactionId);\r\n\r\n\t\t\t} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {\r\n\t\t\t\tLog.d(TAG, \"[MyReceiver] \u7528\u6237\u70b9\u51fb\u6253\u5f00\u4e86\u901a\u77e5\");\r\n\r\n\/\/\t\t\t\t\/\/\u6253\u5f00\u81ea\u5b9a\u4e49\u7684Activity\r\n\/\/\t\t\t\tIntent i = new Intent(context, TestActivity.class);\r\n\/\/\t\t\t\ti.putExtras(bundle);\r\n\/\/\t\t\t\t\/\/i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);\r\n\/\/\t\t\t\ti.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );\r\n\/\/\t\t\t\tcontext.startActivity(i);\r\n\r\n\t\t\t} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {\r\n\t\t\t\tLog.d(TAG, \"[MyReceiver] \u7528\u6237\u6536\u5230\u5230RICH PUSH CALLBACK: \" + bundle.getString(JPushInterface.EXTRA_EXTRA));\r\n\t\t\t\t\/\/\u5728\u8fd9\u91cc\u6839\u636e JPushInterface.EXTRA_EXTRA \u7684\u5185\u5bb9\u5904\u7406\u4ee3\u7801\uff0c\u6bd4\u5982\u6253\u5f00\u65b0\u7684Activity\uff0c \u6253\u5f00\u4e00\u4e2a\u7f51\u9875\u7b49..\r\n\r\n\t\t\t} else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {\r\n\t\t\t\tboolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);\r\n\t\t\t\tLog.w(TAG, \"[MyReceiver]\" + intent.getAction() +\" connected state change to \"+connected);\r\n\t\t\t} else {\r\n\t\t\t\tLog.d(TAG, \"[MyReceiver] Unhandled intent - \" + intent.getAction());\r\n\t\t\t}\r\n\t\t} catch (Exception e){\r\n\r\n\t\t}\r\n\r\n\t}\r\n\r\n\t\/\/ \u6253\u5370\u6240\u6709\u7684 intent extra \u6570\u636e\r\n\tprivate static String printBundle(Bundle bundle) {\r\n\t\tStringBuilder sb = new StringBuilder();\r\n\t\tfor (String key : bundle.keySet()) {\r\n\t\t\tif (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {\r\n\t\t\t\tsb.append(\"\\nkey:\" + key + \", value:\" + bundle.getInt(key));\r\n\t\t\t}else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){\r\n\t\t\t\tsb.append(\"\\nkey:\" + key + \", value:\" + bundle.getBoolean(key));\r\n\t\t\t} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {\r\n\t\t\t\tif (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {\r\n\t\t\t\t\tLog.i(TAG, \"This message has no Extra data\");\r\n\t\t\t\t\tcontinue;\r\n\t\t\t\t}\r\n\r\n\t\t\t\ttry {\r\n\t\t\t\t\tJSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));\r\n\t\t\t\t\tIterator&lt;String&gt; it =  json.keys();\r\n\r\n\t\t\t\t\twhile (it.hasNext()) {\r\n\t\t\t\t\t\tString myKey = it.next();\r\n\t\t\t\t\t\tsb.append(\"\\nkey:\" + key + \", value: [\" +\r\n\t\t\t\t\t\t\t\tmyKey + \" - \" +json.optString(myKey) + \"]\");\r\n\t\t\t\t\t}\r\n\t\t\t\t} catch (JSONException e) {\r\n\t\t\t\t\tLog.e(TAG, \"Get message extra JSON error!\");\r\n\t\t\t\t}\r\n\r\n\t\t\t} else {\r\n\t\t\t\tsb.append(\"\\nkey:\" + key + \", value:\" + bundle.get(key));\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn sb.toString();\r\n\t}\r\n\t\r\n\t\/\/send msg to MainActivity\r\n\tprivate void processCustomMessage(Context context, Bundle bundle) {\r\n\r\n        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);\r\n        String sAction = \"\";\r\n        String DesID = \"\";\r\n        String SrcID = \"\";\r\n        String sParam = \"\";\r\n\r\n        try {\r\n            JSONObject msgJson = new JSONObject(message);\r\n            sAction = msgJson.getString(\"Action\");\r\n            DesID = msgJson.getString(\"DesID\");\r\n            SrcID = msgJson.getString(\"SrcID\");\r\n            sParam = msgJson.getString(\"Param\");\r\n        } catch (JSONException e) {\r\n            e.printStackTrace();\r\n        }\r\n\r\n        MainActivity.sendMessageTo(context,sAction,DesID,SrcID,sParam);\r\n\r\n\r\n\r\n\t}\r\n\r\n}<\/pre>\n<p>\u5728Activity\u4e2d\u6dfb\u52a0\u4e0b\u5217\u4ee3\u7801\u63a5\u6536\u5e76\u5904\u7406\u81ea\u5b9a\u4e49\u6d88\u606f\u3002<\/p>\n<pre class=\"lang:java decode:true \"> \/\/for receive customer msg from jpush server\r\n    private MessageReceiver mMessageReceiver;\r\n    public static final String MESSAGE_RECEIVED_ACTION = \"com.peergine.demo.ext.renmulti.MESSAGE_RECEIVED_ACTION\";\r\n    public static final String KEY_TITLE = \"title\";\r\n    public static final String KEY_MESSAGE = \"message\";\r\n    public static final String KEY_EXTRAS = \"extras\";\r\n\r\n    public void registerMessageReceiver() {\r\n        mMessageReceiver = new MessageReceiver();\r\n        IntentFilter filter = new IntentFilter();\r\n        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);\r\n        filter.addAction(MESSAGE_RECEIVED_ACTION);\r\n        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, filter);\r\n    }\r\n\r\n    public class MessageReceiver extends BroadcastReceiver {\r\n\r\n        @Override\r\n        public void onReceive(Context context, Intent intent) {\r\n            try {\r\n                if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {\r\n                    String sAction = intent.getStringExtra(\"Action\" );\r\n                    String DesID = intent.getStringExtra(\"DesID\" );\r\n                    String SrcID = intent.getStringExtra(\"SrcID\");\r\n                    String sParam = intent.getStringExtra(\"Param\");\r\n\r\n                    if(MyJGPush.isSelection(sAction)){\r\n                        StringBuilder showMsg = new StringBuilder();\r\n                        showMsg.append(SrcID + \" Selection\");\r\n                        popCostom(sAction,DesID,SrcID,sParam);\r\n                    }\r\n\r\n                }\r\n            } catch (Exception e){\r\n                Log.d(\"MessageReceiver\",\"ex = \" + e.toString());\r\n            }\r\n        }\r\n    }\r\n\r\n    private void popCostom(final String sAction, final String DesID, final String SrcID, final String sParam){\r\n        AlertDialog.Builder builder = new AlertDialog.Builder(this);\r\n        builder.setTitle(\"show\");\r\n        builder.setMessage(\"sAction is \" + sAction  + \" sParam = \" + sParam);\r\n\r\n        builder.setPositiveButton(\"YES\",new DialogInterface.OnClickListener() {\r\n            @Override\r\n            public void onClick(DialogInterface dialog, int which) {\r\n                if (findFragment(FragmentMain.class) == null) {\r\n                    DatabaseHelper database = new DatabaseHelper(getApplicationContext());\/\/\u8fd9\u6bb5\u4ee3\u7801\u653e\u5230Activity\u7c7b\u4e2d\u624d\u7528this\r\n\r\n                    SQLiteDatabase db = database.getWritableDatabase();\r\n\r\n                    String addr = \"\";\r\n                    String id = \"\";\r\n                    try {\r\n                        Cursor c = db.query(\"config\", null, null, null, null, null, null);\/\/\u67e5\u8be2\u5e76\u83b7\u5f97\u6e38\u6807\r\n                        if (c.moveToFirst()) {\/\/\u5224\u65ad\u6e38\u6807\u662f\u5426\u4e3a\u7a7a\r\n\r\n                            c.move(c.getCount() - 1);\/\/\u79fb\u52a8\u5230\u6307\u5b9a\u8bb0\u5f55\r\n                            addr = c.getString(c.getColumnIndex(\"addr\"));\r\n                            id = c.getString(c.getColumnIndex(\"id\"));\r\n                            c.close();\r\n                        }\r\n                    }catch (Exception e){\r\n\r\n                    }\r\n\r\n                    if(MainActivity.isEmpty(addr)){\r\n                        addr = \"connect.peergine.com:7781\";\r\n                    }\r\n                    if(!DesID.equals(id)){\r\n                        return;\r\n                    }\r\n                    if(isEmpty(SrcID)){\r\n                        return;\r\n                    }\r\n\r\n                    int iInit = getRender().isInit()?0:1;\r\n\r\n                    loadRootFragment(R.id.fl_container, FragmentMain.newInstance(iInit,addr ,DesID , SrcID, \"\", 0, 1));\r\n                }\r\n            }\r\n        } );\r\n        builder.setNegativeButton(\"NO\", null);\r\n        builder.show();\r\n    }\r\n\r\n    public static int sendMessageTo(Context context,String sAction,String DesID, String SrcID, String sParam ){\r\n        String sActivity = GetTopActivity(ExampleApplication.getInstance());\r\n\r\n\r\n        if (sActivity.contains(MainActivity.class.getSimpleName())) {\r\n            Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);\r\n            msgIntent.putExtra(\"Action\", sAction);\r\n            msgIntent.putExtra(\"DesID\", DesID);\r\n            msgIntent.putExtra(\"SrcID\", SrcID);\r\n            msgIntent.putExtra(\"Param\", sParam);\r\n\r\n            LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);\r\n        } else {\r\n            Intent intent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);\r\n            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);\r\n            intent.setClass(context, MainActivity.class);\r\n            intent.putExtra(\"Action\", sAction);\r\n            intent.putExtra(\"DesID\", DesID);\r\n            intent.putExtra(\"SrcID\", SrcID);\r\n            intent.putExtra(\"Param\", sParam);\r\n            ExampleApplication.getInstance().startActivity(intent);\r\n\r\n        }\r\n        return 0;\r\n    }<\/pre>\n<h1><span class=\"ez-toc-section\" id=\"4%E3%80%81%E5%88%A9%E7%94%A8%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%AB%AF%E5%8F%91%E9%80%81%E6%9E%81%E5%85%89%E6%8E%A8%E9%80%81%E3%80%82\"><\/span>4\u3001\u5229\u7528\u670d\u52a1\u5668\u7aef\u53d1\u9001\u6781\u5149\u63a8\u9001\u3002<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u63a8\u9001\u5206\u6210\u4e24\u6b65\uff1a<\/p>\n<p>\u4e00\uff09 \u7ed1\u5b9aID\u7b49\u5173\u952e\u4fe1\u606f\u3002<\/p>\n<p>\u6781\u5149\u63a8\u9001\u662f\u5b89\u88c5\u5230\u8bbe\u5907\u540e\u4f1a\u81ea\u52a8\u751f\u6210 RegistrationID\uff0c\u901a\u8fc7\u51fd\u6570JPushInterface.getRegistrationID \u83b7\u53d6\u3002P2P\u6709\u81ea\u5df1\u7684ID\uff0cAPP\u53ef\u80fd\u4e5f\u6709\u81ea\u5df1\u8d26\u53f7\u6216\u8005ID\u3002\u4e3a\u4e86\u80fd\u4f7f\u6781\u5149\u63a8\u9001\u53ef\u4ee5\u51c6\u786e\u7684\u63a8\u9001\u81ea\u5b9a\u4e49\u6d88\u606f\u5230\u7279\u5b9a\u7684\u8bbe\u5907\u3002\u5c31\u9700\u8981\u5c06\u8fd9\u6837\u7684\u5173\u952e\u4fe1\u606f\u548cID\u5173\u8054\u5230\u4e00\u8d77\u3002\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u3002<\/p>\n<p>\u4e8c\uff09\u901a\u8fc7P2PID\u63a8\u9001\u81ea\u5b9a\u4e49\u6d88\u606f\u5230P2P\u8bbe\u5907\u3002<\/p>\n<p>\u5ba2\u6237\u7aef\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"lang:java decode:true\">\/**\r\n * \u5c01\u88c5\u7684\u6781\u5149\u63a8\u9001+\u76f4\u64ad\u6a21\u5757\u96c6\u6210\u7c7b\r\n *\/\r\npublic class MyJGPush {\r\n\r\n    private pgLibLiveMultiCapture mCapLive = null;\r\n    private pgLibLiveMultiRender mRndLive = null;\r\n    private pgLibJNINode mNode = null;\r\n\r\n    \/**\r\n     * \u7ed1\u5b9a\u91c7\u96c6\u7aef\u5b9e\u4f8b\r\n     * @param mLive \u91c7\u96c6\u7aef\r\n     *\/\r\n    public void AttachCapture(pgLibLiveMultiCapture mLive){\r\n        mNode = mLive.GetNode();\r\n        mCapLive = mLive;\r\n        mRndLive = null;\r\n    }\r\n\r\n    \/**\r\n     * \u7ed1\u5b9a\u64ad\u653e\u7aef\u5b9e\u4f8b\r\n     * @param mLive \u64ad\u653e\u7aef\r\n     *\/\r\n    public void AttachRender(pgLibLiveMultiRender mLive){\r\n        mNode = mLive.GetNode();\r\n        mCapLive = null;\r\n        mRndLive = mLive;\r\n    }\r\n\r\n    \/**\r\n     * P2P \u672c\u7aefID \u7ed1\u5b9a\u6781\u5149\u63a8\u9001\u8bbe\u5907ID\r\n     * @param regID \u6781\u5149\u63a8\u9001\u8bbe\u5907\u6ce8\u518cID\r\n     * @return P2P\u9519\u8bef\u7801\r\n     *\/\r\n    public int JGPushAttach(String regID){\r\n        if(isEmpty(regID)){\r\n            System.out.println(\"regID empty\");\r\n            return 2;\r\n        }\r\n        String sData = \"JGPushAttach?\" + regID;\r\n        int iErr = 0;\r\n        if(mCapLive!=null){\r\n            iErr = mCapLive.SvrRequest(sData, \"JGPushAttach\");\r\n        }else if(mRndLive!=null){\r\n            iErr = mRndLive.SvrRequest(sData, \"JGPushAttach\");\r\n        }else{\r\n            System.out.println(\"not Attach P2P Live\");\r\n            iErr = 2;\r\n        }\r\n        return iErr;\r\n    }\r\n\r\n    \/**\r\n     * \u63a8\u9001 Selection \u4fe1\u53f7\u5230 DesID\r\n     * @param DesID \u63a8\u9001\u76ee\u6807ID\r\n     * @param SrcID \u63a8\u9001\u6e90ID\r\n     * @param sParam \u81ea\u5b9a\u4e49\u4fe1\u606f\r\n     * @return P2P\u9519\u8bef\u7801\r\n     *\/\r\n    public int JGPushSelection(String DesID, String SrcID, String sParam){\r\n        return JGPush(\"Selection\",DesID,SrcID,sParam);\r\n    }\r\n\r\n    \/**\r\n     * \u63a8\u9001 Selection \u4fe1\u53f7\u5230 DesID\r\n     * @param DesID \u63a8\u9001\u76ee\u6807ID\r\n     * @param SrcID \u63a8\u9001\u6e90ID\r\n     * @param sParam \u81ea\u5b9a\u4e49\u4fe1\u606f\r\n     * @return P2P\u9519\u8bef\u7801\r\n     *\/\r\n    public int JGPushSelectionCancel(String DesID, String SrcID, String sParam){\r\n        return JGPush(\"SelectionCancel\",DesID,SrcID,sParam);\r\n    }\r\n    \/**\r\n     * \u63a8\u9001 Selection \u4fe1\u53f7\u5230 DesID\r\n     * @param sAction \u4fe1\u53f7\u540d\u79f0\r\n     * @param DesID \u63a8\u9001\u76ee\u6807ID\r\n     * @param SrcID \u63a8\u9001\u6e90ID\r\n     * @param sParam \u81ea\u5b9a\u4e49\u4fe1\u606f\r\n     * @return P2P\u9519\u8bef\u7801\r\n     *\/\r\n    public int JGPush( String sAction , String DesID, String SrcID, String sParam){\r\n        JSONObject msgJson = new JSONObject();\r\n        try {\r\n\r\n            msgJson.put(\"Action\",sAction);\r\n            msgJson.put(\"SrcID\",SrcID);\r\n            msgJson.put(\"DesID\",DesID);\r\n            msgJson.put(\"Param\",sParam);\r\n\r\n        } catch (JSONException e) {\r\n            e.printStackTrace();\r\n        }\r\n        String sData = \"JGPush?(User){\" + mNode.omlEncode(DesID) + \"}(Msg){\" + mNode.omlEncode(msgJson.toString()) + \"}\";\r\n        int iErr = 0;\r\n        if(mCapLive!=null){\r\n            iErr = mCapLive.SvrRequest(sData, sParam);\r\n        }else if(mRndLive!=null){\r\n            iErr = mRndLive.SvrRequest(sData, sParam);\r\n        }else{\r\n            iErr = 2;\r\n        }\r\n\r\n        return iErr;\r\n    }\r\n}<\/pre>\n<p>P2P\u670d\u52a1\u5668\u63d2\u4ef6\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"lang:default decode:true\">   ......\r\n\tpublic int PeerExtend(String sPeer, String sData, int iHandle) {\r\n\r\n\t\t\/\/ The sample code.\r\n\t\tint iInd = sData.indexOf('?');\r\n\t\tif (iInd &lt;= 0) {\r\n\t\t\treturn pgErrCode.PG_ERR_Normal;\r\n\t\t}\r\n\r\n\t\t\/\/ Check 'Forward' method.\r\n\t\tString sMeth = sData.substring(0, iInd);\r\n\t\tString sParam = sData.substring(iInd+1);\r\n\r\n\t\t\/\/m_Proc.PeerNotify(sPeer, \"UserExtend\", (\"Push: \" + sData));\r\n\t\t\/\/m_Proc.SetReplyData(\"Reply: \" + sData);\r\n\t\tOutString(\"PeerExtend sMeth = \" + sMeth);\r\n\r\n\t\tif (\"Forward\".equals(sMeth)) {\r\n\t\t\treturn MethodForward(sPeer, sData, iHandle);\r\n\t\t}\r\n\t\telse if (\"JGPush\".equals(sMeth)){\r\n\t\t\treturn JGPush(sPeer, sParam, iHandle);\r\n\t\t}\r\n\t\telse if (\"JGPushAttach\".equals(sMeth)){\r\n\t\t\treturn JGPushAttach(sPeer, sParam, iHandle);\r\n\t\t}\r\n\t\telse{\r\n\t\t\treturn pgErrCode.PG_ERR_Normal;\r\n\t\t}\r\n\t}\r\n  ......<\/pre>\n<p>\u81ea\u5b9a\u4e49\u63a8\u9001\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"lang:default decode:true \">......\r\nprivate static PushPayload buildPushObject_android_cid(String registrationId, String sMsgConten,String cid) {\r\n        if(isEmpty(registrationId)||isEmpty(sMsgConten)){\r\n            return null;\r\n        }\r\n        Collection&lt;String&gt; list =new LinkedList&lt;String&gt;();\r\n        list.add(registrationId);\r\n        PushPayload.Builder builder = PushPayload.newBuilder();\r\n        builder.setPlatform(Platform.android());\r\n        builder.setAudience(Audience.registrationId(registrationId));\r\n        builder.setMessage(Message.newBuilder()\r\n                        .setMsgContent(sMsgConten)\r\n                        \/\/.addExtra(\"from\", \"JPush\")\r\n                        .build());\r\n        if(!isEmpty(cid)){\r\n            builder.setCid(cid);\r\n        }\r\n        return builder.build();\r\n    }\r\n\r\n    private static void SendPushWithCid(int APP, String registrationId, String sMsgConten,String cid) {\r\n        \/\/OutString(\"SendPushWithCid APP = \" + APP + \" registrationId = \" + registrationId + \" sMsgConten = \"+ sMsgConten);\r\n        JPushClient jPushClient = new JPushClient(MASTER_SECRET[APP], APP_KEY[APP]);\r\n        \/\/OutString(\"SendPushWithCid  MASTER_SECRET[APP]=\"+ MASTER_SECRET[APP] + \" APP_KEY[APP] = \" + APP_KEY[APP] );\r\n        PushPayload pushPayload = buildPushObject_android_cid(registrationId, sMsgConten,cid);\r\n        if(pushPayload == null){\r\n            OutString(\"pushPayload = null\");\r\n            return;\r\n        }\r\n        try {\r\n\r\n            PushResult result = jPushClient.sendPush(pushPayload);\r\n            OutString(\"Got result - \" + result);\r\n        } catch (APIConnectionException e) {\r\n            OutString(\"Connection error. Should retry later. \"+ e.toString());\r\n        } catch (APIRequestException e) {\r\n            OutString(\"Error response from JPush server. Should review and fix it. \" + e .toString());\r\n            OutString(\"HTTP Status: \" + e.getStatus());\r\n            OutString(\"Error Code: \" + e.getErrorCode());\r\n            OutString(\"Error Message: \" + e.getErrorMessage());\r\n        }\r\n    }\r\n.......<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u6781\u5149\u63a8\u9001\u6d41\u7a0b\u56fe\u3002 \u6781\u5149\u63a8\u9001\u7b80\u5355\u6d41\u7a0b\u56fe 2\u3001\u9996\u5148\u67e5\u770b\u6781\u5149\u63a8\u9001\u7684\u6587\u6863\uff0c\u96c6\u6210\u6781\u5149\u63a8\u9001\u914d\u7f6e\u548c\u5e93\u3002 \u6781\u5149\u63a8&hellip; <a href=\"http:\/\/blog.peergine.com\/?p=130\" class=\"more-link read-more\" rel=\"bookmark\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">\u600e\u6837\u5229\u7528P2P\u670d\u52a1\u5668\u53d1\u9001\u6781\u5149\u63a8\u9001\uff1f<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-uncategorized","h-entry","h-as-article"],"views":1819,"_links":{"self":[{"href":"http:\/\/blog.peergine.com\/index.php?rest_route=\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.peergine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.peergine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.peergine.com\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.peergine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=130"}],"version-history":[{"count":2,"href":"http:\/\/blog.peergine.com\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":219,"href":"http:\/\/blog.peergine.com\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions\/219"}],"wp:attachment":[{"href":"http:\/\/blog.peergine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.peergine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.peergine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}