flutter 适配 2.0 ,2.0以下版本使用 0.6.3

master
qingqing.wang 4 years ago
parent e4618b10d5
commit fa7f70c174

@ -37,6 +37,6 @@ dependencies {
implementation 'cn.jiguang.sdk:jpush:3.9.0'
implementation 'cn.jiguang.sdk:jcore:2.6.0'
// implementation 'com.android.support:appcompat-v7:28.+'
compileOnly files('libs/flutter.jar')
// compileOnly files('libs/flutter.jar')
}

@ -9,14 +9,6 @@ import android.util.Log;
import org.json.JSONObject;
import cn.jpush.android.data.JPushLocalNotification;
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;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -26,27 +18,23 @@ import java.util.Map;
import java.util.Set;
import cn.jpush.android.api.JPushInterface;
import io.flutter.view.FlutterNativeView;
/** JPushPlugin */
public class JPushPlugin implements MethodCallHandler {
import cn.jpush.android.data.JPushLocalNotification;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
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;
/** Plugin registration. */
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "jpush");
channel.setMethodCallHandler(new JPushPlugin(registrar, channel));
/**
* JPushPlugin
*/
public class JPushPlugin implements FlutterPlugin, MethodCallHandler {
registrar.addViewDestroyListener(new PluginRegistry.ViewDestroyListener() {
@Override
public boolean onViewDestroy(FlutterNativeView flutterNativeView) {
instance.dartIsReady = false;
return false;
}
});
}
private static String TAG = "| JPUSH | Flutter | Android | ";
public static JPushPlugin instance;
static List<Map<String, Object>> openNotificationCache = new ArrayList<>();
private boolean dartIsReady = false;
@ -54,26 +42,36 @@ public class JPushPlugin implements MethodCallHandler {
private List<Result> getRidCache;
private final Registrar registrar;
private final MethodChannel channel;
public final Map<Integer, Result> callbackMap;
private Context context;
private MethodChannel channel;
public Map<Integer, Result> callbackMap;
private int sequence;
private JPushPlugin(Registrar registrar, MethodChannel channel) {
this.registrar = registrar;
this.channel = channel;
public JPushPlugin() {
this.callbackMap = new HashMap<>();
this.sequence = 0;
this.getRidCache = new ArrayList<>();
instance = this;
}
@Override
public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "jpush");
channel.setMethodCallHandler(this);
context = flutterPluginBinding.getApplicationContext();
}
@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
instance.dartIsReady = false;
}
@Override
public void onMethodCall(MethodCall call, Result result) {
Log.i(TAG,call.method);
Log.i(TAG, call.method);
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else if (call.method.equals("setup")) {
@ -91,7 +89,8 @@ public class JPushPlugin implements MethodCallHandler {
} else if (call.method.equals("setAlias")) {
setAlias(call, result);
} else if (call.method.equals("deleteAlias")) {
deleteAlias(call, result);;
deleteAlias(call, result);
;
} else if (call.method.equals("stopPush")) {
stopPush(call, result);
} else if (call.method.equals("resumePush")) {
@ -99,7 +98,7 @@ public class JPushPlugin implements MethodCallHandler {
} else if (call.method.equals("clearAllNotifications")) {
clearAllNotifications(call, result);
} else if (call.method.equals("clearNotification")) {
clearNotification(call,result);
clearNotification(call, result);
} else if (call.method.equals("getLaunchAppNotification")) {
getLaunchAppNotification(call, result);
} else if (call.method.equals("getRegistrationID")) {
@ -112,21 +111,20 @@ public class JPushPlugin implements MethodCallHandler {
isNotificationEnabled(call, result);
} else if (call.method.equals("openSettingsForNotification")) {
openSettingsForNotification(call, result);
}
else {
} else {
result.notImplemented();
}
}
// 主线程再返回数据
public void runMainThread(final Map<String,Object> map, final Result result, final String method) {
Log.d(TAG,"runMainThread:" + "map = " + map + ",method =" + method);
public void runMainThread(final Map<String, Object> map, final Result result, final String method) {
Log.d(TAG, "runMainThread:" + "map = " + map + ",method =" + method);
android.os.Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
if (result == null && method != null){
channel.invokeMethod(method,map);
if (result == null && method != null) {
channel.invokeMethod(method, map);
} else {
result.success(map);
}
@ -135,16 +133,16 @@ public class JPushPlugin implements MethodCallHandler {
}
public void setup(MethodCall call, Result result) {
Log.d(TAG,"setup :" + call.arguments);
Log.d(TAG, "setup :" + call.arguments);
HashMap<String, Object> map = call.arguments();
boolean debug = (boolean)map.get("debug");
boolean debug = (boolean) map.get("debug");
JPushInterface.setDebugMode(debug);
JPushInterface.init(registrar.context()); // 初始化 JPush
JPushInterface.init(context); // 初始化 JPush
String channel = (String)map.get("channel");
JPushInterface.setChannel(registrar.context(), channel);
String channel = (String) map.get("channel");
JPushInterface.setChannel(context, channel);
JPushPlugin.instance.dartIsReady = true;
@ -153,33 +151,33 @@ public class JPushPlugin implements MethodCallHandler {
}
public void scheduleCache() {
Log.d(TAG,"scheduleCache:");
Log.d(TAG, "scheduleCache:");
List<Object> tempList = new ArrayList<Object>();
if (dartIsReady) {
// try to shedule notifcation cache
List<Map<String, Object>> openNotificationCacheList = JPushPlugin.openNotificationCache;
for (Map<String, Object> notification: openNotificationCacheList) {
for (Map<String, Object> notification : openNotificationCacheList) {
JPushPlugin.instance.channel.invokeMethod("onOpenNotification", notification);
tempList.add(notification);
}
openNotificationCacheList.removeAll(tempList);
}
if (registrar == null || registrar.context() == null) {
Log.d(TAG,"scheduleCacheregister context is nil.");
if (context == null) {
Log.d(TAG, "scheduleCacheregister context is nil.");
return;
}
String rid = JPushInterface.getRegistrationID(registrar.context());
String rid = JPushInterface.getRegistrationID(context);
boolean ridAvailable = rid != null && !rid.isEmpty();
if (ridAvailable && dartIsReady) {
// try to schedule get rid cache
tempList.clear();
List<Result> resultList = JPushPlugin.instance.getRidCache;
for (Result res: resultList) {
Log.d(TAG,"scheduleCache rid = " + rid);
for (Result res : resultList) {
Log.d(TAG, "scheduleCache rid = " + rid);
res.success(rid);
tempList.add(res);
}
@ -188,109 +186,110 @@ public class JPushPlugin implements MethodCallHandler {
}
public void setTags(MethodCall call, Result result) {
Log.d(TAG,"setTags");
Log.d(TAG, "setTags");
List<String>tagList = call.arguments();
List<String> tagList = call.arguments();
Set<String> tags = new HashSet<>(tagList);
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.setTags(registrar.context(), sequence, tags);
JPushInterface.setTags(context, sequence, tags);
}
public void cleanTags(MethodCall call, Result result) {
Log.d(TAG,"cleanTags:");
Log.d(TAG, "cleanTags:");
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.cleanTags(registrar.context(), sequence);
JPushInterface.cleanTags(context, sequence);
}
public void addTags(MethodCall call, Result result) {
Log.d(TAG,"addTags: " + call.arguments);
Log.d(TAG, "addTags: " + call.arguments);
List<String>tagList = call.arguments();
List<String> tagList = call.arguments();
Set<String> tags = new HashSet<>(tagList);
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.addTags(registrar.context(), sequence, tags);
JPushInterface.addTags(context, sequence, tags);
}
public void deleteTags(MethodCall call, Result result) {
Log.d(TAG,"deleteTags " + call.arguments);
Log.d(TAG, "deleteTags " + call.arguments);
List<String>tagList = call.arguments();
List<String> tagList = call.arguments();
Set<String> tags = new HashSet<>(tagList);
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.deleteTags(registrar.context(), sequence, tags);
JPushInterface.deleteTags(context, sequence, tags);
}
public void getAllTags(MethodCall call, Result result) {
Log.d(TAG,"getAllTags ");
Log.d(TAG, "getAllTags ");
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.getAllTags(registrar.context(), sequence);
JPushInterface.getAllTags(context, sequence);
}
public void setAlias(MethodCall call, Result result) {
Log.d(TAG,"setAlias: " + call.arguments);
Log.d(TAG, "setAlias: " + call.arguments);
String alias= call.arguments();
String alias = call.arguments();
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.setAlias(registrar.context(), sequence, alias);
JPushInterface.setAlias(context, sequence, alias);
}
public void deleteAlias(MethodCall call, Result result) {
Log.d(TAG,"deleteAlias:");
Log.d(TAG, "deleteAlias:");
String alias= call.arguments();
String alias = call.arguments();
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.deleteAlias(registrar.context(), sequence);
JPushInterface.deleteAlias(context, sequence);
}
public void stopPush(MethodCall call, Result result) {
Log.d(TAG,"stopPush:");
Log.d(TAG, "stopPush:");
JPushInterface.stopPush(registrar.context());
JPushInterface.stopPush(context);
}
public void resumePush(MethodCall call, Result result) {
Log.d(TAG,"resumePush:");
Log.d(TAG, "resumePush:");
JPushInterface.resumePush(registrar.context());
JPushInterface.resumePush(context);
}
public void clearAllNotifications(MethodCall call, Result result) {
Log.d(TAG,"clearAllNotifications: ");
Log.d(TAG, "clearAllNotifications: ");
JPushInterface.clearAllNotifications(registrar.context());
JPushInterface.clearAllNotifications(context);
}
public void clearNotification(MethodCall call, Result result) {
Log.d(TAG,"clearNotification: ");
Log.d(TAG, "clearNotification: ");
Object id = call.arguments;
if (id != null) {
JPushInterface.clearNotificationById(registrar.context(),(int)id);
JPushInterface.clearNotificationById(context, (int) id);
}
}
public void getLaunchAppNotification(MethodCall call, Result result) {
Log.d(TAG,"");
Log.d(TAG, "");
}
public void getRegistrationID(MethodCall call, Result result) {
Log.d(TAG,"getRegistrationID: ");
Log.d(TAG, "getRegistrationID: ");
if (registrar == null || registrar.context() == null) {
Log.d(TAG,"register context is nil.");
if (context == null) {
Log.d(TAG, "register context is nil.");
return;
}
String rid = JPushInterface.getRegistrationID(registrar.context());
String rid = JPushInterface.getRegistrationID(context);
if (rid == null || rid.isEmpty()) {
getRidCache.add(result);
} else {
@ -300,17 +299,17 @@ public class JPushPlugin implements MethodCallHandler {
public void sendLocalNotification(MethodCall call, Result result) {
Log.d(TAG,"sendLocalNotification: " + call.arguments);
Log.d(TAG, "sendLocalNotification: " + call.arguments);
try {
HashMap<String, Object> map = call.arguments();
JPushLocalNotification ln = new JPushLocalNotification();
ln.setBuilderId((Integer)map.get("buildId"));
ln.setNotificationId((Integer)map.get("id"));
ln.setBuilderId((Integer) map.get("buildId"));
ln.setNotificationId((Integer) map.get("id"));
ln.setTitle((String) map.get("title"));
ln.setContent((String) map.get("content"));
HashMap<String, Object> extra = (HashMap<String, Object>)map.get("extra");
HashMap<String, Object> extra = (HashMap<String, Object>) map.get("extra");
if (extra != null) {
JSONObject json = new JSONObject(extra);
@ -320,39 +319,39 @@ public class JPushPlugin implements MethodCallHandler {
long date = (long) map.get("fireTime");
ln.setBroadcastTime(date);
JPushInterface.addLocalNotification(registrar.context(), ln);
JPushInterface.addLocalNotification(context, ln);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setBadge(MethodCall call, Result result) {
Log.d(TAG,"setBadge: " + call.arguments);
Log.d(TAG, "setBadge: " + call.arguments);
HashMap<String, Object> map = call.arguments();
Object numObject = map.get("badge");
if (numObject != null) {
int num = (int)numObject;
JPushInterface.setBadgeNumber(registrar.context(),num);
int num = (int) numObject;
JPushInterface.setBadgeNumber(context, num);
result.success(true);
}
}
/// 检查当前应用的通知开关是否开启
private void isNotificationEnabled(MethodCall call, Result result) {
Log.d(TAG,"isNotificationEnabled: ");
int isEnabled = JPushInterface.isNotificationEnabled(registrar.context());
Log.d(TAG, "isNotificationEnabled: ");
int isEnabled = JPushInterface.isNotificationEnabled(context);
//1表示开启0表示关闭-1表示检测失败
HashMap<String, Object> map = new HashMap();
map.put("isEnabled",isEnabled==1?true:false);
map.put("isEnabled", isEnabled == 1 ? true : false);
runMainThread(map,result,null);
runMainThread(map, result, null);
}
private void openSettingsForNotification(MethodCall call, Result result) {
Log.d(TAG,"openSettingsForNotification: " );
Log.d(TAG, "openSettingsForNotification: ");
JPushInterface.goToAppNotificationSettings(registrar.context());
JPushInterface.goToAppNotificationSettings(context);
}
@ -369,14 +368,13 @@ public class JPushPlugin implements MethodCallHandler {
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(JPushInterface.ACTION_REGISTRATION_ID)) {
String rId = intent.getStringExtra(JPushInterface.EXTRA_REGISTRATION_ID);
Log.d("JPushPlugin","on get registration");
Log.d("JPushPlugin", "on get registration");
JPushPlugin.transmitReceiveRegistrationId(rId);
} else if (action.equals(JPushInterface.ACTION_MESSAGE_RECEIVED)) {
@ -389,7 +387,7 @@ public class JPushPlugin implements MethodCallHandler {
}
private void handlingMessageReceive(Intent intent) {
Log.d(TAG,"handlingMessageReceive " + intent.getAction());
Log.d(TAG, "handlingMessageReceive " + intent.getAction());
String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE);
Map<String, Object> extras = getNotificationExtras(intent);
@ -397,7 +395,7 @@ public class JPushPlugin implements MethodCallHandler {
}
private void handlingNotificationOpen(Context context, Intent intent) {
Log.d(TAG,"handlingNotificationOpen " + intent.getAction());
Log.d(TAG, "handlingNotificationOpen " + intent.getAction());
String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT);
@ -413,7 +411,7 @@ public class JPushPlugin implements MethodCallHandler {
}
private void handlingNotificationReceive(Context context, Intent intent) {
Log.d(TAG,"handlingNotificationReceive " + intent.getAction());
Log.d(TAG, "handlingNotificationReceive " + intent.getAction());
String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT);
@ -422,7 +420,7 @@ public class JPushPlugin implements MethodCallHandler {
}
private Map<String, Object> getNotificationExtras(Intent intent) {
Log.d(TAG,"");
Log.d(TAG, "");
Map<String, Object> extrasMap = new HashMap<String, Object>();
for (String key : intent.getExtras().keySet()) {
@ -440,12 +438,12 @@ public class JPushPlugin implements MethodCallHandler {
static void transmitMessageReceive(String message, Map<String, Object> extras) {
Log.d(TAG,"transmitMessageReceive " + "message=" + message + "extras=" + extras);
Log.d(TAG, "transmitMessageReceive " + "message=" + message + "extras=" + extras);
if (instance == null) {
return;
}
Map<String, Object> msg= new HashMap<>();
Map<String, Object> msg = new HashMap<>();
msg.put("message", message);
msg.put("extras", extras);
@ -453,9 +451,9 @@ public class JPushPlugin implements MethodCallHandler {
}
static void transmitNotificationOpen(String title, String alert, Map<String, Object> extras) {
Log.d(TAG,"transmitNotificationOpen " + "title=" + title + "alert=" + alert + "extras=" + extras);
Log.d(TAG, "transmitNotificationOpen " + "title=" + title + "alert=" + alert + "extras=" + extras);
Map<String, Object> notification= new HashMap<>();
Map<String, Object> notification = new HashMap<>();
notification.put("title", title);
notification.put("alert", alert);
notification.put("extras", extras);
@ -475,13 +473,13 @@ public class JPushPlugin implements MethodCallHandler {
}
static void transmitNotificationReceive(String title, String alert, Map<String, Object> extras) {
Log.d(TAG,"transmitNotificationReceive " + "title=" + title + "alert=" + alert + "extras=" + extras);
Log.d(TAG, "transmitNotificationReceive " + "title=" + title + "alert=" + alert + "extras=" + extras);
if (instance == null) {
return;
}
Map<String, Object> notification= new HashMap<>();
Map<String, Object> notification = new HashMap<>();
notification.put("title", title);
notification.put("alert", alert);
notification.put("extras", extras);
@ -489,7 +487,7 @@ public class JPushPlugin implements MethodCallHandler {
}
static void transmitReceiveRegistrationId(String rId) {
Log.d(TAG,"transmitReceiveRegistrationId " + rId);
Log.d(TAG, "transmitReceiveRegistrationId " + rId);
if (instance == null) {
return;

@ -28,12 +28,27 @@
there is no splash screen (such as the default splash screen
defined in @style/LaunchTheme). -->
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<!-- Displays an Android View that continues showing the launch screen
Drawable until Flutter paints its first frame, then this splash
screen fades out. A splash screen is useful to avoid any visual
gap between the end of Android's launch screen and the painting of
Flutter's first frame. -->
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>

@ -1,13 +1,6 @@
package com.jiguang.jpushexample;
import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.embedding.android.FlutterActivity;
public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
}
}

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Theme applied to the Android Window while the process is starting when the OS's Dark Mode setting is on -->
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
<!-- Show a splash screen on the activity. Automatically removed when
Flutter draws its first frame -->
<item name="android:windowBackground">@drawable/launch_background</item>
</style>
<!-- Theme applied to the Android Window as soon as the process has started.
This theme determines the color of the Android Window while your
Flutter UI initializes, as well as behind your Flutter UI while its
running.
This Theme is only used starting with V2 of Flutter's Android embedding. -->
<style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
<item name="android:windowBackground">?android:colorBackground</item>
</style>
</resources>

@ -1,8 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
<!-- Show a splash screen on the activity. Automatically removed when
Flutter draws its first frame -->
<item name="android:windowBackground">@drawable/launch_background</item>
</style>
<!-- Theme applied to the Android Window as soon as the process has started.
This theme determines the color of the Android Window while your
Flutter UI initializes, as well as behind your Flutter UI while its
running.
This Theme is only used starting with V2 of Flutter's Android embedding. -->
<style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
<item name="android:windowBackground">?android:colorBackground</item>
</style>
</resources>

@ -336,14 +336,13 @@ class CustomButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new FlatButton(
return new TextButton(
onPressed: onPressed,
child: new Text("$title"),
color: Color(0xff585858),
highlightColor: Color(0xff888888),
splashColor: Color(0xff888888),
textColor: Colors.white,
//padding: EdgeInsets.fromLTRB(5, 5, 5, 5),
style: new ButtonStyle(foregroundColor: MaterialStateProperty.all(Colors.white),
overlayColor: MaterialStateProperty.all(Color(0xff888888)),
backgroundColor: MaterialStateProperty.all(Color(0xff585858)),
padding: MaterialStateProperty.all(EdgeInsets.fromLTRB(10, 5, 10, 5)), ),
);
}
}

@ -6,7 +6,7 @@ import 'package:platform/platform.dart';
typedef Future<dynamic> EventHandler(Map<String, dynamic> event);
class JPush {
final String flutter_log = "| JPUSH | Flutter | ";
static const String flutter_log = "| JPUSH | Flutter | ";
factory JPush() => _instance;
final MethodChannel _channel;

@ -1,6 +1,6 @@
name: jpush_flutter
description: JIGUANG officially supported JPush Flutter plugin (Android & iOS). 极光推送官方支持的 Flutter 插件Android & iOS(https://www.jiguang.cn).
version: 0.6.3
version: 2.0.1
# author: xudong.rao <xudong.rao@outlook.com>
homepage: https://www.jiguang.cn
@ -9,13 +9,13 @@ environment:
flutter: ">=1.10.0"
dependencies:
platform: ^2.0.0
platform: ^3.0.0
flutter:
sdk: flutter
dev_dependencies:
test: ^1.3.0
mockito: ^3.0.0
test: ^1.16.8
mockito: ^5.0.2
# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

Loading…
Cancel
Save