add android plugin

master
huangminlinux 7 years ago
parent ad85baa20a
commit 0fff34327d

@ -32,3 +32,8 @@ android {
disable 'InvalidPackage'
}
}
dependencies {
compile 'cn.jiguang.sdk:jpush:3.1.6' // JPush 3.1.6
compile 'cn.jiguang.sdk:jcore:1.2.5' // JCore 1.2.5
}

@ -1,3 +1,26 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jiguang.jpush">
<application>
<receiver
android:name="com.jiguang.jpush.JPushPlugin$JPushReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" />
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_CLICK_ACTION" />
<action android:name="cn.jpush.android.intent.CONNECTION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<receiver android:name="com.jiguang.jpush.JPushEventReceiver">
<intent-filter>
<action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
</application>
</manifest>

@ -0,0 +1,115 @@
package com.jiguang.jpush;
import android.content.Context;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.service.JPushMessageReceiver;
import io.flutter.plugin.common.MethodChannel.Result;
public class JPushEventReceiver extends JPushMessageReceiver {
@Override
public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
super.onTagOperatorResult(context, jPushMessage);
JSONObject resultJson = new JSONObject();
int sequence = jPushMessage.getSequence();
try {
resultJson.put("sequence", sequence);
} catch (JSONException e) {
e.printStackTrace();
}
Result callback = JPushPlugin.instance.callbackMap.get(sequence);//instance.eventCallbackMap.get(sequence);
if (callback == null) {
Log.i("JPushPlugin", "Unexpected error, callback is null!");
return;
}
if (jPushMessage.getErrorCode() == 0) { // success
Set<String> tags = jPushMessage.getTags();
List<String> tagList = new ArrayList<>(tags);
Map<String, Object> res = new HashMap<>();
res.put("tags", tagList);
callback.success(res);
} else {
try {
resultJson.put("code", jPushMessage.getErrorCode());
} catch (JSONException e) {
e.printStackTrace();
}
callback.error(Integer.toString(jPushMessage.getErrorCode()), "", "");
}
JPushPlugin.instance.callbackMap.remove(sequence);
}
@Override
public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
super.onCheckTagOperatorResult(context, jPushMessage);
int sequence = jPushMessage.getSequence();
Result callback = JPushPlugin.instance.callbackMap.get(sequence);
if (callback == null) {
Log.i("JPushPlugin", "Unexpected error, callback is null!");
return;
}
if (jPushMessage.getErrorCode() == 0) {
Set<String> tags = jPushMessage.getTags();
List<String> tagList = new ArrayList<>(tags);
Map<String, Object> res = new HashMap<>();
res.put("tags", tagList);
callback.success(res);
} else {
callback.error(Integer.toString(jPushMessage.getErrorCode()), "", "");
}
JPushPlugin.instance.callbackMap.remove(sequence);
}
@Override
public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
super.onAliasOperatorResult(context, jPushMessage);
int sequence = jPushMessage.getSequence();
Result callback = JPushPlugin.instance.callbackMap.get(sequence);
if (callback == null) {
Log.i("JPushPlugin", "Unexpected error, callback is null!");
return;
}
if (jPushMessage.getErrorCode() == 0) { // success
Map<String, Object> res = new HashMap<>();
res.put("alias", (jPushMessage.getAlias() == null)? "" : jPushMessage.getAlias());
callback.success(res);
} else {
callback.error(Integer.toString(jPushMessage.getErrorCode()), "", "");
}
JPushPlugin.instance.callbackMap.remove(sequence);
}
}

