Skip to content

Commit

Permalink
升级版本
Browse files Browse the repository at this point in the history
Android SDK 3.4.5 - 2017.07.24更新
IOS SDK 2.2.6 - 2017.06.27更新
优化功能
统一安卓与iOS实现逻辑,都走cordova.fireDocumentEvent触发事件
增加等待队列,在JS初始化时统一回调,保证事件不丢失
  • Loading branch information
ahzmr committed Sep 7, 2017
1 parent bcefcc9 commit 9a0f965
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 155 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Change Log
## 2.0.0 - 2017-09-07
### 升级版本
- Android SDK 3.4.5 - 2017.07.24更新
- IOS SDK 2.2.6 - 2017.06.27更新

### 优化功能
- 统一安卓与iOS实现逻辑,都走cordova.fireDocumentEvent触发事件
- 增加等待队列,在JS初始化时统一回调,保证事件不丢失

## 1.0.0 - 2017-03-28
### 增加功能
- 增加IOS实现

## 0.1.0 - 2016-08-29
### 增加功能
- 增加安卓实现
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Cordova MiPush Plugin

[![release](https://img.shields.io/badge/release-0.1.0-blue.svg)](https://github.com/ParadiseHell/mipush-cordova-plugin)
[![platforms](https://img.shields.io/badge/platforms-Android-lightgrey.svg)](https://github.com/ParadiseHell/mipush-cordova-plugin)
[![release](https://img.shields.io/badge/release-0.1.0-blue.svg)](https://github.com/wenin819/cordova-plugin-mipush)
[![platforms](https://img.shields.io/badge/platforms-Android-lightgrey.svg)](https://github.com/wenin819/cordova-plugin-mipush)

支持Android、IOS的Cordova小米推送插件

## 集成步骤

- 通过 Cordova Plugins 安装,要求 Cordova CLI 5.0+:

cordova plugin add mipush-cordova-plugin --variable MI_PUSH_APP_KEY=your_mipush_appkey --variable MI_PUSH_APP_ID=your_mipush_appid --variable MI_PUSH_APP_IOS_ID=your_mipush_appid --variable MI_PUSH_APP_IOS_KEY=your_mipush_appkey
cordova plugin add cordova-plugin-mipush --variable MI_PUSH_APP_KEY=your_mipush_appkey --variable MI_PUSH_APP_ID=your_mipush_appid --variable MI_PUSH_APP_IOS_ID=your_mipush_appid --variable MI_PUSH_APP_IOS_KEY=your_mipush_appkey

- 通过Url安装:

cordova plugin add https://github.com/ParadiseHell/mipush-cordova-plugin.git --variable MI_PUSH_APP_KEY=your_mipush_appkey --variable MI_PUSH_APP_ID=your_mipush_appid --variable MI_PUSH_APP_IOS_ID=your_mipush_appid --variable MI_PUSH_APP_IOS_KEY=your_mipush_appkey
cordova plugin add https://github.com/wenin819/cordova-plugin-mipush.git --variable MI_PUSH_APP_KEY=your_mipush_appkey --variable MI_PUSH_APP_ID=your_mipush_appid --variable MI_PUSH_APP_IOS_ID=your_mipush_appid --variable MI_PUSH_APP_IOS_KEY=your_mipush_appkey

- 或者下载本地安装

Expand Down Expand Up @@ -65,9 +65,9 @@ Cordova CLI 的具体用法可参考 [Cordova CLI 官方文档](https://cordova.
## 感谢

- [极光推送cordova插件](https://github.com/jpush/jpush-phonegap-plugin)
- [cordova小米推送插件 安卓版实现](https://github.com/ParadiseHell/mipush-cordova-plugin)


## 更多

- 本项目参考极光推送cordova插件编写
- 若有问题,请发我邮件:[email protected]
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
{
"name": "mipush-cordova-plugin",
"version": "1.0.2",
"description": "Cordova MiPush Plugin",
"name": "cordova-plugin-mipush",
"version": "2.0.0",
"description": "Cordova/Phonegap MiPush Plugin",
"cordova": {
"id": "cordova-plugin-mipush",
"platforms": [
"android"
"android",
"ios"
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/ParadiseHell/mipush-cordova-plugin.git"
"url": "git+https://github.com/wenin819/cordova-plugin-mipush.git"
},
"keywords": [
"cordova",
"phonegap",
"mipush",
"ecosystem:cordova",
"cordova-android"
"cordova-android",
"cordova-ios"
],
"engines": [
{
"name": "cordova",
"version": ">=3.0"
}
],
"author": "ChengTao",
"author": "https://github.com/wenin819",
"license": "Apache 2.0",
"bugs": {
"url": "https://github.com/ParadiseHell/mipush-cordova-plugin/issues"
"url": "https://github.com/wenin819/cordova-plugin-mipush/issues"
},
"homepage": "https://github.com/ParadiseHell/mipush-cordova-plugin#readme"
"homepage": "https://github.com/wenin819/cordova-plugin-mipush#readme"
}
6 changes: 3 additions & 3 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-mipush"
version="0.1.0">
version="2.0.0">

<name>MiPush</name>
<description>Cordova MiPush Plugin</description>
<author>ChengTao</author>
<author>https://github.com/wenin819</author>
<license>Apache 2.0</license>
<keywords>cordova,mipush</keywords>

Expand Down Expand Up @@ -130,7 +130,7 @@
<meta-data android:name="MiPushAppId" android:value="$MI_PUSH_APP_IDmipush"></meta-data>
</config-file>

<source-file src="src/android/MiPush_SDK_Client_3_2_2.jar" target-dir="libs" />
<source-file src="src/android/MiPush_SDK_Client_3_4_5.jar" target-dir="libs" />
<source-file src="src/android/MiPushPlugin.java" target-dir="src/com/ct/cordova/mipush" />
<source-file src="src/android/MiPushReceiver.java" target-dir="src/com/ct/cordova/mipush" />

Expand Down
178 changes: 101 additions & 77 deletions src/android/MiPushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushMessage;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
Expand All @@ -19,22 +21,23 @@
import org.json.JSONObject;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MiPushPlugin extends CordovaPlugin {
public static final String CallbackJsName = "callbackJs";
private static String TAG = "MiPushPlugin";
private static String MI_PUSH = "mipush";
private static Activity activity;
private static MiPushPlugin instance;
public static String openNotificationTitle;
public static String openNotificationDescription;
public static String openNotificationExtras;
public static Map<String, String> openNotificationExtraMap;

private static final List<String> callbackJsQueue = new ArrayList<>();
private static boolean hasInit = false;

private final List<String> methodList =
Arrays.asList(
"init",
Expand All @@ -59,11 +62,6 @@ public MiPushPlugin() {
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
activity = cordova.getActivity();
//如果是首次启动,并且点击的通知消息,则处理消息
if (openNotificationTitle != null) {
onNotificationMessageClickedCallBack(openNotificationTitle, openNotificationDescription,
openNotificationExtras, openNotificationExtraMap);
}
}

@Override
Expand Down Expand Up @@ -114,6 +112,7 @@ public void init(JSONArray data, CallbackContext callbackContext) {
Log.e(TAG, "-------APP_KEY-------" + APP_KEY + "------APP_ID----" + APP_ID);
MiPushClient.registerPush(activity, APP_ID, APP_KEY);
Log.e(TAG, "-------------init------------------");
hasInit = true;
callbackContext.success();
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -283,55 +282,20 @@ public void unSetTopic(JSONArray data, CallbackContext callbackContext) {

/**
* 接受到消息
*
* @param title
* @param description
* @param extras
* @param extra
*/
public static void onNotificationMessageArrivedCallBack(String title, String description, String extras, Map<String, String> extra) {
public static void onNotificationMessageArrivedCallBack(MiPushMessage message) {
Log.e(TAG, "-------------onNotificationMessageArrivedCallBack------------------");
if (instance == null) {
return;
}
JSONObject object = getNotificationJsonObject(title, description, extras, extra);
Log.e(TAG, "-------------onNotificationMessageArrivedCallBack------------------" + object.toString());
String format = "window.plugins.MiPushPlugin.onNotificationMessageArrivedCallBack(%s);";
final String js = String.format(format, object.toString());
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
instance.webView.loadUrl("javascript:" + js);
}
});
JSONObject object = getNotificationJsonObject(message);
callbackWithType("notificationMessageArrived", object);
}

/**
* 用户点击
*
* @param title
* @param description
* @param extras
* @param extra
*/
public static void onNotificationMessageClickedCallBack(String title, String description, String extraStr, Map<String, String> extra) {
public static void onNotificationMessageClickedCallBack(MiPushMessage message) {
Log.e(TAG, "-------------onNotificationMessageClickedCallBack------------------");
if (instance == null) {
return;
}
JSONObject object = getNotificationJsonObject(title, description, extraStr, extra);
Log.e(TAG, "-------------onNotificationMessageClickedCallBack------------------" + object.toString());
String format = "window.plugins.MiPushPlugin.onNotificationMessageClickedCallBack(%s);";
final String js = String.format(format, object.toString());
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
instance.webView.loadUrl("javascript:" + js);
}
});
MiPushPlugin.openNotificationTitle = null;
MiPushPlugin.openNotificationDescription = null;
MiPushPlugin.openNotificationExtras = null;
JSONObject object = getNotificationJsonObject(message);
callbackWithType("notificationMessageClicked", object);
}

/**
Expand All @@ -341,52 +305,112 @@ public void run() {
*/
public static void onReceiveRegisterResultCallBack(String regId) {
Log.e(TAG, "-------------onReceiveRegisterResultCallBack------------------" + regId);
if (instance == null) {
return;
}
try {
JSONObject object = new JSONObject();
object.put("regId", regId);
String format = "window.plugins.MiPushPlugin.onReceiveRegisterResultCallBack(%s);";
final String js = String.format(format, object.toString());
callbackWithType("receiveRegisterResult", object);
handleCallbackJsQueue();
} catch (JSONException e) {
e.printStackTrace();
}
}

/**
* 返回数据到前端
* @param type 事件类型
* @param data 事件数据
*/
public static void callbackWithType(String type, Map<String, Object> data) {
callbackWithType(type, new JSONObject(data));
}

/**
* 返回数据到前端
* @param type 事件类型
* @param data 事件数据
*/
public static void callbackWithType(final String type, JSONObject data) {
Log.e(TAG, "-------------callbackWithType------------------" + type);
final String js = getCallbackJS(type, data);

if (instance != null && hasInit) {
Log.e(TAG, "callbackWithType run: " + type);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
instance.webView.loadUrl("javascript:" + js);
}
});
} catch (JSONException e) {
e.printStackTrace();
} else {
callbackJsQueue.add(js);
}
}

private static void handleCallbackJsQueue() {
if(!hasInit || callbackJsQueue.isEmpty()) {
return;
}
for (final String js : callbackJsQueue) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
instance.webView.loadUrl("javascript:" + js);
}
});
}
callbackJsQueue.clear();
}

/**
* 获取消息通知的回调JS
* @param type 类型
* @param data 数据
*/
private static String getCallbackJS(final String type, JSONObject data) {
String format = "cordova.fireDocumentEvent('mipush.%s', %s);";
return String.format(format, type, data.toString());
}

/**
* 获取消息通知的Json对象
*
* @param title
* @param description
* @param extraStr
* @param extra
* @return
* @param message 推送消息
*/
public static JSONObject getNotificationJsonObject(String title, String description, String extraStr, Map<String, String> extra) {
public static JSONObject getNotificationJsonObject(MiPushMessage message) {
JSONObject data = new JSONObject();
try {
data.put("title", title);
data.put("description", description);
if (null != extra) {
for (Map.Entry<String, String> entry : extra.entrySet()) {
data.put(entry.getKey(), entry.getValue());
data.put("messageId", message.getMessageId());
data.put("passThrough", message.getPassThrough());
data.put("messageType", message.getMessageType());
if(!TextUtils.isEmpty(message.getAlias())) {
data.put("alias", message.getAlias());
}

if(!TextUtils.isEmpty(message.getUserAccount())) {
data.put("user_account", message.getUserAccount());
}
if (null != extraStr && extraStr.indexOf('{') == 0) {
JSONObject extrasObject = new JSONObject(extraStr);
Iterator it = extrasObject.keys();
while (it.hasNext()) {
String key = (String) it.next();
String value = extrasObject.getString(key);
data.put(key, value);

if(!TextUtils.isEmpty(message.getTopic())) {
data.put("topic", message.getTopic());
}

data.put("content", message.getContent());
if(!TextUtils.isEmpty(message.getDescription())) {
data.put("description", message.getDescription());
}

if(!TextUtils.isEmpty(message.getTitle())) {
data.put("title", message.getTitle());
}

data.put("isNotified", message.isNotified());
data.put("notifyId", message.getNotifyId());
data.put("notifyType", message.getNotifyType());
if(!TextUtils.isEmpty(message.getCategory())) {
data.put("category", message.getCategory());
}

if(message.getExtra() != null) {
data.put("extra", new JSONObject(message.getExtra()));
}
} catch (JSONException e) {
e.printStackTrace();
Expand Down
Loading

0 comments on commit 9a0f965

Please sign in to comment.