{"id":342,"date":"2022-04-07T19:18:53","date_gmt":"2022-04-07T11:18:53","guid":{"rendered":"https:\/\/fugary.com\/?p=342"},"modified":"2022-04-08T17:58:51","modified_gmt":"2022-04-08T09:58:51","slug":"cordova%e6%9e%81%e5%85%89%e6%8e%a8%e9%80%81%e5%8e%82%e5%95%86%e9%80%9a%e9%81%93%e6%89%93%e4%b8%8d%e5%bc%80app%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/fugary.com\/?p=342","title":{"rendered":"Cordova\u6781\u5149\u63a8\u9001\u5382\u5546\u901a\u9053\u6253\u4e0d\u5f00APP\u4ee5\u53ca\u4e0d\u54cd\u5e94\u4e8b\u4ef6\u95ee\u9898"},"content":{"rendered":"<p>\u6700\u8fd1\u5728\u505a<code>Cordova<\/code>\u7684\u6d88\u606f\u63a8\u9001\uff0c\u5728\u96c6\u6210\u5382\u5546\u901a\u9053\u7684\u65f6\u5019\u53d1\u73b0\u4e0d\u80fd\u5728<code>js<\/code>\u4e2d\u4f7f\u7528<code>jpush.openNotification<\/code>\u6216\u8005<code>jpush.receiveNotification<\/code>\u4e8b\u4ef6\u6765\u76d1\u542c\u6536\u5230\u6d88\u606f\u63d0\u793a\u3002<\/p>\n<p>\u56e0\u4e3a\u5382\u5546\u901a\u9053\u5e76\u6ca1\u6709\u8d70<code>JPushEventReceiver.onNotifyMessageOpened<\/code>\u65b9\u6cd5\uff0c\u56e0\u6b64\u6ca1\u6709\u4e8b\u4ef6\u53d1\u51fa\u6765\u3002<\/p>\n<p>\u76ee\u524d<code>cordova<\/code>\u7248\u672c\u7684<code>jpush sdk<\/code>\u662f<code>4.2.8<\/code>\u7248\u672c\uff0c\u636e\u5b98\u65b9\u8bf4<code>4.6.x<\/code>\u7684\u5382\u5546\u901a\u9053\u5c31\u652f\u6301<\/p>\n<p>\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/jpush\/jpush-phonegap-plugin\">https:\/\/github.com\/jpush\/jpush-phonegap-plugin<\/a><\/p>\n<p><strong>\u524d\u63d0\uff1a\u5df2\u7ecf\u914d\u7f6e\u597d\u5382\u5546\u901a\u9053<\/strong><\/p>\n<p>\u672c\u6587\u662f\u4ecb\u7ecd\u539f\u7406\uff0c\u5982\u679c\u76f4\u63a5\u4f7f\u7528\uff0c\u53ef\u4ee5\u76f4\u63a5\u8df3\u5230\u3010<code>GitHub<\/code>\u5730\u5740\u3011\u7ae0\u8282\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee<code>GitHub<\/code>\u5730\u5740\uff1a<\/p>\n<p><code>GitHub<\/code>\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/fugary\/cordova-plugin-jpush-custom\">https:\/\/github.com\/fugary\/cordova-plugin-jpush-custom<\/a><\/p>\n<h3>\u76d1\u542c\u65b9\u5f0f<\/h3>\n<p>\u73b0\u6709\u542f\u52a8\u903b\u8f91<\/p>\n<pre><code class=\"language-js\">window.JPush.init()\nwindow.JPush.setDebugMode(true)\nwindow.setTimeout(getRegistrationID, 1000) \/\/ \u83b7\u53d6RegistrationID\ndocument.addEventListener(&#039;jpush.openNotification&#039;, evt =&gt; {\n    \/\/ \u4e8b\u4ef6\u5904\u7406\n}, false)\ndocument.addEventListener(&#039;jpush.receiveNotification&#039;, evt =&gt; {\n    \/\/ \u4e8b\u4ef6\u5904\u7406\n}, false)<\/code><\/pre>\n<p>\u914d\u7f6e\u597d\u5382\u5546\u901a\u9053\u4e4b\u540e\uff0c\u672c\u5730\u4ee5<code>vivo<\/code>\u624b\u673a\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u63a8\u9001\u6d88\u606f\uff0c\u4f46\u662f\u70b9\u6d88\u606f\u6ca1\u6709\u53cd\u5e94\uff0c\u6253\u4e0d\u5f00<code>APP<\/code>\u3002<\/p>\n<h3>\u53d1\u9001\u7aef\u4fee\u6539<\/h3>\n<p>\u76ee\u524d\u4f7f\u7528<code>Java<\/code>\u8c03\u7528\u670d\u52a1\u7aef<code>SDK<\/code>\u53d1\u9001\u6d88\u606f\uff0c \u9700\u8981\u6dfb\u52a0<code>uri_activity<\/code>\u5c5e\u6027<code>setUriActivity(&quot;com.xxx.MainActivity&quot;)<\/code>\u5c31\u53ef\u4ee5\u5b9e\u73b0\u70b9\u51fb\u6d88\u606f\u53ef\u4ee5\u6253\u5f00<code>APP<\/code>\u4e86\u3002<\/p>\n<pre><code class=\"language-java\">JPushClient jPushClient = new JPushClient(&quot;MasterSecret&quot;, &quot;AppKey&quot;);\nString messageTitle = &quot;Test\u6d88\u606f\u6807\u9898&quot;;\nString messageContent = &quot;Test\u6d88\u606f\u5185\u5bb9\u6d88\u606f\u5185\u5bb9&quot;;\nString registrationId = &quot;1507bfd3f7366d1c33b&quot;;\nString platform = &quot;android&quot;;\nboolean isAndroid = platform.equalsIgnoreCase(&quot;android&quot;);\n\/\/ \u6dfb\u52a0\u4e00\u4e9bextras\u4fe1\u606f\nMap&lt;String, String&gt; extras = new HashMap&lt;&gt;();\nextras.put(&quot;orderNo&quot;, &quot;xxxxxx&quot;);\n\/\/ iOS\u6d88\u606f\nIosAlert alert = IosAlert.newBuilder().setTitleAndBody(messageTitle, &quot;&quot;, messageContent).build();\nNotification androidNotification = Notification.newBuilder()\n    .addPlatformNotification(AndroidNotification.newBuilder()\n                             .setAlert(messageContent).setTitle(messageTitle).addExtras(extras)\n                             .setUriActivity(&quot;com.xxx.MainActivity&quot;) \/\/ cordova\u7684activity\u5168\u79f0\uff0cpackageName.MainActivity\n                             .build()).build();\nPushPayload pushPayload = PushPayload.newBuilder()\n    .setAudience(Audience.registrationId(registrationId))\n    .setPlatform(isAndroid ? Platform.android() : Platform.ios())\n    .setNotification(isAndroid ? androidNotification : Notification.ios(alert, extras))\n    .setOptions(getVivoOptions())\n    .build();\nPushResult pushResult = jPushClient.sendPush(pushPayload);<\/code><\/pre>\n<p>\u7136\u540e\u518d\u6b21\u5c1d\u8bd5\uff0c\u6536\u5230\u6d88\u606f\u540e\u53ef\u4ee5\u5728<code>vivo<\/code>\u624b\u673a\u4e0a\u6253\u5f00\u6d88\u606f\u4e86\u3002<\/p>\n<p>\u4e0d\u8fc7\u8fd8\u662f\u6ca1\u6709\u89e6\u53d1\u4e8b\u4ef6\uff0c\u6ca1\u6cd5\u5b9a\u5236\u6536\u5230\u6d88\u606f\u6253\u5f00<code>APP<\/code>\u4e4b\u540e\u7684\u903b\u8f91\u3002<\/p>\n<h3>\u5f00\u53d1Cordova\u63d2\u4ef6<\/h3>\n<p>\u200b   \u53c2\u8003\uff1a<a href=\"https:\/\/fugary.com\/?p=339\">https:\/\/fugary.com\/?p=339<\/a><\/p>\n<p>\u200b   \u56e0\u4e3a\u6d88\u606f\u53d1\u9001\u5230<code>Activity<\/code>\u4e4b\u540e\uff0c\u53ef\u4ee5\u7528<code>Intent<\/code>\u83b7\u53d6\u5230\uff0c\u4e0d\u8fc7\u521a\u6253\u5f00<code>APP<\/code>\u83b7\u53d6\u5230\u6d88\u606f\u7684\u65f6\u5019<code>Cordova<\/code>\u73af\u5883\u5e76\u6ca1\u6709\u521d\u59cb\u5316\u597d\uff0c<code>WebView<\/code>\u4e5f\u6ca1\u6709\u52a0\u8f7d\u5b8c\uff0c\u8fd9\u65f6\u5019\u4e0d\u80fd\u76f4\u63a5\u53d1\u51fa\u4e8b\u4ef6\u3002\u56e0\u6b64\u76ee\u524d\u601d\u8def\u662f\u5f00\u53d1\u4e00\u4e2a<code>Cordova<\/code>\u63d2\u4ef6\uff0c\u5728\u521d\u59cb\u5316\u7684\u65f6\u5019\u628a<code>Intent<\/code>\u4e2d\u7684\u6570\u636e\u53d6\u51fa\u6765\u5b58\u5728Java\u7684\u5c5e\u6027\u4e2d\uff0c\u7136\u540e\u518d\u521d\u59cb\u5316\u5b8c\u6210\u540e\u8c03\u7528\u4e00\u4e2a<code>JS<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u6570\u636e\uff0c\u5728<code>JS<\/code>\u4e2d\u89e6\u53d1\u6781\u5149\u63a8\u9001\u7684\u4e8b\u4ef6<code>jpush.receiveNotification<\/code>\u4e8b\u4ef6\uff0c\u8fd9\u6837\u539f\u6765\u76d1\u542c\u7684\u4e8b\u4ef6\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86\u3002<\/p>\n<h4>\u63d2\u4ef6Java\u4ee3\u7801<\/h4>\n<p>\u53c2\u8003<code>Intent<\/code>\u6570\u636e\u83b7\u53d6\uff1a<a href=\"https:\/\/docs.jiguang.cn\/jpush\/practice\/intent\">https:\/\/docs.jiguang.cn\/jpush\/practice\/intent<\/a><\/p>\n<p>\u63d2\u4ef6\u4e3b\u8981<code>Java<\/code>\u4ee3\u7801\uff0c\u6570\u636e\u5148\u5b58\u50a8\u5728<code>extraMessage<\/code>\u4e2d\uff0c\u7136\u540e\u901a\u8fc7<code>checkReceiveMessage<\/code>\u65b9\u6cd5\u53d6\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-java\">public class JPushCustomPlugin extends CordovaPlugin {\n\n    private static final String TAG = JPushCustomPlugin.class.getSimpleName();\n    private Activity cordovaActivity;\n    private JPushCustomPlugin instance;\n    private String extraMessage;\n    @Override\n    public void initialize(CordovaInterface cordova, CordovaWebView webView) {\n        super.initialize(cordova, webView);\n        cordovaActivity = cordova.getActivity();\n        instance = this;\n        LOG.i(TAG, &quot;init JPushCustomPlugin ...................................&quot;);\n        processJPushMessage(cordovaActivity);\n    }\n    protected void processJPushMessage(Activity cordovaActivity) {\n        if (instance == null) {\n            return;\n        }\n        extraMessage = getNotificationExtras(cordovaActivity.getIntent());\n        LOG.i(TAG, &quot;JPush Message: %s&quot;, extraMessage);\n    }\n    protected String getNotificationExtras(Intent intent) {\n        String data = null;\n        if (intent != null) {\n            \/\/\u83b7\u53d6\u534e\u4e3a\u5e73\u53f0\u9644\u5e26\u7684jpush\u4fe1\u606f\n            if (intent.getData() != null) {\n                data = intent.getData().toString();\n            }\n            \/\/\u83b7\u53d6fcm\/oppo\/\u5c0f\u7c73\/vivo\/\u9b45\u65cf \u5e73\u53f0\u9644\u5e26\u7684jpush\u4fe1\u606f\n            if (TextUtils.isEmpty(data) &amp;&amp; intent.getExtras() != null) {\n                data = intent.getExtras().getString(&quot;JMessageExtra&quot;);\n            }\n        }\n        return data;\n    }\n    @Override\n    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {\n        if (action.equals(&quot;coolMethod&quot;)) {\n            String message = args.getString(0);\n            this.coolMethod(message, callbackContext);\n            return true;\n        }\n        if (action.equals(&quot;checkReceiveMessage&quot;)) {\n            this.checkReceiveMessage(callbackContext);\n            return true;\n        }\n        return false;\n    }\n    private void coolMethod(String message, CallbackContext callbackContext) {\n        if (message != null &amp;&amp; message.length() &gt; 0) {\n            callbackContext.success(message);\n        } else {\n            callbackContext.error(&quot;Expected one non-empty string argument.&quot;);\n        }\n    }\n    private void checkReceiveMessage(CallbackContext callbackContext) throws JSONException {\n        if (this.extraMessage != null &amp;&amp; !this.extraMessage.isEmpty()) {\n            JSONObject json = new JSONObject(this.extraMessage);\n            callbackContext.success(json);\n            this.extraMessage = null;\n        } else {\n            callbackContext.error(&quot;No push message.&quot;);\n        }\n    }\n}<\/code><\/pre>\n<h4>\u63d2\u4ef6JS\u4ee3\u7801<\/h4>\n<p>\u6570\u636e\u83b7\u53d6\u7684<code>JS<\/code>\u4ee3\u7801\uff0c<code>JS<\/code>\u901a\u8fc7<code>Cordova<\/code>\u73af\u5883\u76f4\u63a5\u8c03\u7528\u5230<code>Java<\/code>\u7684<code>checkReceiveMessage<\/code>\u65b9\u6cd5\u53d6\u5230\u6570\u636e\uff0c\u5e76\u8f6c\u53d1\u5230<code>jpush.receiveNotification<\/code>\u4e8b\u4ef6\u4e2d\uff1a<\/p>\n<pre><code class=\"language-js\">var exec = require(&#039;cordova\/exec&#039;);\nvar JPushCustomPlugin = function () {\n    \/\/ noop\n};\n\/\/ \u4fdd\u7559\u6d4b\u8bd5\u7528\nJPushCustomPlugin.prototype.coolMethod = function (arg0, success, error) {\n    exec(success, error, &#039;JPushCustomPlugin&#039;, &#039;coolMethod&#039;, [arg0]);\n};\nvar receiveMessageInAndroidCallback = function (data) {\n    var eventData = {\n        extras: data.n_extras,\n        alert: data.n_content,\n        title: data.n_title\n    };\n    \/\/ \u8c03\u7528\n    cordova.fireDocumentEvent(&quot;jpush.receiveNotification&quot;, eventData);\n};\nJPushCustomPlugin.prototype.checkReceiveMessage = function () {\n    exec(receiveMessageInAndroidCallback, function (err) {\n        console.log(err);\n    }, &#039;JPushCustomPlugin&#039;, &#039;checkReceiveMessage&#039;, []);\n};\n\n\/**\n * \u81ea\u5b9a\u4e49cordova\u63d2\u4ef6\n *\n * @type {JPushCustomPlugin}\n *\/\nmodule.exports = new JPushCustomPlugin();\n<\/code><\/pre>\n<h4>\u5177\u4f53\u4f7f\u7528<\/h4>\n<p>\u9700\u8981\u5728\u6781\u5149\u63a8\u9001\u521d\u59cb\u5316\u7b49\u5b8c\u6210\u4e4b\u540e\uff0c\u6dfb\u52a0<code>JPushCustomPlugin<\/code>\u7684\u6d88\u606f\u83b7\u53d6\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-js\">if (window.JPushCustomPlugin) {\n    window.JPushCustomPlugin.checkReceiveMessage()\n}<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\uff0c\u5b9e\u73b0\u4e86\u70b9\u51fb\u6d88\u606f\u6253\u5f00<code>APP<\/code>\u89e6\u53d1<code>jpush.receiveNotification<\/code>\u4e8b\u4ef6\uff0c\u540e\u7eed\u76d1\u542c\u903b\u8f91\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u5904\u7406\u3002<\/p>\n<h3>Github\u5730\u5740<\/h3>\n<p>\u76f8\u5173\u6587\u4ef6\u63d0\u4ea4\u5230<code>GitHub<\/code>\u4e2d\uff0c\u65b9\u4fbf\u76f4\u63a5\u7528<code>Cordova<\/code>\u8c03\u7528<code>GitHub<\/code>\u4ed3\u5e93\u5b89\u88c5\u63d2\u4ef6<\/p>\n<p><code>GitHub<\/code>\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/fugary\/cordova-plugin-jpush-custom\">https:\/\/github.com\/fugary\/cordova-plugin-jpush-custom<\/a><\/p>\n<p>\u5b89\u88c5\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"language-sh\">cordova plugin add https:\/\/github.com\/fugary\/cordova-plugin-jpush-custom.git<\/code><\/pre>\n<p>\u7136\u540e\u4f7f\u7528\uff0c\u9700\u8981\u5728<code>JPush.init()<\/code>\u7b49<code>JPush<\/code>\u76f8\u5173\u4ee3\u7801\u4e4b\u540e<\/p>\n<pre><code class=\"language-js\">if (window.JPushCustomPlugin) {\n    window.JPushCustomPlugin.checkReceiveMessage()\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u505aCordova\u7684\u6d88\u606f\u63a8\u9001\uff0c\u5728\u96c6\u6210\u5382\u5546\u901a\u9053\u7684\u65f6\u5019\u53d1\u73b0\u4e0d\u80fd\u5728js\u4e2d\u4f7f\u7528jpush.openNotifica [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[26,36,73,72],"_links":{"self":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/342"}],"collection":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=342"}],"version-history":[{"count":3,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/342\/revisions"}],"predecessor-version":[{"id":345,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/342\/revisions\/345"}],"wp:attachment":[{"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}