@ -1,25 +1,292 @@
package com.jiguang.jpush;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
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 cn.jpush.android.api.JPushInterface;
/** JPushPlugin */
public class JPushPlugin implements MethodCallHandler {
/** Plugin registration. */
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "jpush");
channel.setMethodCallHandler(new JPushPlugin());
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else {
result.notImplemented();
}
}
/** Plugin registration. */
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "jpush");
channel.setMethodCallHandler(new JPushPlugin(registrar, channel));
Log.d("JPushPlugin", "11111 registerWith");
}
public static JPushPlugin instance;
private static Map<String, Object> launchOpenNotification;
private final Registrar registrar;
private final MethodChannel channel;
public final Map<Integer, Result> callbackMap;
private int sequence;
private JPushPlugin(Registrar registrar, MethodChannel channel) {
Log.d("huangmin", "hahahahah initial");
this.registrar = registrar;
this.channel = channel;
this.callbackMap = new HashMap<>();
this.sequence = 0;
instance = this;
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else if (call.method.equals("setup")) {
setup(call, result);
} else if (call.method.equals("setTags")) {
setTags(call, result);
} else if (call.method.equals("cleanTags")) {
cleanTags(call, result);
} else if (call.method.equals("addTags")) {
addTags(call, result);
} else if (call.method.equals("deleteTags")) {
deleteTags(call, result);
} else if (call.method.equals("getAllTags")) {
getAllTags(call, result);
} else if (call.method.equals("setAlias")) {
setAlias(call, result);
} else if (call.method.equals("deleteAlias")) {
deleteAlias(call, result);;
} else if (call.method.equals("stopPush")) {
stopPush(call, result);
} else if (call.method.equals("resumePush")) {
resumePush(call, result);
} else if (call.method.equals("clearAllNotifications")) {
clearAllNotifications(call, result);
} else if (call.method.equals("getLaunchAppNotification")) {
getLaunchAppNotification(call, result);
} else if (call.method.equals("getRegistrationID")) {
getRegistrationID(call, result);
} else {
result.notImplemented();
}
}
public void setup(MethodCall call, Result result) {
JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
JPushInterface.init(registrar.context()); // 初始化 JPush
Log.d("huangmin123456","fdsfadf");
}
public void setTags(MethodCall call, Result result) {
List<String>tagList = call.arguments();
Set<String> tags = new HashSet<>(tagList);
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.setTags(registrar.context(), sequence, tags);
}
public void cleanTags(MethodCall call, Result result) {
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.cleanTags(registrar.context(), sequence);
}
public void addTags(MethodCall call, Result result) {
List<String>tagList = call.arguments();
Set<String> tags = new HashSet<>(tagList);
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.addTags(registrar.context(), sequence, tags);
}
public void deleteTags(MethodCall call, Result result) {
List<String>tagList = call.arguments();
Set<String> tags = new HashSet<>(tagList);
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.deleteTags(registrar.context(), sequence, tags);
}
public void getAllTags(MethodCall call, Result result) {
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.getAllTags(registrar.context(), sequence);
}
public void setAlias(MethodCall call, Result result) {
String alias= call.arguments();
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.setAlias(registrar.context(), sequence, alias);
}
public void deleteAlias(MethodCall call, Result result) {
String alias= call.arguments();
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.deleteAlias(registrar.context(), sequence);
}
public void stopPush(MethodCall call, Result result) {
JPushInterface.stopPush(registrar.context());
}
public void resumePush(MethodCall call, Result result) {
JPushInterface.resumePush(registrar.context());
}
public void clearAllNotifications(MethodCall call, Result result) {
JPushInterface.clearAllNotifications(registrar.context());
}
public void getLaunchAppNotification(MethodCall call, Result result) {
}
public void getRegistrationID(MethodCall call, Result result) {
String rid = JPushInterface.getRegistrationID(registrar.context());
result.success(rid);
}
/**
* ,,广
* :http://docs.jiguang.cn/client/android_api/
*/
public static class JPushReceiver extends BroadcastReceiver {
private static final List<String> IGNORED_EXTRAS_KEYS = Arrays.asList("cn.jpush.android.TITLE",
"cn.jpush.android.MESSAGE", "cn.jpush.android.APPKEY", "cn.jpush.android.NOTIFICATION_CONTENT_TITLE");
public JPushReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d("JPushReceive", "11111 onreceive board cast");
String action = intent.getAction();
if (action.equals(JPushInterface.ACTION_REGISTRATION_ID)) {
String rId = intent.getStringExtra(JPushInterface.EXTRA_REGISTRATION_ID);
JPushPlugin.transmitReceiveRegistrationId(rId);
} else if (action.equals(JPushInterface.ACTION_MESSAGE_RECEIVED)) {
handlingMessageReceive(intent);
} else if (action.equals(JPushInterface.ACTION_NOTIFICATION_RECEIVED)) {
handlingNotificationReceive(context, intent);
} else if (action.equals(JPushInterface.ACTION_NOTIFICATION_OPENED)) {
handlingNotificationOpen(context, intent);
}
}
private void handlingMessageReceive(Intent intent) {
String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE);
Map<String, Object> extras = getNotificationExtras(intent);
JPushPlugin.transmitMessageReceive(msg, extras);
}
private void handlingNotificationOpen(Context context, Intent intent) {
String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT);
Map<String, Object> extras = getNotificationExtras(intent);
JPushPlugin.transmitNotificationOpen(title, alert, extras);
Intent launch = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
if (launch != null) {
launch.addCategory(Intent.CATEGORY_LAUNCHER);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(launch);
}
}
private void handlingNotificationReceive(Context context, Intent intent) {
String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT);
Map<String, Object> extras = getNotificationExtras(intent);
JPushPlugin.transmitNotificationReceive(title, alert, extras);
}
private Map<String, Object> getNotificationExtras(Intent intent) {
Map<String, Object> extrasMap = new HashMap<String, Object>();
for (String key : intent.getExtras().keySet()) {
if (!IGNORED_EXTRAS_KEYS.contains(key)) {
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
extrasMap.put(key, intent.getIntExtra(key, 0));
} else {
extrasMap.put(key, intent.getStringExtra(key));
}
}
}
return extrasMap;
}
}
static void transmitMessageReceive(String message, Map<String, Object> extras) {
if (instance == null) {
return;
}
Log.d("JPushPlugin", "onReceiveMessage native");
Map<String, Object> msg= new HashMap<>();
msg.put("message", message);
msg.put("extras", extras);
JPushPlugin.instance.channel.invokeMethod("onReceiveMessage", msg);
}
static void transmitNotificationOpen(String title, String alert, Map<String, Object> extras) {
if (instance == null) {
return;
}
Log.d("JPushPlugin", "onOpenNotification native");
Map<String, Object> notification= new HashMap<>();
notification.put("title", title);
notification.put("alert", alert);
notification.put("extras", extras);
JPushPlugin.instance.channel.invokeMethod("onOpenNotification", notification);
}
static void transmitNotificationReceive(String title, String alert, Map<String, Object> extras) {
if (instance == null) {
return;
}
Log.d("JPushPlugin", "onReceiveNotification native");
Map<String, Object> notification= new HashMap<>();
notification.put("title", title);
notification.put("alert", alert);
notification.put("extras", extras);
JPushPlugin.instance.channel.invokeMethod("onReceiveNotification", notification);
}
static void transmitReceiveRegistrationId(String rId) {
if (instance == null) {
return;
}
Log.d("JPushPlugin", "transmitReceiveRegistrationId");
JPushPlugin.instance.channel.invokeMethod("ReceiveRegistrationId", "success congratulation!");
}
}

