initial project

This commit is contained in:
Lincoln 2013-10-18 11:14:33 +08:00
commit b6aa2e8d8c
9 changed files with 505 additions and 0 deletions

32
README.md Normal file
View File

@ -0,0 +1,32 @@
## JPush PhoneGap Plugin ##
**MarkdownPad** 是 Windows 平台上一个功能完善的 Markdown 编辑器。
### Installation ###
1.使用 phonegap 或者 cordova cli 添加插件:
`cordova plugin add https://github.com/jpush/jpush-phonegap-plugin.git`
2.修改www/config.xml文件添加或者覆盖以下字段
###Android 手工安装###
1.复制src/android/*.java 到cn/jpush/phonega/目录下(即cn.jpush.phonegap的包下)
2.复制src/android/ .so
3.复制 .jar
4.复制 .xml
5.复制 .png
6.修改AndroidManifest.xml 在manifest节点下添加以下权限
7.修改AndroidManifest.xml 在manifest/application节点下添加SDK相关组件声明

136
plugin.xml Normal file
View File

@ -0,0 +1,136 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cn.jpush.phonegap.JPushPlugin"
version="1.5.2">
<name>JPush Plugin</name>
<description>JPush for cordova plugin</description>
<author>JPush</author>
<keywords>JPush,push</keywords>
<license>Apache 2.0 License</license>
<engines>
<engine name="cordova" version=">=3.0"/>
</engines>
<!-- android -->
<platform name="android">
<js-module src="www/JPushPlugin.js" name="JPushPlugin">
<clobbers target="window.plugins.jPushPlugin"/>
</js-module>
<config-file target="res/xml/config.xml" parent="/*">
<feature name="JPushPlugin">
<param name="android-package" value="cn.jpush.phonegap.JPushPlugin"/>
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest">
<!-- Required 一些系统要求的权限,如访问网络等-->
<uses-permission android:name="$PACKAGE_NAME.permission.JPUSH_MESSAGE"/>
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<permission
android:name="$PACKAGE_NAME.permission.JPUSH_MESSAGE"
android:protectionLevel="signature"/>
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<!-- Required SDK核心功能-->
<activity
android:name="cn.jpush.android.ui.PushActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="cn.jpush.android.ui.PushActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="$PACKAGE_NAME"/>
</intent-filter>
</activity>
<!-- Required SDK核心功能-->
<service
android:name="cn.jpush.android.service.DownloadService"
android:enabled="true"
android:exported="false">
</service>
<!-- Required SDK 核心功能-->
<service
android:name="cn.jpush.android.service.PushService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTER"/>
<action android:name="cn.jpush.android.intent.REPORT"/>
<action android:name="cn.jpush.android.intent.PushService"/>
<action android:name="cn.jpush.android.intent.PUSH_TIME"/>
</intent-filter>
</service>
<!-- Required SDK核心功能-->
<receiver
android:name="cn.jpush.android.service.PushReceiver"
android:enabled="true">
<intent-filter android:priority="1000">
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY"/>
<!--Required 显示通知栏 -->
<category android:name="$PACKAGE_NAME"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT"/>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
<!-- Optional -->
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
<!-- User defined. For test only 用户自定义的广播接收器 -->
<receiver
android:name="cn.jpush.phonegap.MyReceiver"
android:enabled="true">
<intent-filter android:priority="1000">
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY"/>
<!-- Required 显示通知栏 -->
<category android:name="$PACKAGE_NAME"/>
</intent-filter>
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION"/>
<!-- Required 用户注册SDK的intent -->
<action android:name="cn.jpush.android.intent.UNREGISTRATION"/>
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED"/>
<!-- Required 用户接收SDK消息的intent -->
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED"/>
<!-- Required 用户接收SDK通知栏信息的intent -->
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED"/>
<!-- Required 用户打开自定义通知栏的intent -->
<action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK"/>
<!-- Optional 用户接受Rich Push Javascript 回调函数的intent -->
<category android:name="$PACKAGE_NAME"/>
</intent-filter>
</receiver>
<!-- Required SDK核心功能-->
<receiver android:name="cn.jpush.android.service.AlarmReceiver"/>
<!-- Required . Enable it you can get statistics data with channel -->
<meta-data android:name="JPUSH_CHANNEL" android:value="developer-default"/>
<meta-data android:name="JPUSH_APPKEY" android:value="299d0fee887820e7d90a68b2"/>
</config-file>
<source-file src="src/android/jpush-sdk-release1.5.0.jar" target-dir="libs"/>
<source-file src="src/android/armeabi/libjpush.so" target-dir="libs/armeabi"/>
<source-file src="src/android/JPushPlugin.java" target-dir="src/cn/jpush/phonegap"/>
<source-file src="src/android/MyReceiver.java" target-dir="src/cn/jpush/phonegap"/>
<source-file src="src/android/test_notification_layout.xml" target-dir="res/layout"/>
<source-file src="src/android/jpush_notification_icon.png" target-dir="res/drawable"/>
</platform>
</plugin>

View File

@ -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);
}
}

View File

@ -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

BIN
src/android/libjpush.so Normal file

Binary file not shown.

View File

@ -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>

46
www/JPushPlugin.js Normal file
View File

@ -0,0 +1,46 @@
var JPushPlugin = function(){
};
JPushPlugin.prototype.failure = function (msg) {
console.log("Javascript Callback Error: " + msg)
}
JPushPlugin.prototype.call_native = function (callback, name, args) {
if(arguments.length == 2) {
args = []
}
ret = cordova.exec(
callback, // called when signature capture is successful
this.failure, // called when signature capture encounters an error
'JPushPlugin', // Tell cordova that we want to run "JPushPlugin"
name, // Tell the plugin the action we want to perform
args); // List of arguments to the plugin
return ret;
}
JPushPlugin.prototype.setTags = function (tags, callback) {
this.call_native(callback, "setTags", [tags])
}
JPushPlugin.prototype.setAlias = function (alias, callback) {
this.call_native(callback, "setAlias", [alias])
}
JPushPlugin.prototype.pushCallback = function (data) {
var strArr = [data]
var str = strArr[0].message
document.getElementById('tarea').value=str
}
JPushPlugin.prototype.getIncoming = function (callback) {
this.call_native(callback, "getIncoming");
}
JPushPlugin.prototype.setBasicPushNotificationBuilder = function(callback){
this.call_native(callback,"setBasicPushNotificationBuilder");
}
JPushPlugin.prototype.setCustomPushNotificationBuilder = function(callback){
this.call_native(callback,"setCustomPushNotificationBuilder");
}
// Register the plugin
if(!window.plugins) {
window.plugins = {};
}
if(!window.plugins.jPushPlugin){
window.plugins.jPushPlugin = new JPushPlugin();
}