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;
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;
/** JPushPlugin */
public class JPushPlugin implements MethodCallHandler {
/**
* JPushPlugin
*/
public class JPushPlugin implements FlutterPlugin, MethodCallHandler {
/** Plugin registration. */
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "jpush");
channel.setMethodCallHandler(new JPushPlugin(registrar, channel));
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,23 +42,33 @@ 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);
@ -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")) {
@ -112,8 +111,7 @@ public class JPushPlugin implements MethodCallHandler {
isNotificationEnabled(call, result);
} else if (call.method.equals("openSettingsForNotification")) {
openSettingsForNotification(call, result);
}
else {
} else {
result.notImplemented();
}
}
@ -141,10 +139,10 @@ public class JPushPlugin implements MethodCallHandler {
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);
JPushInterface.setChannel(context, channel);
JPushPlugin.instance.dartIsReady = true;
@ -167,12 +165,12 @@ public class JPushPlugin implements MethodCallHandler {
openNotificationCacheList.removeAll(tempList);
}
if (registrar == null || registrar.context() == null) {
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
@ -194,7 +192,7 @@ public class JPushPlugin implements MethodCallHandler {
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) {
@ -202,7 +200,7 @@ public class JPushPlugin implements MethodCallHandler {
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.cleanTags(registrar.context(), sequence);
JPushInterface.cleanTags(context, sequence);
}
public void addTags(MethodCall call, Result result) {
@ -212,7 +210,7 @@ public class JPushPlugin implements MethodCallHandler {
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) {
@ -222,7 +220,7 @@ public class JPushPlugin implements MethodCallHandler {
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) {
@ -230,7 +228,7 @@ public class JPushPlugin implements MethodCallHandler {
sequence += 1;
callbackMap.put(sequence, result);
JPushInterface.getAllTags(registrar.context(), sequence);
JPushInterface.getAllTags(context, sequence);
}
public void setAlias(MethodCall call, Result result) {
@ -239,7 +237,7 @@ public class JPushPlugin implements MethodCallHandler {
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) {
@ -248,31 +246,32 @@ public class JPushPlugin implements MethodCallHandler {
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:");
JPushInterface.stopPush(registrar.context());
JPushInterface.stopPush(context);
}
public void resumePush(MethodCall call, Result result) {
Log.d(TAG, "resumePush:");
JPushInterface.resumePush(registrar.context());
JPushInterface.resumePush(context);
}
public void clearAllNotifications(MethodCall call, Result result) {
Log.d(TAG, "clearAllNotifications: ");
JPushInterface.clearAllNotifications(registrar.context());
JPushInterface.clearAllNotifications(context);
}
public void clearNotification(MethodCall call, Result result) {
Log.d(TAG, "clearNotification: ");
Object id = call.arguments;
if (id != null) {
JPushInterface.clearNotificationById(registrar.context(),(int)id);
JPushInterface.clearNotificationById(context, (int) id);
}
}
@ -285,12 +284,12 @@ public class JPushPlugin implements MethodCallHandler {
public void getRegistrationID(MethodCall call, Result result) {
Log.d(TAG, "getRegistrationID: ");
if (registrar == null || registrar.context() == null) {
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 {
@ -320,7 +319,7 @@ 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();
}
@ -333,7 +332,7 @@ public class JPushPlugin implements MethodCallHandler {
Object numObject = map.get("badge");
if (numObject != null) {
int num = (int) numObject;
JPushInterface.setBadgeNumber(registrar.context(),num);
JPushInterface.setBadgeNumber(context, num);
result.success(true);
}
}
@ -341,7 +340,7 @@ public class JPushPlugin implements MethodCallHandler {
/// 检查当前应用的通知开关是否开启
private void isNotificationEnabled(MethodCall call, Result result) {
Log.d(TAG, "isNotificationEnabled: ");
int isEnabled = JPushInterface.isNotificationEnabled(registrar.context());
int isEnabled = JPushInterface.isNotificationEnabled(context);
//1表示开启0表示关闭-1表示检测失败
HashMap<String, Object> map = new HashMap();
map.put("isEnabled", isEnabled == 1 ? true : false);
@ -352,7 +351,7 @@ public class JPushPlugin implements MethodCallHandler {
private void openSettingsForNotification(MethodCall call, Result result) {
Log.d(TAG, "openSettingsForNotification: ");
JPushInterface.goToAppNotificationSettings(registrar.context());
JPushInterface.goToAppNotificationSettings(context);
}
@ -369,7 +368,6 @@ public class JPushPlugin implements MethodCallHandler {
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

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