mirror of
https://github.com/jpush/jpush-phonegap-plugin.git
synced 2026-04-19 00:03:45 +08:00
initial project
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
package cn.jpush.phonegap;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaInterface;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.CordovaWebView;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import android.util.Log;
|
||||
import cn.jpush.android.api.BasicPushNotificationBuilder;
|
||||
import cn.jpush.android.api.CustomPushNotificationBuilder;
|
||||
import cn.jpush.android.api.JPushInterface;
|
||||
|
||||
public class JPushPlugin extends CordovaPlugin {
|
||||
private final static List<String> methodList =
|
||||
Arrays.asList(
|
||||
"setTags",
|
||||
"setTagAlias",
|
||||
"setAlias",
|
||||
"getIncoming",
|
||||
"setBasicPushNotificationBuilder",
|
||||
"setCustomPushNotificationBuilder");
|
||||
private ExecutorService executorService = Executors.newFixedThreadPool(1);
|
||||
private static JPushPlugin instance;
|
||||
|
||||
public static String incomingAlert;
|
||||
public static Map<String, String> incomingExtras;
|
||||
|
||||
public JPushPlugin() {
|
||||
instance = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
|
||||
super.initialize(cordova, webView);
|
||||
JPushInterface.setDebugMode(true);
|
||||
JPushInterface.init(cordova.getActivity().getApplicationContext());
|
||||
}
|
||||
|
||||
private static JSONObject notificationObject(String message,
|
||||
Map<String, String> extras) {
|
||||
JSONObject data = new JSONObject();
|
||||
try {
|
||||
data.put("message", message);
|
||||
data.put("extras", new JSONObject(extras));
|
||||
} catch (JSONException e) {
|
||||
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
static void raisePush(String message, Map<String, String> extras) {
|
||||
if (instance == null) {
|
||||
return;
|
||||
}
|
||||
JSONObject data = notificationObject(message, extras);
|
||||
String js = String
|
||||
.format("window.plugins.jPushPlugin.pushCallback(%s);",
|
||||
data.toString());
|
||||
try {
|
||||
instance.webView.sendJavascript(js);
|
||||
} catch (NullPointerException e) {
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(final String action, final JSONArray data,
|
||||
final CallbackContext callbackContext) throws JSONException {
|
||||
if (!methodList.contains(action)) {
|
||||
return false;
|
||||
}
|
||||
executorService.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Method method = JPushPlugin.class.getDeclaredMethod(action,
|
||||
JSONArray.class, CallbackContext.class);
|
||||
method.invoke(JPushPlugin.this, data, callbackContext);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
void setTags(JSONArray data, CallbackContext callbackContext) {
|
||||
HashSet<String> tags = new HashSet<String>();
|
||||
try {
|
||||
String tagStr = data.getString(0);
|
||||
String[] tagArr = tagStr.split(",");
|
||||
for (String tag : tagArr) {
|
||||
tags.add(tag);
|
||||
}
|
||||
Set<String> validTags = JPushInterface.filterValidTags(tags);
|
||||
JPushInterface.setTags(this.cordova.getActivity()
|
||||
.getApplicationContext(), validTags, null);
|
||||
callbackContext.success();
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("Error reading tags JSON");
|
||||
}
|
||||
}
|
||||
|
||||
void setAlias(JSONArray data, CallbackContext callbackContext) {
|
||||
Log.e("lincoln", "set alias start");
|
||||
try {
|
||||
String alias = data.getString(0);
|
||||
JPushInterface.setAlias(this.cordova.getActivity()
|
||||
.getApplicationContext(), alias, null);
|
||||
Log.e("lincoln", "set alias:" + alias);
|
||||
callbackContext.success();
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("Error reading alias JSON");
|
||||
}
|
||||
}
|
||||
|
||||
void setTagAlias(JSONArray data, CallbackContext callbackContext) {
|
||||
HashSet<String> tags = new HashSet<String>();
|
||||
String alias;
|
||||
try {
|
||||
alias = data.getString(0);
|
||||
JSONArray tagsArr = data.getJSONArray(1);
|
||||
for (int i = 0; i < tagsArr.length(); i++) {
|
||||
tags.add(tagsArr.getString(i));
|
||||
}
|
||||
|
||||
JPushInterface.setAliasAndTags(this.cordova.getActivity()
|
||||
.getApplicationContext(), alias, tags);
|
||||
callbackContext.success();
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("Error reading tagAlias JSON");
|
||||
}
|
||||
}
|
||||
|
||||
void getIncoming(JSONArray data, CallbackContext callBackContext) {
|
||||
String alert = JPushPlugin.incomingAlert;
|
||||
Map<String, String> extras = JPushPlugin.incomingExtras;
|
||||
|
||||
JSONObject jsonData = new JSONObject();
|
||||
try {
|
||||
jsonData.put("message", alert);
|
||||
jsonData.put("extras", new JSONObject(extras));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
callBackContext.success(jsonData);
|
||||
|
||||
JPushPlugin.incomingAlert = "";
|
||||
JPushPlugin.incomingExtras = new HashMap<String, String>();
|
||||
}
|
||||
|
||||
void setBasicPushNotificationBuilder(JSONArray data,
|
||||
CallbackContext callbackContext) {
|
||||
BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(
|
||||
this.cordova.getActivity());
|
||||
builder.developerArg0 = "Basic builder 1";
|
||||
JPushInterface.setPushNotificationBuilder(1, builder);
|
||||
JSONObject obj = new JSONObject();
|
||||
try {
|
||||
obj.put("id", 1);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
callbackContext.success(obj);
|
||||
}
|
||||
|
||||
void setCustomPushNotificationBuilder(JSONArray data,
|
||||
CallbackContext callbackContext) {
|
||||
CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder(
|
||||
this.cordova.getActivity(), R.layout.test_notitfication_layout,
|
||||
R.id.icon, R.id.title, R.id.text);
|
||||
builder.developerArg0 = "Custom Builder 1";
|
||||
builder.layoutIconDrawable = R.drawable.jpush_notification_icon;
|
||||
JPushInterface.setPushNotificationBuilder(2, builder);
|
||||
JSONObject obj = new JSONObject();
|
||||
try {
|
||||
obj.put("id", 2);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
callbackContext.success(obj);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package cn.jpush.phonegap;
|
||||
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import cn.jpush.android.api.JPushInterface;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
public class MyReceiver extends BroadcastReceiver {
|
||||
private static String TAG = "Client Receiver";
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
|
||||
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
|
||||
|
||||
}else if (JPushInterface.ACTION_UNREGISTER.equals(intent.getAction())){
|
||||
|
||||
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
|
||||
handlingReceivedMessage(intent);
|
||||
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
|
||||
|
||||
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
|
||||
handlingNotificationOpen(context,intent);
|
||||
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
|
||||
|
||||
} else {
|
||||
Log.d(TAG, "Unhandled intent - " + intent.getAction());
|
||||
}
|
||||
|
||||
}
|
||||
private void handlingReceivedMessage(Intent intent) {
|
||||
String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE);
|
||||
Map<String,String> extras = getNotificationExtras(intent);
|
||||
|
||||
JPushPlugin.raisePush(msg, extras);
|
||||
}
|
||||
private void handlingNotificationOpen(Context context,Intent intent){
|
||||
String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT);
|
||||
Map<String,String> extras = getNotificationExtras(intent);
|
||||
|
||||
Intent launch = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
|
||||
launch.addCategory(Intent.CATEGORY_LAUNCHER);
|
||||
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
|
||||
JPushPlugin.incomingAlert = alert;
|
||||
JPushPlugin.incomingExtras = extras;
|
||||
|
||||
context.startActivity(launch);
|
||||
}
|
||||
private Map<String, String> getNotificationExtras(Intent intent) {
|
||||
Map<String, String> extrasMap = new HashMap<String, String>();
|
||||
|
||||
for (String key : intent.getExtras().keySet()) {
|
||||
if (!IGNORED_EXTRAS_KEYS.contains(key)) {
|
||||
Log.e("key","key:"+key);
|
||||
extrasMap.put(key, intent.getStringExtra(key));
|
||||
}
|
||||
}
|
||||
return extrasMap;
|
||||
}
|
||||
private static final List<String> IGNORED_EXTRAS_KEYS =
|
||||
Arrays.asList("cn.jpush.android.TITLE","cn.jpush.android.MESSAGE","cn.jpush.android.APPKEY");
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 3.3 KiB |
Binary file not shown.
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="horizontal" >
|
||||
<ImageView android:id="@+id/icon"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
/>
|
||||
<LinearLayout
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="wrap_content"
|
||||
android:orientation="vertical"
|
||||
>
|
||||
<TextView android:id="@+id/title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
/>
|
||||
<TextView android:id="@+id/text"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
/>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user