@ -26,21 +26,36 @@ apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 27
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.jiguang.jpushexample"
// applicationId "com.jiguang.jpushexample"
applicationId "com.example.huminios.jpushtest" //JPush .
minSdkVersion 16
targetSdkVersion 27
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
ndk {
// cpu .so
abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64'// 'arm64-v8a',
//
}
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "a1703c14b186a68a66ef86c1", //JPush Appkey.
JPUSH_CHANNEL : "developer-default", //.
]
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
@ -48,6 +63,13 @@ android {
signingConfig signingConfigs.debug
}
}
buildToolsVersion '27.0.3'
packagingOptions {
exclude 'lib/arm64-v8a/libgojni.so'
exclude 'lib/x86/libgojni.so'
exclude 'lib/x86_64/libgojni.so'
}
}
flutter {

@ -43,7 +43,10 @@ class _MyAppState extends State<MyApp> {
JPush.addEventHandler(
onReceiveNotification: (Map<String, dynamic> message) async {
print("flutter onReceiveNotification: $message");
// print("flutter onReceiveNotification: $message");
setState(() {
_platformVersion = "setAlias error: $message";
});
},
onOpenNotification: (Map<String, dynamic> message) async {
print("flutter onOpenNotification: $message");
@ -86,6 +89,7 @@ class _MyAppState extends State<MyApp> {
child: new Column(
children:[
new Text('result: $_platformVersion\n'),
new FlatButton(
child: new Text('applyPushAuthority\n'),
onPressed: () {

@ -1,8 +1,8 @@
name: jpush
description: A new flutter plugin project.
version: 0.0.1
author:
homepage:
author: huminios <380108184@qq.com>
homepage: https://www.jiguang.cn
environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"

Loading…
Cancel
Save